Skip to content

Commit

Permalink
Merge pull request #250 from freedomofpress/rename-files
Browse files Browse the repository at this point in the history
Rename files
  • Loading branch information
heartsucker authored Feb 26, 2019
2 parents 7ccea62 + 1569888 commit b7daf82
Show file tree
Hide file tree
Showing 3 changed files with 205 additions and 59 deletions.
6 changes: 2 additions & 4 deletions securedrop_client/message_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ def __init__(self, api, home, is_qubes):

def run(self, loop=True):
while True:
logger.debug('Syncing messages.')
submissions = storage.find_new_submissions(self.session)

for db_submission in submissions:
Expand All @@ -93,7 +92,7 @@ def run(self, loop=True):
tb = traceback.format_exc()
logger.critical("Exception while downloading submission!\n{}".format(tb))

logger.debug('Completed message sync.')
logger.debug('Submissions synced')

if not loop:
break
Expand All @@ -117,7 +116,6 @@ def __init__(self, api, home, is_qubes):

def run(self, loop=True):
while True:
logger.debug('Syncing replies.')
replies = storage.find_new_replies(self.session)

for db_reply in replies:
Expand All @@ -143,7 +141,7 @@ def run(self, loop=True):
tb = traceback.format_exc()
logger.critical("Exception while downloading reply!\n{}".format(tb))

logger.debug('Completed reply sync.')
logger.debug('Replies synced')

if not loop:
break
Expand Down
41 changes: 29 additions & 12 deletions securedrop_client/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ def update_sources(remote_sources, local_sources, session, data_dir):
# Removing the UUID from local_uuids ensures this record won't be
# deleted at the end of this function.
local_uuids.remove(source.uuid)
logger.info('Updated source {}'.format(source.uuid))
logger.debug('Updated source {}'.format(source.uuid))
else:
# A new source to be added to the database.
ns = Source(uuid=source.uuid,
Expand All @@ -133,7 +133,7 @@ def update_sources(remote_sources, local_sources, session, data_dir):
last_updated=parse(source.last_updated),
document_count=source.number_of_documents)
session.add(ns)
logger.info('Added new source {}'.format(source.uuid))
logger.debug('Added new source {}'.format(source.uuid))

# The uuids remaining in local_uuids do not exist on the remote server, so
# delete the related records.
Expand All @@ -142,7 +142,7 @@ def update_sources(remote_sources, local_sources, session, data_dir):
delete_single_submission_or_reply_on_disk(document, data_dir)

session.delete(deleted_source)
logger.info('Deleted source {}'.format(deleted_source.uuid))
logger.debug('Deleted source {}'.format(deleted_source.uuid))

session.commit()

Expand All @@ -157,9 +157,13 @@ def update_submissions(remote_submissions, local_submissions, session, data_dir)
local_uuids = {submission.uuid for submission in local_submissions}
for submission in remote_submissions:
if submission.uuid in local_uuids:
# Update an existing record.
local_submission = [s for s in local_submissions
if s.uuid == submission.uuid][0]
# Update files on disk to match new filename.
if (local_submission.filename != submission.filename):
rename_file(data_dir, local_submission.filename,
submission.filename)
# Update an existing record.
local_submission.filename = submission.filename
local_submission.size = submission.size
local_submission.is_read = submission.is_read
Expand All @@ -168,7 +172,7 @@ def update_submissions(remote_submissions, local_submissions, session, data_dir)
# Removing the UUID from local_uuids ensures this record won't be
# deleted at the end of this function.
local_uuids.remove(submission.uuid)
logger.info('Updated submission {}'.format(submission.uuid))
logger.debug('Updated submission {}'.format(submission.uuid))
else:
# A new submission to be added to the database.
_, source_uuid = submission.source_url.rsplit('/', 1)
Expand All @@ -178,15 +182,15 @@ def update_submissions(remote_submissions, local_submissions, session, data_dir)
filename=submission.filename,
download_url=submission.download_url)
session.add(ns)
logger.info('Added new submission {}'.format(submission.uuid))
logger.debug('Added new submission {}'.format(submission.uuid))

# The uuids remaining in local_uuids do not exist on the remote server, so
# delete the related records.
for deleted_submission in [s for s in local_submissions
if s.uuid in local_uuids]:
delete_single_submission_or_reply_on_disk(deleted_submission, data_dir)
session.delete(deleted_submission)
logger.info('Deleted submission {}'.format(deleted_submission.uuid))
logger.debug('Deleted submission {}'.format(deleted_submission.uuid))

session.commit()

Expand All @@ -204,16 +208,19 @@ def update_replies(remote_replies, local_replies, session, data_dir):
local_uuids = {reply.uuid for reply in local_replies}
for reply in remote_replies:
if reply.uuid in local_uuids:
# Update an existing record.
local_reply = [r for r in local_replies if r.uuid == reply.uuid][0]
# Update files on disk to match new filename.
if (local_reply.filename != reply.filename):
rename_file(data_dir, local_reply.filename, reply.filename)
# Update an existing record.
user = find_or_create_user(reply.journalist_uuid,
reply.journalist_username, session)
local_reply.journalist_id = user.id
local_reply.filename = reply.filename
local_reply.size = reply.size

local_uuids.remove(reply.uuid)
logger.info('Updated reply {}'.format(reply.uuid))
logger.debug('Updated reply {}'.format(reply.uuid))
else:
# A new reply to be added to the database.
source_uuid = reply.source_uuid
Expand All @@ -226,14 +233,14 @@ def update_replies(remote_replies, local_replies, session, data_dir):
filename=reply.filename,
size=reply.size)
session.add(nr)
logger.info('Added new reply {}'.format(reply.uuid))
logger.debug('Added new reply {}'.format(reply.uuid))

# The uuids remaining in local_uuids do not exist on the remote server, so
# delete the related records.
for deleted_reply in [r for r in local_replies if r.uuid in local_uuids]:
delete_single_submission_or_reply_on_disk(deleted_reply, data_dir)
session.delete(deleted_reply)
logger.info('Deleted reply {}'.format(deleted_reply.uuid))
logger.debug('Deleted reply {}'.format(deleted_reply.uuid))

session.commit()

Expand Down Expand Up @@ -320,6 +327,16 @@ def delete_single_submission_or_reply_on_disk(obj_db, data_dir):
'File {} already deleted, skipping'.format(file_to_delete))


def rename_file(data_dir: str, filename: str, new_filename: str):
filename, _ = os.path.splitext(filename)
new_filename, _ = os.path.splitext(new_filename)
try:
os.rename(os.path.join(data_dir, filename),
os.path.join(data_dir, new_filename))
except OSError as e:
logger.debug('File could not be renamed: {}'.format(e))


def get_data(sdc_home: str, filename: str) -> str:
filename, _ = os.path.splitext(filename)
full_path = os.path.join(sdc_home, 'data', filename)
Expand All @@ -328,5 +345,5 @@ def get_data(sdc_home: str, filename: str) -> str:
msg = f.read()
except FileNotFoundError:
logger.debug('File not found: {}'.format(full_path))
msg = '<Content deleted>'
msg = '<Not Found>'
return msg
Loading

0 comments on commit b7daf82

Please sign in to comment.