From db7d5a65522a61df980347e5a9f4d3bbf6fdc4a9 Mon Sep 17 00:00:00 2001 From: Micah Lee Date: Sat, 19 May 2018 21:11:57 -0700 Subject: [PATCH] Move downloads_dir validation into the /upload request in Web, and display an error in both CLI and GUI --- onionshare/__init__.py | 17 ----------------- onionshare/web.py | 19 +++++++++++++++++++ onionshare_gui/onionshare_gui.py | 6 ++++++ 3 files changed, 25 insertions(+), 17 deletions(-) diff --git a/onionshare/__init__.py b/onionshare/__init__.py index 52226b48..1cebc4e3 100644 --- a/onionshare/__init__.py +++ b/onionshare/__init__.py @@ -89,23 +89,6 @@ def main(cwd=None): # Debug mode? common.debug = debug - # In receive mode, validate downloads dir - if receive: - valid = True - try: - common.validate_downloads_dir() - - except DownloadsDirErrorCannotCreate: - print(strings._('error_cannot_create_downloads_dir').format(common.settings.get('downloads_dir'))) - valid = False - - except DownloadsDirErrorNotWritable: - print(strings._('error_downloads_dir_not_writable').format(common.settings.get('downloads_dir'))) - valid = False - - if not valid: - sys.exit() - # Create the Web object web = Web(common, False, receive) diff --git a/onionshare/web.py b/onionshare/web.py index 282ddd81..3d1279ce 100644 --- a/onionshare/web.py +++ b/onionshare/web.py @@ -39,6 +39,7 @@ from flask import ( from werkzeug.utils import secure_filename from . import strings +from .common import DownloadsDirErrorCannotCreate, DownloadsDirErrorNotWritable class Web(object): """ @@ -53,6 +54,8 @@ class Web(object): REQUEST_CLOSE_SERVER = 6 REQUEST_UPLOAD_NEW_FILE_STARTED = 7 REQUEST_UPLOAD_FILE_RENAMED = 8 + REQUEST_ERROR_DOWNLOADS_DIR_CANNOT_CREATE = 9 + REQUEST_ERROR_DOWNLOADS_DIR_NOT_WRITABLE = 10 def __init__(self, common, gui_mode, receive_mode=False): self.common = common @@ -306,6 +309,22 @@ class Web(object): """ Upload files. """ + # Make sure downloads_dir exists + valid = True + try: + self.common.validate_downloads_dir() + except DownloadsDirErrorCannotCreate: + self.add_request(Web.REQUEST_ERROR_DOWNLOADS_DIR_CANNOT_CREATE, request.path) + print(strings._('error_cannot_create_downloads_dir').format(self.common.settings.get('downloads_dir'))) + valid = False + except DownloadsDirErrorNotWritable: + self.add_request(Web.REQUEST_ERROR_DOWNLOADS_DIR_NOT_WRITABLE, request.path) + print(strings._('error_downloads_dir_not_writable').format(self.common.settings.get('downloads_dir'))) + valid = False + if not valid: + flash('Error uploading, please inform the OnionShare user') + return redirect('/{}'.format(slug_candidate)) + files = request.files.getlist('file[]') filenames = [] for f in files: diff --git a/onionshare_gui/onionshare_gui.py b/onionshare_gui/onionshare_gui.py index 893d2dae..10fa62b6 100644 --- a/onionshare_gui/onionshare_gui.py +++ b/onionshare_gui/onionshare_gui.py @@ -393,6 +393,12 @@ class OnionShareGui(QtWidgets.QMainWindow): elif event["type"] == Web.REQUEST_UPLOAD_FILE_RENAMED: mode.handle_request_upload_file_renamed(event) + if event["type"] == Web.REQUEST_ERROR_DOWNLOADS_DIR_CANNOT_CREATE: + Alert(self.common, strings._('error_cannot_create_downloads_dir').format(self.common.settings.get('downloads_dir'))) + + if event["type"] == Web.REQUEST_ERROR_DOWNLOADS_DIR_NOT_WRITABLE: + Alert(self.common, strings._('error_downloads_dir_not_writable').format(self.common.settings.get('downloads_dir'))) + elif event["path"] != '/favicon.ico': self.status_bar.showMessage('[#{0:d}] {1:s}: {2:s}'.format(mode.web.error404_count, strings._('other_page_loaded', True), event["path"]))