Skip to content

Commit

Permalink
Remove temp file when is downloaded
Browse files Browse the repository at this point in the history
  • Loading branch information
jcjc712 committed Nov 7, 2023
1 parent 4767606 commit 29112a3
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
4 changes: 3 additions & 1 deletion dataherald/api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,9 @@ def get_response(self, response_id: str) -> Response:
pass

@abstractmethod
def get_response_file(self, response_id: str) -> FileResponse:
def get_response_file(
self, response_id: str, background_tasks: BackgroundTasks
) -> FileResponse:
pass

@abstractmethod
Expand Down
13 changes: 11 additions & 2 deletions dataherald/api/fastapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ def async_scanning(scanner, database, scanner_request, storage):
)


def delete_file(file_location: str):
os.remove(file_location)


class FastAPI(API):
def __init__(self, system: System):
super().__init__(system)
Expand Down Expand Up @@ -369,7 +373,9 @@ def get_response(self, response_id: str) -> Response:
return result

@override
def get_response_file(self, response_id: str) -> FileResponse:
def get_response_file(
self, response_id: str, background_tasks: BackgroundTasks
) -> FileResponse:
response_repository = ResponseRepository(self.storage)
question_repository = QuestionRepository(self.storage)
db_connection_repository = DatabaseConnectionRepository(self.storage)
Expand All @@ -389,8 +395,11 @@ def get_response_file(self, response_id: str) -> FileResponse:

s3 = S3()

file_location = s3.download(result.csv_file_path, db_connection.file_storage)
background_tasks.add_task(delete_file, file_location)

return FileResponse(
s3.download(result.csv_file_path, db_connection.file_storage),
file_location,
media_type="text/csv",
)

Expand Down
6 changes: 4 additions & 2 deletions dataherald/server/fastapi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,9 +307,11 @@ def update_response(self, response_id: str) -> Response:
"""Update a response"""
return self._api.update_response(response_id)

def get_response_file(self, response_id: str) -> FileResponse:
def get_response_file(
self, response_id: str, background_tasks: BackgroundTasks
) -> FileResponse:
"""Get a response file"""
return self._api.get_response_file(response_id)
return self._api.get_response_file(response_id, background_tasks)

def execute_sql_query(self, query: Query) -> tuple[str, dict]:
"""Executes a query on the given db_connection_id"""
Expand Down

0 comments on commit 29112a3

Please sign in to comment.