def _download(self, api: API, db_object: Union[File, Message, Reply], session: Session) -> str: ''' Download the encrypted file. Check file integrity and move it to the data directory before marking it as downloaded. Note: On Qubes OS, files are downloaded to /home/user/QubesIncoming/sd-proxy ''' try: etag, download_path = self.call_download_api(api, db_object) if not self._check_file_integrity(etag, download_path): exception = DownloadChecksumMismatchException( 'Downloaded file had an invalid checksum.', type(db_object), db_object.uuid) raise exception destination = db_object.location(self.data_dir) os.makedirs(os.path.dirname(destination), mode=0o700, exist_ok=True) shutil.move(download_path, destination) mark_as_downloaded(type(db_object), db_object.uuid, session) logger.info("File downloaded to {}".format(destination)) return destination except BaseError as e: logger.debug("Failed to download file: {}".format( db_object.filename)) raise e
def _download(self, api: API, db_object: Union[File, Message, Reply], session: Session) -> str: """ Download the encrypted file. Check file integrity and move it to the data directory before marking it as downloaded. Note: On Qubes OS, files are downloaded to /home/user/QubesIncoming/sd-proxy """ try: etag, download_path = self.call_download_api(api, db_object) if not self._check_file_integrity(etag, download_path): download_error = (session.query(DownloadError).filter_by( name=DownloadErrorCodes.CHECKSUM_ERROR.name).one()) db_object.download_error = download_error session.commit() exception = DownloadChecksumMismatchException( "Downloaded file had an invalid checksum.", type(db_object), db_object.uuid) raise exception destination = db_object.location(self.data_dir) os.makedirs(os.path.dirname(destination), mode=0o700, exist_ok=True) shutil.move(download_path, destination) db_object.download_error = None mark_as_downloaded(type(db_object), db_object.uuid, session) logger.info("File downloaded to {}".format(destination)) return destination except BaseError as e: raise e
def _download(self, api: API, db_object: Union[File, Message, Reply], session: Session) -> None: ''' Download the encrypted file. Check file integrity and move it to the data directory before marking it as downloaded. Note: On Qubes OS, files are downloaded to ~/QubesIncoming. ''' try: etag, download_path = self.call_download_api(api, db_object) if not self._check_file_integrity(etag, download_path): exception = DownloadChecksumMismatchException( 'Downloaded file had an invalid checksum.', type(db_object), db_object.uuid ) raise exception shutil.move(download_path, os.path.join(self.data_dir, db_object.filename)) mark_as_downloaded(type(db_object), db_object.uuid, session) logger.info("File downloaded: {}".format(db_object.filename)) except BaseError as e: logger.debug("Failed to download file: {}".format(db_object.filename)) raise e
def test_mark_reply_as_downloaded(mocker): session = mocker.MagicMock() reply = factory.Reply(source=factory.Source(), is_downloaded=False) session.query().filter_by().one.return_value = reply mark_as_downloaded(type(reply), "mock_uuid", session) assert reply.is_downloaded is True session.add.assert_called_once_with(reply) session.commit.assert_called_once_with()
def test_mark_message_as_downloaded(mocker): session = mocker.MagicMock() message = factory.Message(source=factory.Source(), is_downloaded=False) session.query().filter_by().one.return_value = message mark_as_downloaded(type(message), "mock_uuid", session) assert message.is_downloaded is True session.add.assert_called_once_with(message) session.commit.assert_called_once_with()
def test_mark_file_as_downloaded(mocker): session = mocker.MagicMock() file = factory.File(source=factory.Source(), is_downloaded=False) session.query().filter_by().one.return_value = file mark_as_downloaded(type(file), 'mock_uuid', session) assert file.is_downloaded is True session.add.assert_called_once_with(file) session.commit.assert_called_once_with()