Skip to content

Commit

Permalink
feature: remove file from file system on remove file entity
Browse files Browse the repository at this point in the history
  • Loading branch information
mutantsan committed Aug 4, 2023
1 parent de395b5 commit 503deea
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion ckanext/files/logic/action.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,11 @@ def file_delete(context, data_dict):
if not file:
raise tk.ObjectNotFound("File not found")

# TODO: remove file
context["session"].delete(file)
context["session"].commit()

_remove_file_from_filesystem(file.path)

return True


Expand All @@ -98,3 +100,22 @@ def file_show(context, data_dict):
raise tk.ObjectNotFound("File not found")

return file.dictize(context)


def _remove_file_from_filesystem(file_path: str) -> bool:
"""Remove a file from the file system"""
storage_path = get_storage_path()
file_path = os.path.join(storage_path, 'storage', file_path)

if not os.path.exists(file_path):
# TODO: What are we going to do then? Probably, skip silently
return True

try:
os.remove(file_path)
except OSError:
# TODO: in future, we are going to rewrite code a bit, so we could
# track files that are hanging by themselves and clear em
return False

return True

0 comments on commit 503deea

Please sign in to comment.