updated get files, show urls
This commit is contained in:
12
app/api.py
12
app/api.py
@@ -75,17 +75,7 @@ async def delete_file(filename: str, api_key: str = Depends(verify_api_key)):
|
|||||||
|
|
||||||
@app.get("/files/")
|
@app.get("/files/")
|
||||||
async def get_list_of_files(api_key: str = Depends(verify_api_key)):
|
async def get_list_of_files(api_key: str = Depends(verify_api_key)):
|
||||||
files = db.get_all_files()
|
return 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
|
|
||||||
]
|
|
||||||
|
|
||||||
@app.get("/healthchecker")
|
@app.get("/healthchecker")
|
||||||
async def healthchecker():
|
async def healthchecker():
|
||||||
|
|||||||
30
app/db.py
30
app/db.py
@@ -42,6 +42,11 @@ def to_base36(n: int, width: int) -> str:
|
|||||||
result.append(chars[rem])
|
result.append(chars[rem])
|
||||||
return "".join(reversed(result)).rjust(width, "0")
|
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:
|
def from_base36(s: str) -> int:
|
||||||
chars = "0123456789abcdefghijklmnopqrstuvwxyz"
|
chars = "0123456789abcdefghijklmnopqrstuvwxyz"
|
||||||
@@ -63,7 +68,21 @@ Base.metadata.create_all(bind=engine)
|
|||||||
def get_all_files():
|
def get_all_files():
|
||||||
with Session(autoflush=False, bind=engine) as db:
|
with Session(autoflush=False, bind=engine) as db:
|
||||||
statement = select(File)
|
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:
|
def file_exists(size: int, hash_value: str) -> bool:
|
||||||
with Session(bind=engine) as db:
|
with Session(bind=engine) as db:
|
||||||
@@ -77,9 +96,7 @@ def file_exists(size: int, hash_value: str) -> bool:
|
|||||||
if existed_file is None:
|
if existed_file is None:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
url = f"{to_base36(existed_file.id, PADDING)}"
|
url = get_url_from_id(existed_file.id, existed_file.extension)
|
||||||
if existed_file.extension:
|
|
||||||
url += f".{existed_file.extension}"
|
|
||||||
|
|
||||||
return url
|
return url
|
||||||
|
|
||||||
@@ -97,10 +114,7 @@ def add_file(filename: str, content_type, size: int, hash):
|
|||||||
new_file.hash = hash
|
new_file.hash = hash
|
||||||
db.add(new_file)
|
db.add(new_file)
|
||||||
db.commit()
|
db.commit()
|
||||||
url = f"{to_base36(new_file.id, PADDING)}"
|
url = get_url_from_id(new_file.id, new_file.extension)
|
||||||
if new_file.extension:
|
|
||||||
url += f".{new_file.extension}"
|
|
||||||
|
|
||||||
return url
|
return url
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user