Skip to content

Commit

Permalink
Remove files and assets on project removal
Browse files Browse the repository at this point in the history
  • Loading branch information
iharsuvorau committed Jan 31, 2024
1 parent 7fbf180 commit 620c977
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 31 deletions.
3 changes: 1 addition & 2 deletions backend/services/api-server/api_server/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,13 @@
from fastapi.responses import JSONResponse
from starlette.middleware.cors import CORSMiddleware

from api_server import users
from api_server.assets.controller import router as assets_router
from api_server.files.blobs_controller import router as blobs_router
from api_server.files.files_controller import router as files_router
from api_server.processing_requests.controller import router as processing_router
from api_server.projects.controller import router as projects_router
from api_server.users.init_db import create_initial_user, create_system_user
from api_server.users.schemas import UserCreate, UserRead, UserUpdate
from api_server.users.schemas import UserCreate, UserRead
from api_server.users.users import auth_backend, current_optional_user, fastapi_users
from api_server.users.users_controller import users_router
from api_server.utils.exceptions.fastapi_handlers import general_exception_handler, http_exception_handler
Expand Down
11 changes: 3 additions & 8 deletions backend/services/api-server/api_server/assets/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,21 +91,16 @@ async def update_asset(
async def delete_asset(self, asset_id: uuid.UUID) -> None:
asset = await self.get_asset(asset_id)
await self.asset_repository.delete_asset(asset_id)

for file_id in asset.files_ids:
involved_assets = await self.get_assets_by_file_id(file_id)

# guard to avoid deleting files that are used by other assets
involved_assets = [asset for asset in involved_assets if (asset.id != asset_id and not asset.deletion_time)]
if len(involved_assets) > 0:
continue

await self.file_service.delete_file(file_id)

async def delete_assets_by_project_id(self, project_id: uuid.UUID) -> None:
assets = await self.get_assets_by_project_id(project_id)
asset_ids = [asset.id for asset in assets]
await self.asset_repository.delete_assets(asset_ids)
for asset in assets:
for file_id in asset.files_ids:
await self.file_service.delete_file(file_id)

async def does_asset_exist(self, asset_id: uuid.UUID) -> bool:
asset = await self.asset_repository.get_asset(asset_id)
Expand Down
20 changes: 1 addition & 19 deletions backend/services/api-server/api_server/files/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,28 +28,10 @@ def __init__(self, file_repository: FileRepository) -> None:

async def save_file(self, name: str, file_type: FileType, file_bytes: bytes, users_ids: list[uuid.UUID]) -> File:
hash = self._compute_sha256(file_bytes)

# if self._hash_exists_on_disk(hash):
# try:
# file = await self.file_repository.get_file_by_hash(hash)
# if file.deletion_time is not None:
# raise FileNotFoundError

# # add users if they don't have access to the file yet
# if not await self.users_have_access_to_file(users_ids, file.id):
# await self.file_repository.add_users_to_file_if_needed(file.id, users_ids)

# raise FileExists(file)
# except FileNotFoundError:
# # File exists on disk but not in database, continue creating the database record
# pass
# else:
file_path = self.base_dir / hash
with file_path.open("wb") as file:
file.write(file_bytes)

url = self._generate_url(hash)

return await self.file_repository.create_file(
name=name,
content_hash=hash,
Expand All @@ -74,7 +56,7 @@ async def delete_file(self, file_id: uuid.UUID) -> None:
# Each file on disk can have multiple references from File entities in the database (to preserve space)
# because we create new files only when the hash of the file content is different.
other_references = await self.file_repository.get_files_by_hash(content_hash)
other_references = [file for file in other_references if file.id != file_id]
other_references = [file for file in other_references if file.id != file_id and file.deletion_time is None]
if len(other_references) == 0:
self._remove_file_from_disk(content_hash)

Expand Down
2 changes: 0 additions & 2 deletions backend/services/api-server/api_server/projects/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,6 @@ async def add_processing_request_to_project(
return await self._project_repository.add_processing_request_to_project(project_id, processing_request_id)

async def delete_project(self, project_id: uuid.UUID) -> None:
# TODO: cancel processing requests

try:
await self._asset_service.delete_assets_by_project_id(project_id)
except Exception:
Expand Down

0 comments on commit 620c977

Please sign in to comment.