Move downloads_dir validation into the /upload request in Web, and display an error in both CLI and GUI

This commit is contained in:
Micah Lee 2018-05-19 21:11:57 -07:00
parent c23ab77a58
commit db7d5a6552
No known key found for this signature in database
GPG Key ID: 403C2657CD994F73
3 changed files with 25 additions and 17 deletions

View File

@ -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)

View File

@ -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:

View File

@ -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"]))