Skip to content

Commit

Permalink
updated some ops to be background tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
fullerzz committed Jul 20, 2024
1 parent de56bf3 commit 7685792
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
7 changes: 3 additions & 4 deletions src/smolvault/cache/cache_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ def save_file(self, filename: str, data: bytes) -> str:
f.write(data)
return file_path.as_posix()

def delete_file(self, filename: str) -> None:
file_path = self.cache_dir / filename
if file_path.exists():
file_path.unlink()
def delete_file(self, local_path: str) -> None:
file_path = pathlib.Path(local_path)
file_path.unlink(missing_ok=True)
18 changes: 12 additions & 6 deletions src/smolvault/main.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import json
import logging
import os
import pathlib
import sys
import urllib.parse
from logging.handlers import RotatingFileHandler
from typing import Annotated

from fastapi import Depends, FastAPI, File, Form, UploadFile
from fastapi import BackgroundTasks, Depends, FastAPI, File, Form, UploadFile
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import FileResponse, Response

Expand Down Expand Up @@ -62,15 +62,17 @@ async def upload_file(


@app.get("/file/{name}")
async def get_file(db_client: Annotated[DatabaseClient, Depends(DatabaseClient)], name: str) -> Response:
async def get_file(
db_client: Annotated[DatabaseClient, Depends(DatabaseClient)], name: str, background_tasks: BackgroundTasks
) -> Response:
record = db_client.get_metadata(urllib.parse.unquote(name))
if record is None:
return Response(content=json.dumps({"error": "File not found"}), status_code=404, media_type="application/json")
if record.local_path is None or cache.file_exists(record.local_path) is False:
content = s3_client.download(record.object_key)
record.local_path = cache.save_file(record.file_name, content)
record.cache_timestamp = int(os.path.getmtime(record.local_path))
db_client.update_metadata(record)
record.cache_timestamp = int(pathlib.Path(record.local_path).stat().st_mtime)
background_tasks.add_task(db_client.update_metadata, record)

return FileResponse(path=record.local_path, filename=record.file_name)

Expand Down Expand Up @@ -118,12 +120,16 @@ async def update_file_tags(


@app.delete("/file/{name}")
async def delete_file(db_client: Annotated[DatabaseClient, Depends(DatabaseClient)], name: str) -> Response:
async def delete_file(
db_client: Annotated[DatabaseClient, Depends(DatabaseClient)], name: str, background_tasks: BackgroundTasks
) -> Response:
record: FileMetadataRecord | None = db_client.get_metadata(name)
if record is None:
return Response(content=json.dumps({"error": "File not found"}), status_code=404, media_type="application/json")
s3_client.delete(record.object_key)
db_client.delete_metadata(record)
if record.local_path:
background_tasks.add_task(cache.delete_file, record.local_path)
return Response(
content=json.dumps({"message": "File deleted successfully", "record": record.model_dump()}),
status_code=200,
Expand Down

0 comments on commit 7685792

Please sign in to comment.