Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updates the filesize after download and decryption #969

Merged
merged 5 commits into from
Mar 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions securedrop_client/gui/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -2244,6 +2244,13 @@ def __init__(
file_ready_signal.connect(self._on_file_downloaded, type=Qt.QueuedConnection)
file_missing.connect(self._on_file_missing, type=Qt.QueuedConnection)

def update_file_size(self):
try:
self.file_size.setText(humanize_filesize(self.file.size))
except Exception as e:
logger.error(f"Could not update file size on FileWidget: {e}")
self.file_size.setText("")

def eventFilter(self, obj, event):
t = event.type()
if t == QEvent.MouseButtonPress:
Expand All @@ -2267,6 +2274,7 @@ def _set_file_state(self):
self.middot.show()
self.print_button.show()
self.file_name.show()
self.update_file_size()
else:
logger.debug('Changing file {} state to not downloaded'.format(self.uuid))
self.download_button.setText(_('DOWNLOAD'))
Expand Down
3 changes: 3 additions & 0 deletions securedrop_client/logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -768,6 +768,9 @@ def on_file_download_success(self, uuid: Any) -> None:
"""
self.session.commit()
file_obj = storage.get_file(self.session, uuid)
# Let us update the size of the file.
storage.update_file_size(uuid, self.data_dir, self.session)

self.file_ready.emit(file_obj.source.uuid, uuid, file_obj.filename)

def on_file_download_failure(self, exception: Exception) -> None:
Expand Down
12 changes: 12 additions & 0 deletions securedrop_client/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import logging
import os
import shutil
from pathlib import Path
from dateutil.parser import parse
from typing import List, Tuple, Type, Union

Expand Down Expand Up @@ -492,6 +493,17 @@ def mark_as_downloaded(
session.commit()


def update_file_size(uuid: str, path: str, session: Session) -> None:
"""
Updates file size to the decrypted size
"""
db_obj = session.query(File).filter_by(uuid=uuid).one()
stat = Path(db_obj.location(path)).stat()
db_obj.size = stat.st_size
session.add(db_obj)
session.commit()


def mark_as_decrypted(
model_type: Union[Type[File], Type[Message], Type[Reply]],
uuid: str,
Expand Down
2 changes: 1 addition & 1 deletion tests/functional/test_download_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,4 @@ def check_for_sources():
qtbot.wait(5000)
assert file_msg.export_button.isHidden() is False
assert file_msg.file_name.text() == "hello.txt"
assert file_msg.file_size.text() == "625B"
assert file_msg.file_size.text() == "9B"
2 changes: 1 addition & 1 deletion tests/functional/test_export_dialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def check_for_sources():
qtbot.wait(5000)
assert file_msg.export_button.isHidden() is False
assert file_msg.file_name.text() == "hello.txt"
assert file_msg.file_size.text() == "625B"
assert file_msg.file_size.text() == "9B"

# Let us export
qtbot.mouseClick(file_msg.export_button, Qt.LeftButton)
Expand Down
20 changes: 20 additions & 0 deletions tests/gui/test_widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -2397,6 +2397,26 @@ def test_FileWidget__on_print_clicked_missing_file(mocker, session, source):
dialog.assert_not_called()


def test_FileWidget_update_file_size_with_deleted_file(
mocker, homedir, config, session_maker, source
):
mock_gui = mocker.MagicMock()
controller = logic.Controller('http://localhost', mock_gui, session_maker, homedir)

file = factory.File(source=source['source'], is_downloaded=True)
controller.session.add(file)
controller.session.commit()

fw = FileWidget(file.uuid, controller, mocker.MagicMock(), mocker.MagicMock(), 0)

with mocker.patch(
"securedrop_client.gui.widgets.humanize_filesize",
side_effect=Exception("boom!")
):
fw.update_file_size()
assert fw.file_size.text() == ""


@pytest.mark.parametrize("key", [Qt.Key_Enter, Qt.Key_Return])
def test_ModalDialog_keyPressEvent_does_not_close_on_enter_or_return(mocker, key):
dialog = ModalDialog()
Expand Down
21 changes: 20 additions & 1 deletion tests/test_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
delete_single_submission_or_reply_on_disk, get_local_files, find_new_files, \
source_exists, set_message_or_reply_content, mark_as_downloaded, mark_as_decrypted, get_file, \
get_message, get_reply, update_and_get_user, update_missing_files, mark_as_not_downloaded, \
mark_all_pending_drafts_as_failed, delete_local_source_by_uuid, update_source_key
mark_all_pending_drafts_as_failed, delete_local_source_by_uuid, update_source_key, \
update_file_size

from securedrop_client import db
from tests import factory
Expand Down Expand Up @@ -1218,3 +1219,21 @@ def test_pending_replies_are_marked_as_failed_on_logout_login(mocker, session,

for draft in session.query(db.DraftReply).all():
assert draft.send_status == failed_status


def test_update_file_size(homedir, session):
source = factory.Source()
f = factory.File(source=source)
session.add(f)
session.commit()

real_size = 2112
data_dir = os.path.join(homedir, 'data')
file_location = f.location(data_dir)

os.makedirs(os.path.dirname(file_location), mode=0o700, exist_ok=True)
with open(file_location, mode='w') as f1:
f1.write("x" * real_size)
update_file_size(f.uuid, data_dir, session)

assert f.size == real_size