diff --git a/app/api.py b/app/api.py index 37e7e5b..5e241a0 100644 --- a/app/api.py +++ b/app/api.py @@ -75,17 +75,7 @@ async def delete_file(filename: str, api_key: str = Depends(verify_api_key)): @app.get("/files/") async def get_list_of_files(api_key: str = Depends(verify_api_key)): - files = db.get_all_files() - return [ - { - "id": f.id, - "name": f.name, - "content_type": f.content_type, - "size": f.size, - "hash": f.hash, - } - for f in files - ] + return db.get_all_files() @app.get("/healthchecker") async def healthchecker(): diff --git a/app/db.py b/app/db.py index e91c8e2..bbcbcfa 100644 --- a/app/db.py +++ b/app/db.py @@ -42,6 +42,11 @@ def to_base36(n: int, width: int) -> str: result.append(chars[rem]) return "".join(reversed(result)).rjust(width, "0") +def get_url_from_id(id: int, extension): + url = f"{to_base36(id, PADDING)}" + if extension: + url += f".{extension}" + return url def from_base36(s: str) -> int: chars = "0123456789abcdefghijklmnopqrstuvwxyz" @@ -63,7 +68,21 @@ Base.metadata.create_all(bind=engine) def get_all_files(): with Session(autoflush=False, bind=engine) as db: statement = select(File) - return db.scalars(statement).all() + files = db.scalars(statement).all() + print(files) + print(type(files)) + print(type(files[0])) + return [ + { + "url": get_url_from_id(f.id, f.extension), + "name": f.name, + "content_type": f.content_type, + "size": f.size, + } + for f in files + ] + + def file_exists(size: int, hash_value: str) -> bool: with Session(bind=engine) as db: @@ -77,9 +96,7 @@ def file_exists(size: int, hash_value: str) -> bool: if existed_file is None: return None - url = f"{to_base36(existed_file.id, PADDING)}" - if existed_file.extension: - url += f".{existed_file.extension}" + url = get_url_from_id(existed_file.id, existed_file.extension) return url @@ -97,10 +114,7 @@ def add_file(filename: str, content_type, size: int, hash): new_file.hash = hash db.add(new_file) db.commit() - url = f"{to_base36(new_file.id, PADDING)}" - if new_file.extension: - url += f".{new_file.extension}" - + url = get_url_from_id(new_file.id, new_file.extension) return url