diff options
| author | UltraQbik <no1skill@yandex.ru> | 2024-08-26 23:59:14 +0300 |
|---|---|---|
| committer | UltraQbik <no1skill@yandex.ru> | 2024-08-26 23:59:14 +0300 |
| commit | fae7cca5091c69083f9f2943310ee5e9c5016520 (patch) | |
| tree | 9f152e36b11e0db2f20e99bd9eaac690cb84c079 | |
| parent | f13dcbc4cd81591817da02751a33589c7198d1bd (diff) | |
| download | httpy-fae7cca5091c69083f9f2943310ee5e9c5016520.tar.gz httpy-fae7cca5091c69083f9f2943310ee5e9c5016520.zip | |
Working file manager
| -rw-r--r-- | .gitignore | 1 | ||||
| -rw-r--r-- | src/file_man.py | 67 |
2 files changed, 63 insertions, 5 deletions
diff --git a/.gitignore b/.gitignore index 3b67be5..bf503f9 100644 --- a/.gitignore +++ b/.gitignore @@ -164,3 +164,4 @@ cython_debug/ /key.pem /www/yes.txt /www/images/ +/compress/ diff --git a/src/file_man.py b/src/file_man.py index 1cb3831..bfd2fd4 100644 --- a/src/file_man.py +++ b/src/file_man.py @@ -1,4 +1,5 @@ import os +from typing import Any from src.config import FILE_MAN_PATH_MAP, FILE_MAN_VERBOSE @@ -11,7 +12,12 @@ def list_path(path) -> list[str]: return paths -def generate_path_map(): +def generate_path_map() -> dict[str, dict[str, Any]]: + """ + Generate a full path map for HTTP server + """ + + # generate basic path map path_map = {} for key in FILE_MAN_PATH_MAP.keys(): if not (os.path.exists(FILE_MAN_PATH_MAP[key]["path"]) or os.path.exists(FILE_MAN_PATH_MAP[key]["path"][:-2])): @@ -26,16 +32,67 @@ def generate_path_map(): else: path_map[key] = {"path": FILE_MAN_PATH_MAP[key]["path"]} + # add headers + for val in path_map.values(): + extension = os.path.splitext(val["path"])[1] + headers = {} + match extension: + case ".htm" | ".html": + headers["Content-Type"] = "text/html" + case ".css": + headers["Content-Type"] = "text/css" + case ".txt": + headers["Content-Type"] = "text/plain" + case ".png": + headers["Content-Type"] = "image/png" + case ".webp": + headers["Content-Type"] = "image/avif" + case ".jpg" | ".jpeg": + headers["Content-Type"] = "image/jpeg" + headers["Content-Length"] = os.path.getsize(val["path"]) + val["headers"] = headers + + # print list of paths if FILE_MAN_VERBOSE: print("LIST OF ALLOWED PATHS:") max_key_len = max([len(x) for x in path_map.keys()]) - max_val_len = max([len(x.__repr__()) for x in path_map.values()]) + max_val_len = max([len(x["path"]) for x in path_map.values()]) print(f"\t{'web': ^{max_key_len}} | {'path': ^{max_val_len}}\n" f"\t{'='*max_key_len}=#={'='*max_val_len}") for key, val in path_map.items(): - print(f"\t{key: <{max_key_len}} | {val}") - print("END OF LIST.") + print(f"\t{key: <{max_key_len}} | {val['path']}") + print("END OF LIST.", len(path_map), end="\n\n") + + return path_map + + +def compress_path_map(path_map: dict[str, dict[str, Any]], path_prefix: str = "compress", regen: bool = False): + """ + Compresses all files using gzip + """ + + import gzip + if not os.path.exists(path_prefix): + os.mkdir(path_prefix) + for val in path_map.values(): + filepath = f"{path_prefix}/{val["path"]}" + if not os.path.exists((dirs := os.path.dirname(filepath))): # add missing folders + os.makedirs(dirs) + if not os.path.exists(filepath) or regen: + with gzip.open(filepath, "wb") as comp: # compress + with open(val["path"], "rb") as file: + comp.writelines(file) + val["path"] = filepath + val["headers"]["Content-Length"] = os.path.getsize(filepath) + + if FILE_MAN_VERBOSE: + print("COMPRESSED PATH MAP:") + max_len = max([len(x["path"]) for x in path_map.values()]) + for val in path_map.values(): + print(f"\t'{val['path']: <{max_len}}' {val['headers']['Content-Length']} bytes") + print("END OF LIST.", len(path_map), end="\n\n") + return path_map -PATH_MAP = generate_path_map() +PATH_MAP = compress_path_map(generate_path_map()) |