Skip to content

Commit

Permalink
feature: add load spinner, style error msg
Browse files Browse the repository at this point in the history
  • Loading branch information
mutantsan committed Sep 3, 2023
1 parent d58df2e commit fa57b87
Show file tree
Hide file tree
Showing 12 changed files with 36 additions and 29 deletions.
6 changes: 3 additions & 3 deletions ckanext/unfold/adapters/_7z.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def build_directory_tree(filepath: str, remote: Optional[bool] = False):
with py7zr.SevenZipFile(filepath) as archive:
if archive.needs_password():
raise unf_exception.UnfoldError(
"Archive is protected with password"
"Error. Archive is protected with password"
)

file_list: list[FileInfo] = archive.list()
Expand Down Expand Up @@ -79,11 +79,11 @@ def _prepare_table_data(entry: FileInfo) -> dict[str, Any]:
def get7zlist_from_url(url) -> list[FileInfo]:
"""Download an archive and fetch a file list. 7z file doesn't allow us
to download it partially and fetch only file list."""
resp = requests.get(url)
resp = requests.get(url, timeout=unf_utils.DEFAULT_TIMEOUT)

archive = py7zr.SevenZipFile(BytesIO(resp.content))

if archive.needs_password():
raise unf_exception.UnfoldError("Archive is protected with password")
raise unf_exception.UnfoldError("Error. Archive is protected with password")

return py7zr.SevenZipFile(BytesIO(resp.content)).list()
6 changes: 3 additions & 3 deletions ckanext/unfold/adapters/rar.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def build_directory_tree(
with rarfile.RarFile(filepath) as archive:
if archive.needs_password():
raise unf_exception.UnfoldError(
"Archive is protected with password"
"Error. Archive is protected with password"
)

file_list: list[RarInfo] = archive.infolist()
Expand Down Expand Up @@ -92,11 +92,11 @@ def _fetch_mtime(entry: RarInfo) -> str:
def get_rarlist_from_url(url) -> list[RarInfo]:
"""Download an archive and fetch a file list. Rar file doesn't allow us
to download it partially and fetch only file list."""
resp = requests.get(url)
resp = requests.get(url, timeout=unf_utils.DEFAULT_TIMEOUT)

archive = rarfile.RarFile(BytesIO(resp.content))

if archive.needs_password():
raise unf_exception.UnfoldError("Archive is protected with password")
raise unf_exception.UnfoldError("Error. Archive is protected with password")

return archive.infolist()
2 changes: 1 addition & 1 deletion ckanext/unfold/adapters/rpm.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def get_rpmlist_from_url(url) -> list[RPMInfo]:
"""Download an archive and fetch a file list. Tar file doesn't allow us
to download it partially and fetch only file list, because the information
about each file is stored at the beggining of the file"""
resp = requests.get(url)
resp = requests.get(url, timeout=unf_utils.DEFAULT_TIMEOUT)

return RPMFile(fileobj=BytesIO(resp.content)).getmembers()

Expand Down
2 changes: 1 addition & 1 deletion ckanext/unfold/adapters/tar.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,6 @@ def get_tarlist_from_url(url) -> list[TarInfo]:
"""Download an archive and fetch a file list. Tar file doesn't allow us
to download it partially and fetch only file list, because the information
about each file is stored at the beggining of the file"""
resp = requests.get(url)
resp = requests.get(url, timeout=unf_utils.DEFAULT_TIMEOUT)

return TarFile(fileobj=BytesIO(resp.content)).getmembers()
1 change: 1 addition & 0 deletions ckanext/unfold/adapters/zip.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ def _get_remote_zip_infolist(url: str, start, end) -> list[ZipInfo]:
headers={
"Range": "bytes={}-{}".format(start, end),
},
timeout=unf_utils.DEFAULT_TIMEOUT,
)

