Receive mode puts files in a directory based on the timestamp of the upload

This commit is contained in:
Micah Lee 2018-10-25 21:13:16 -07:00
parent 8f40553e09
commit 2c45f6851e
6 changed files with 16 additions and 47 deletions

View file

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

View file

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

View file

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

View file

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