Skip to content

Commit

Permalink
fix: project file clean-up
Browse files Browse the repository at this point in the history
  • Loading branch information
martastain committed Jul 12, 2024
1 parent 2c05bdd commit d457e60
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 13 deletions.
6 changes: 6 additions & 0 deletions api/activities/activity.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ async def post_project_activity(
entity_id: PathEntityID,
user: CurrentUser,
activity: ProjectActivityPostModel,
background_tasks: BackgroundTasks,
x_sender: str | None = Header(default=None),
) -> CreateActivityResponseModel:
"""Create an activity.
Expand Down Expand Up @@ -78,6 +79,8 @@ async def post_project_activity(
data=activity.data,
)

background_tasks.add_task(delete_unused_files, project_name)

return CreateActivityResponseModel(id=id)


Expand All @@ -86,6 +89,7 @@ async def delete_project_activity(
project_name: ProjectName,
activity_id: str,
user: CurrentUser,
background_tasks: BackgroundTasks,
x_sender: str | None = Header(default=None),
) -> EmptyResponse:
"""Delete an activity.
Expand All @@ -106,6 +110,8 @@ async def delete_project_activity(
sender=x_sender,
)

background_tasks.add_task(delete_unused_files, project_name)

return EmptyResponse()


Expand Down
69 changes: 56 additions & 13 deletions ayon_server/helpers/project_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,61 @@ def id_to_path(project_name: str, file_id: str) -> str:
)


async def delete_project_file(project_name: str, file_id: str) -> None:
"""Delete a project file"""

query = f"""
DELETE FROM project_{project_name}.files
WHERE id = $1
"""
await Postgres.execute(query, file_id)

file_id = str(file_id).replace("-", "")
assert len(file_id) == 32

query = f"""
WITH updated_activities AS (
SELECT
id,
jsonb_set(
data,
'{{files}}',
(SELECT jsonb_agg(elem)
FROM jsonb_array_elements(data->'files') elem
WHERE elem->>'id' != '{file_id}')
) AS new_data
FROM
project_{project_name}.activities
WHERE
data->'files' @> jsonb_build_array(
jsonb_build_object('id', '{file_id}')
)
)
UPDATE project_{project_name}.activities
SET data = updated_activities.new_data
FROM updated_activities
WHERE activities.id = updated_activities.id;
"""

await Postgres.execute(query)

path = id_to_path(project_name, file_id)
if not os.path.exists(path):
return

try:
os.remove(path)
except Exception as e:
logging.error(f"Failed to delete file {path}: {e}")

directory = os.path.dirname(path)
if not os.listdir(directory):
try:
os.rmdir(directory)
except Exception as e:
logging.error(f"Failed to delete directory {directory}: {e}")


async def delete_unused_files(project_name: str) -> None:
"""Delete files that are not referenced in any activity."""

Expand All @@ -30,16 +85,4 @@ async def delete_unused_files(project_name: str) -> None:

async for row in Postgres.iterate(query):
logging.debug(f"Deleting unused file {row['id']}")
query = f"""
DELETE FROM project_{project_name}.files
WHERE id = $1
"""
await Postgres.execute(query, row["id"])

path = id_to_path(project_name, row["id"])
try:
os.remove(path)
except FileNotFoundError:
pass
except Exception as e:
logging.error(f"Failed to delete file {path}: {e}")
await delete_project_file(project_name, row["id"])

0 comments on commit d457e60

Please sign in to comment.