return ZipFile(BytesIO(resp.content)).infolist()
2 changes: 1 addition & 1 deletion ckanext/unfold/assets/css/unfold.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 4 additions & 16 deletions ckanext/unfold/assets/js/unfold-init-jstree.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ ckan.module("unfold-init-jstree", function ($, _) {
$.ajax({
url: this.sandbox.url("/api/action/get_archive_structure"),
data: { "id": this.options.resourceId },
success: this._onSuccessRequest,
error: function (xhr, status, error) {}
success: this._onSuccessRequest
});
},

Expand All @@ -31,19 +30,6 @@ ckan.module("unfold-init-jstree", function ($, _) {
$("#jstree-search").val("").trigger("change");
},

_preparePayload: function () {
return { "id": this.options.resourceId };
},

_onErrorRequest: function (xhr, status, error) {
let errMsg = xhr.responseJSON.error;

$(this.el).jstree(true).settings.plugins = [];
$(this.el).jstree(true).destroy();

$("#archive-tree-error").text("Error: " + errMsg)
},

_onSuccessRequest: function (data) {
if (data.result.error) {
this._displayErrorReason(data.result.error);
Expand All @@ -53,7 +39,9 @@ ckan.module("unfold-init-jstree", function ($, _) {
},

_displayErrorReason: function (error) {
$("#archive-tree-error").text("Error: " + error);
$(".archive-tree--spinner").remove();
$("#archive-tree-error span").text(error);
$("#archive-tree-error").toggle();
},

_initJsTree: function (data) {
Expand Down
Binary file added ckanext/unfold/public/error.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added ckanext/unfold/public/spinner.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 10 additions & 2 deletions ckanext/unfold/templates/unfold_preview.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,17 @@
</div>
</div>

<div id="archive-tree" data-module="unfold-init-jstree" data-module-resource-id="{{ resource.id }}"></div>
<div id="archive-tree" data-module="unfold-init-jstree" data-module-resource-id="{{ resource.id }}">
<div class="archive-tree--spinner">
<img src="{{ h.url_for_static('/spinner.gif') }}" width="20">
{{ _("The archive tree is currently being initialized. Please wait...") }}
</div>
</div>

<div id="archive-tree-error"></div>
<div id="archive-tree-error" style="display: none">
<img src="{{ h.url_for_static('/error.gif') }}" width="30">
<span></span>
</div>
</div>

{% asset 'unfold/unfold-js' %}
Expand Down
6 changes: 6 additions & 0 deletions ckanext/unfold/theme/unfold.scss
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,9 @@
display: table;
}
}

#archive-tree-error {
padding: 10px;
border: 1px solid #ced4da;
border-radius: 0.25rem;
}
8 changes: 6 additions & 2 deletions ckanext/unfold/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import ckanext.unfold.types as unf_types

DEFAULT_DATE_FORMAT = "%d/%m/%Y - %H:%M"
DEFAULT_TIMEOUT = 60 # seconds
log = logging.getLogger(__name__)


Expand Down Expand Up @@ -100,7 +101,8 @@ def save_archive_structure(nodes: list[unf_types.Node], resource_id: str) -> Non
"""Save an archive structure to redis to"""
conn = connect_to_redis()
conn.set(
f"ckanext:unfold:tree:{resource_id}", json.dumps([n.model_dump() for n in nodes])
f"ckanext:unfold:tree:{resource_id}",
json.dumps([n.model_dump() for n in nodes]),
)
conn.close()

Expand All @@ -121,7 +123,9 @@ def delete_archive_structure(resource_id: str) -> None:
conn.close()


def get_archive_tree(resource: dict[str, Any]) -> list[unf_types.Node] | list[dict[str, Any]]:
def get_archive_tree(
resource: dict[str, Any]
) -> list[unf_types.Node] | list[dict[str, Any]]:
remote = False

if resource.get("url_type") == "upload":
Expand Down

0 comments on commit fa57b87

Please sign in to comment.