Created
August 1, 2024 16:16
-
-
Save FFY00/674372d920ea1a9600b280188dde1a89 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/env python | |
import datetime | |
import locale | |
import os | |
import pathlib | |
import sys | |
import zoneinfo | |
import urllib.parse | |
def main() -> None: | |
root = pathlib.Path(os.environ['DOCUMENT_ROOT']) | |
target = root / os.environ['TARGET'].removeprefix('/') | |
files = [] | |
if target.is_file(): | |
base = target.parent | |
files.append(target) | |
last_modified = target.stat().st_mtime | |
elif target.is_dir(): | |
base = target | |
last_modified = target.stat().st_mtime | |
for path in target.rglob('*'): | |
if path.is_file(): | |
if path.name != 'README.xml': | |
files.append(path) | |
elif path.is_dir(): | |
mtime = path.stat().st_mtime | |
if mtime > last_modified: | |
last_modified = mtime | |
else: | |
print(f'Invalid path: {target}', file=sys.stderr) | |
sys.exit(1) | |
last_modified_local_date = datetime.datetime.fromtimestamp(last_modified) | |
last_modified_date = last_modified_local_date.astimezone(zoneinfo.ZoneInfo('GMT')) | |
# The date format depends on the locale, so make sure we are using the right one. | |
locale.setlocale(locale.LC_ALL, 'en_US.UTF-8') | |
print('X-Archive-Files: zip') | |
print(f'Content-Disposition: attachment; filename={target.name}.zip') | |
print(f'Last-Modified: {last_modified_date:%a, %d %b %Y %H:%M:%S %Z}') | |
print() | |
for file in files: | |
origin = urllib.parse.quote(str(file.relative_to(root))) | |
arcname = file.relative_to(base).as_posix() | |
print(f'- {file.stat().st_size} /{origin} {arcname}') | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment