diff options
author | Micah Lee <micah@micahflee.com> | 2018-10-25 21:13:16 -0700 |
---|---|---|
committer | Micah Lee <micah@micahflee.com> | 2018-10-25 21:13:16 -0700 |
commit | 6233487ecdaed7c4a678a01889703bfd0dec0787 (patch) | |
tree | d9aeebd8eabc5908e60a69560757cb19a66fcec0 /onionshare | |
parent | 23a1ed9165324340024544679ac21e92d277c2e1 (diff) | |
download | onionshare-6233487ecdaed7c4a678a01889703bfd0dec0787.tar.gz onionshare-6233487ecdaed7c4a678a01889703bfd0dec0787.zip |
Receive mode puts files in a directory based on the timestamp of the upload
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 | 27 | ||||
-rw-r--r-- | onionshare/web/web.py | 1 |
4 files changed, 15 insertions, 42 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..66e00240 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: diff --git a/onionshare/web/web.py b/onionshare/web/web.py index 52c4da16..2ae011b7 100644 --- a/onionshare/web/web.py +++ b/onionshare/web/web.py @@ -39,7 +39,6 @@ class Web(object): REQUEST_UPLOAD_FILE_RENAMED = 7 REQUEST_UPLOAD_FINISHED = 8 REQUEST_ERROR_DOWNLOADS_DIR_CANNOT_CREATE = 9 - REQUEST_ERROR_DOWNLOADS_DIR_NOT_WRITABLE = 10 def __init__(self, common, is_gui, mode='share'): self.common = common |