Skip to content

Commit

Permalink
feature: add file sizes to directory listing (#258)
Browse files Browse the repository at this point in the history
  • Loading branch information
librarian authored Jan 26, 2024
1 parent d23000f commit 01ea147
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
1 change: 1 addition & 0 deletions .github/actions/test/action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ runs:
echo "::group::s3-sync"
if [ "$SYNC_TO_S3" = "true" ];
then
du -h "$TESTS_DATA_DIR/"
sudo -E -H -u github s3cmd sync --follow-symlinks --acl-private --no-progress --stats --no-check-md5 "$TESTS_DATA_DIR/" "$S3_BUCKET_PATH/test_data/"
fi
echo "::endgroup::"
Expand Down
13 changes: 13 additions & 0 deletions .github/scripts/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@
)


def sizeof_fmt(num, suffix="B"):
"""Converts file size to a human-readable format."""

Check failure on line 18 in .github/scripts/index.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] .github/scripts/index.py#L18 <401>

First line should be in imperative mood
Raw output
./scripts/index.py:18:1: D401 First line should be in imperative mood
for unit in ["", "K", "M", "G", "T", "P", "E", "Z"]:
if abs(num) < 1024.0:
return f"{num:3.1f}{unit}{suffix}"
num /= 1024.0
return f"{num:.1f}Yi{suffix}"


def list_files(client, bucket, prefix):
paginator = client.get_paginator("list_objects_v2")
for page in paginator.paginate(Bucket=bucket, Prefix=prefix, Delimiter="/"):
Expand Down Expand Up @@ -49,6 +58,7 @@ def generate_index_html(bucket, files, dirs, current_prefix):
"url": generate_absolute_url(bucket, parent_dir),
"type": "directory",
"date": "",
"size": "", # Directories don't have a size
}
)
for d in dirs:
Expand All @@ -61,6 +71,7 @@ def generate_index_html(bucket, files, dirs, current_prefix):
"url": dir_url,
"type": "directory",
"date": "",
"size": "", # Directories don't have a size
}
)
for f in files:
Expand All @@ -70,12 +81,14 @@ def generate_index_html(bucket, files, dirs, current_prefix):
file_date = datetime.fromtimestamp(f["LastModified"].timestamp()).strftime(
"%Y-%m-%d %H:%M:%S"
)
file_size = sizeof_fmt(f["Size"])
entries.append(
{
"name": os.path.basename(file_key),
"url": file_url,
"type": "file",
"date": file_date,
"size": file_size,
}
)

Expand Down
8 changes: 6 additions & 2 deletions .github/scripts/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,10 @@
.directory-icon:before {
content: '📁'; /* Folder icon */
}
.date {
.date, .size {
float: right;
color: #666;
margin-left: 20px;
color: #999;
}

#timestamp {
Expand All @@ -65,6 +66,9 @@ <h1>Directory listing for {{ directory }}</h1>
{% if entry.date %}
<span class="date">{{ entry.date }}</span>
{% endif %}
{% if entry.size %}
<span class="size">{{ entry.size }}</span>
{% endif %}
</li>
{% endfor %}
</ul>
Expand Down

0 comments on commit 01ea147

Please sign in to comment.