Skip to content

Commit

Permalink
app: delete files in source collection when we delete a source locally
Browse files Browse the repository at this point in the history
  • Loading branch information
redshiftzero authored and sssoleileraaa committed Mar 4, 2020
1 parent ab52f92 commit 84354b7
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
2 changes: 1 addition & 1 deletion securedrop_client/logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -749,7 +749,7 @@ def on_delete_source_success(self, result) -> None:
Handler for when a source deletion succeeds.
"""
# Delete the local version of the source.
storage.delete_local_source_by_uuid(self.session, result)
storage.delete_local_source_by_uuid(self.session, result, self.data_dir)
# Update the sources UI.
self.update_sources()

Expand Down
13 changes: 12 additions & 1 deletion securedrop_client/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import logging
import glob
import os
import shutil
from dateutil.parser import parse
from typing import List, Tuple, Type, Union

Expand All @@ -48,12 +49,13 @@ def get_local_sources(session: Session) -> List[Source]:
return session.query(Source).all()


def delete_local_source_by_uuid(session: Session, uuid: str) -> None:
def delete_local_source_by_uuid(session: Session, uuid: str, data_dir: str) -> None:
"""
Delete the source with the referenced UUID.
"""
source = session.query(Source).filter_by(uuid=uuid).one_or_none()
if source:
delete_source_collection(source.journalist_filename, data_dir)
session.delete(source)
session.commit()
logger.info("Deleted source with UUID {} from local database.".format(uuid))
Expand Down Expand Up @@ -530,6 +532,15 @@ def set_message_or_reply_content(
session.commit()


def delete_source_collection(journalist_filename: str, data_dir: str) -> None:
source_folder = os.path.join(data_dir, journalist_filename)
try:
shutil.rmtree(source_folder)
logging.info('Source documents for {} deleted'.format(journalist_filename))
except FileNotFoundError:
logging.info('No source documents for {} to delete'.format(journalist_filename))


def delete_single_submission_or_reply_on_disk(obj_db: Union[File, Message, Reply],
data_dir: str) -> None:
"""
Expand Down

0 comments on commit 84354b7

Please sign in to comment.