diff options
author | Micah Lee <micah@micahflee.com> | 2018-11-18 17:04:20 -0800 |
---|---|---|
committer | Micah Lee <micah@micahflee.com> | 2018-11-18 17:04:20 -0800 |
commit | e29c0e7b69b7f9c5fcdf8489f97c8dfd8554311d (patch) | |
tree | 6be516838fd1c06546a2c69da8706a9547122f4a /onionshare | |
parent | 12180f08dc117ce1dbd72017b8af97cc77891c9a (diff) | |
parent | a39cd08083f4443e498611bbc1c086a05f5cffde (diff) | |
download | onionshare-e29c0e7b69b7f9c5fcdf8489f97c8dfd8554311d.tar.gz onionshare-e29c0e7b69b7f9c5fcdf8489f97c8dfd8554311d.zip |
Merge branch 'develop' into v3-revisited
Diffstat (limited to 'onionshare')
-rw-r--r-- | onionshare/__init__.py | 2 | ||||
-rw-r--r-- | onionshare/common.py | 27 | ||||
-rw-r--r-- | onionshare/web/receive_mode.py | 34 | ||||
-rw-r--r-- | onionshare/web/web.py | 6 |
4 files changed, 25 insertions, 44 deletions
diff --git a/onionshare/__init__.py b/onionshare/__init__.py index 069559c5..1e81333e 100644 --- a/onionshare/__init__.py +++ b/onionshare/__init__.py @@ -21,7 +21,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. import os, sys, time, argparse, threading from . import strings -from .common import Common, DownloadsDirErrorCannotCreate, DownloadsDirErrorNotWritable +from .common import Common from .web import Web from .onion import * from .onionshare import OnionShare diff --git a/onionshare/common.py b/onionshare/common.py index cab1e747..ffa6529f 100644 --- a/onionshare/common.py +++ b/onionshare/common.py @@ -32,20 +32,6 @@ import time from .settings import Settings -class DownloadsDirErrorCannotCreate(Exception): - """ - Error creating the downloads dir (~/OnionShare by default). - """ - pass - - -class DownloadsDirErrorNotWritable(Exception): - """ - Downloads dir is not writable. - """ - pass - - class Common(object): """ The Common object is shared amongst all parts of OnionShare. @@ -390,19 +376,6 @@ class Common(object): }""" } - def validate_downloads_dir(self): - """ - Validate that downloads_dir exists, and create it if it doesn't - """ - if not os.path.isdir(self.settings.get('downloads_dir')): - try: - os.mkdir(self.settings.get('downloads_dir'), 0o700) - except: - raise DownloadsDirErrorCannotCreate - - if not os.access(self.settings.get('downloads_dir'), os.W_OK): - raise DownloadsDirErrorNotWritable - @staticmethod def random_string(num_bytes, output_len=None): """ diff --git a/onionshare/web/receive_mode.py b/onionshare/web/receive_mode.py index 4a6934a1..3149029f 100644 --- a/onionshare/web/receive_mode.py +++ b/onionshare/web/receive_mode.py @@ -4,7 +4,6 @@ from datetime import datetime from flask import Request, request, render_template, make_response, flash, redirect from werkzeug.utils import secure_filename -from ..common import DownloadsDirErrorCannotCreate, DownloadsDirErrorNotWritable from .. import strings @@ -59,17 +58,19 @@ class ReceiveModeWeb(object): """ Upload files. """ - # Make sure downloads_dir exists + # Make sure the receive mode dir exists + now = datetime.now() + date_dir = now.strftime("%Y-%m-%d") + time_dir = now.strftime("%H.%M.%S") + receive_mode_dir = os.path.join(self.common.settings.get('downloads_dir'), date_dir, time_dir) valid = True try: - self.common.validate_downloads_dir() - except DownloadsDirErrorCannotCreate: - self.web.add_request(self.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.web.add_request(self.web.REQUEST_ERROR_DOWNLOADS_DIR_NOT_WRITABLE, request.path) - print(strings._('error_downloads_dir_not_writable').format(self.common.settings.get('downloads_dir'))) + os.makedirs(receive_mode_dir, 0o700) + except PermissionError: + self.web.add_request(self.web.REQUEST_ERROR_DOWNLOADS_DIR_CANNOT_CREATE, request.path, { + "receive_mode_dir": receive_mode_dir + }) + print(strings._('error_cannot_create_downloads_dir').format(receive_mode_dir)) valid = False if not valid: flash('Error uploading, please inform the OnionShare user', 'error') @@ -86,7 +87,7 @@ class ReceiveModeWeb(object): # Automatically rename the file, if a file of the same name already exists filename = secure_filename(f.filename) filenames.append(filename) - local_path = os.path.join(self.common.settings.get('downloads_dir'), filename) + local_path = os.path.join(receive_mode_dir, filename) if os.path.exists(local_path): if '.' in filename: # Add "-i", e.g. change "foo.txt" to "foo-2.txt" @@ -98,7 +99,7 @@ class ReceiveModeWeb(object): valid = False while not valid: new_filename = '{}-{}.{}'.format('.'.join(name), i, ext) - local_path = os.path.join(self.common.settings.get('downloads_dir'), new_filename) + local_path = os.path.join(receive_mode_dir, new_filename) if os.path.exists(local_path): i += 1 else: @@ -109,7 +110,7 @@ class ReceiveModeWeb(object): valid = False while not valid: new_filename = '{}-{}'.format(filename, i) - local_path = os.path.join(self.common.settings.get('downloads_dir'), new_filename) + local_path = os.path.join(receive_mode_dir, new_filename) if os.path.exists(local_path): i += 1 else: @@ -124,6 +125,13 @@ class ReceiveModeWeb(object): 'new_filename': basename }) + # Tell the GUI the receive mode directory for this file + self.web.add_request(self.web.REQUEST_UPLOAD_SET_DIR, request.path, { + 'id': request.upload_id, + 'filename': basename, + 'dir': receive_mode_dir + }) + self.common.log('ReceiveModeWeb', 'define_routes', '/upload, uploaded {}, saving to {}'.format(f.filename, local_path)) print(strings._('receive_mode_received_file').format(local_path)) f.save(local_path) diff --git a/onionshare/web/web.py b/onionshare/web/web.py index 52c4da16..a423b2e1 100644 --- a/onionshare/web/web.py +++ b/onionshare/web/web.py @@ -37,9 +37,9 @@ class Web(object): REQUEST_RATE_LIMIT = 5 REQUEST_CLOSE_SERVER = 6 REQUEST_UPLOAD_FILE_RENAMED = 7 - REQUEST_UPLOAD_FINISHED = 8 - REQUEST_ERROR_DOWNLOADS_DIR_CANNOT_CREATE = 9 - REQUEST_ERROR_DOWNLOADS_DIR_NOT_WRITABLE = 10 + REQUEST_UPLOAD_SET_DIR = 8 + REQUEST_UPLOAD_FINISHED = 9 + REQUEST_ERROR_DOWNLOADS_DIR_CANNOT_CREATE = 10 def __init__(self, common, is_gui, mode='share'): self.common = common |