giants-maps-api/app/utils.py

35 lines
905 B
Python
Raw Normal View History

2021-12-22 15:56:35 +01:00
import zlib
import pathlib
from typing import List
2021-12-22 15:56:35 +01:00
from .config import config
import os
import datetime
2021-12-22 15:56:35 +01:00
def crc32(bytes_in: bytes) -> int:
return zlib.crc32(bytes_in, 0) & 0xffffffff
def map_file_to_dict(map_file: pathlib.Path) -> dict:
with open(map_file, "rb") as fp:
content = fp.read()
crc = crc32(content)
return {
"crc": crc,
"crc_human": hex(crc)[2:].upper(),
"name": map_file.name,
"size": os.stat(map_file).st_size,
"upload_date": datetime.datetime.fromtimestamp(os.stat(map_file).st_ctime),
"blob_location": f"{config['base_url']}/{config['upload_path']}{map_file.name}"
}
def read_all_maps() -> List[dict]:
print("reading all maps")
maps = []
for file in os.listdir(config["upload_path"]):
maps.append(map_file_to_dict(pathlib.Path(config["upload_path"] + "/" + file)))
return maps