From eb3d6f217164098762ff6cc2a7d4d42aad6c319a Mon Sep 17 00:00:00 2001 From: Micah Lee Date: Mon, 7 May 2018 23:07:11 -0700 Subject: [PATCH] Start making Web events actually put Upload objects into Uploads --- onionshare/web.py | 92 +++++++++++++++++-------- onionshare_gui/mode.py | 12 ++++ onionshare_gui/onionshare_gui.py | 6 ++ onionshare_gui/receive_mode/__init__.py | 30 ++++++++ onionshare_gui/receive_mode/uploads.py | 86 ++++++----------------- share/locale/en.json | 8 ++- 6 files changed, 142 insertions(+), 92 deletions(-) diff --git a/onionshare/web.py b/onionshare/web.py index 8fe46bcc..63a0fcb5 100644 --- a/onionshare/web.py +++ b/onionshare/web.py @@ -51,6 +51,8 @@ class Web(object): REQUEST_CANCELED = 4 REQUEST_RATE_LIMIT = 5 REQUEST_CLOSE_SERVER = 6 + REQUEST_UPLOAD_NEW_FILE_STARTED = 7 + REQUEST_UPLOAD_FILE_RENAMED = 8 def __init__(self, common, gui_mode, receive_mode=False): self.common = common @@ -104,6 +106,7 @@ class Web(object): self.download_count = 0 self.upload_count = 0 + self.error404_count = 0 # If "Stop After First Download" is checked (stay_open == False), only allow @@ -141,7 +144,7 @@ class Web(object): """ self.check_slug_candidate(slug_candidate) - self.add_request(self.REQUEST_LOAD, request.path) + self.add_request(Web.REQUEST_LOAD, request.path) # Deny new downloads if "Stop After First Download" is checked and there is # currently a download @@ -184,7 +187,9 @@ class Web(object): path = request.path # Tell GUI the download started - self.add_request(self.REQUEST_STARTED, path, {'id': download_id}) + self.add_request(Web.REQUEST_STARTED, path, { + 'id': download_id} + ) dirname = os.path.dirname(self.zip_filename) basename = os.path.basename(self.zip_filename) @@ -205,7 +210,9 @@ class Web(object): while not self.done: # The user has canceled the download, so stop serving the file if self.client_cancel: - self.add_request(self.REQUEST_CANCELED, path, {'id': download_id}) + self.add_request(Web.REQUEST_CANCELED, path, { + 'id': download_id + }) break chunk = fp.read(chunk_size) @@ -225,7 +232,10 @@ class Web(object): "\r{0:s}, {1:.2f}% ".format(self.common.human_readable_filesize(downloaded_bytes), percent)) sys.stdout.flush() - self.add_request(self.REQUEST_PROGRESS, path, {'id': download_id, 'bytes': downloaded_bytes}) + self.add_request(Web.REQUEST_PROGRESS, path, { + 'id': download_id, + 'bytes': downloaded_bytes + }) self.done = False except: # looks like the download was canceled @@ -233,7 +243,9 @@ class Web(object): canceled = True # tell the GUI the download has canceled - self.add_request(self.REQUEST_CANCELED, path, {'id': download_id}) + self.add_request(Web.REQUEST_CANCELED, path, { + 'id': download_id + }) fp.close() @@ -270,7 +282,7 @@ class Web(object): The web app routes for receiving files """ def index_logic(): - self.add_request(self.REQUEST_LOAD, request.path) + self.add_request(Web.REQUEST_LOAD, request.path) r = make_response(render_template( 'receive.html', @@ -330,6 +342,15 @@ class Web(object): else: valid = True + basename = os.path.basename(local_path) + if f.filename != basename: + # Tell the GUI that the file has changed names + self.add_request(Web.REQUEST_UPLOAD_FILE_RENAMED, request.path, { + 'id': request.upload_id, + 'old_filename': f.filename, + 'new_filename': basename + }) + self.common.log('Web', 'receive_routes', '/upload, uploaded {}, saving to {}'.format(f.filename, local_path)) print(strings._('receive_mode_received_file').format(local_path)) f.save(local_path) @@ -360,7 +381,7 @@ class Web(object): if self.common.settings.get('receive_allow_receiver_shutdown'): self.force_shutdown() r = make_response(render_template('closed.html')) - self.add_request(self.REQUEST_CLOSE_SERVER, request.path) + self.add_request(Web.REQUEST_CLOSE_SERVER, request.path) return self.add_security_headers(r) else: return redirect('/{}'.format(slug_candidate)) @@ -397,14 +418,14 @@ class Web(object): return "" def error404(self): - self.add_request(self.REQUEST_OTHER, request.path) + self.add_request(Web.REQUEST_OTHER, request.path) if request.path != '/favicon.ico': self.error404_count += 1 # In receive mode, with public mode enabled, skip rate limiting 404s if not (self.receive_mode and self.common.settings.get('receive_public_mode')): if self.error404_count == 20: - self.add_request(self.REQUEST_RATE_LIMIT, request.path) + self.add_request(Web.REQUEST_RATE_LIMIT, request.path) self.force_shutdown() print(strings._('error_rate_limit')) @@ -610,6 +631,7 @@ class ReceiveModeWSGIMiddleware(object): environ['web'] = self.web return self.app(environ, start_response) + class ReceiveModeTemporaryFile(object): """ A custom TemporaryFile that tells ReceiveModeRequest every time data gets @@ -649,29 +671,45 @@ class ReceiveModeRequest(Request): self.web = environ['web'] # A dictionary that maps filenames to the bytes uploaded so far - self.onionshare_progress = {} + self.progress = {} + + # Is this a valid upload request? + self.upload_request = False + if self.method == 'POST': + if self.web.common.settings.get('receive_public_mode'): + if self.path == '/upload': + self.upload_request = True + else: + if self.path == '/{}/upload'.format(self.web.slug): + self.upload_request = True + + # If this is an upload request, create an upload_id (attach it to the request) + self.upload_id = self.web.upload_count + self.web.upload_count += 1 + + # Tell the GUI + self.web.add_request(Web.REQUEST_STARTED, self.path, { + 'id': self.upload_id + }) def _get_file_stream(self, total_content_length, content_type, filename=None, content_length=None): """ This gets called for each file that gets uploaded, and returns an file-like writable stream. """ - # Each upload has a unique id. Now that the upload is starting, attach its - # upload_id to the request - self.upload_id = self.web.upload_count - self.web.upload_count += 1 - - # Tell GUI the upload started - self.web.add_request(self.web.REQUEST_STARTED, self.path, { - 'id': self.upload_id + # Tell the GUI about the new file upload + self.web.add_request(Web.REQUEST_UPLOAD_NEW_FILE_STARTED, self.path, { + 'id': self.upload_id, + 'filename': filename, + 'total_bytes': total_content_length }) - self.onionshare_progress[filename] = { + self.progress[filename] = { 'total_bytes': total_content_length, 'uploaded_bytes': 0 } - if len(self.onionshare_progress) > 0: + if len(self.progress) > 0: print('') return ReceiveModeTemporaryFile(filename, self.onionshare_update_func) @@ -681,20 +719,20 @@ class ReceiveModeRequest(Request): When closing the request, print a newline if this was a file upload. """ super(ReceiveModeRequest, self).close() - if len(self.onionshare_progress) > 0: + if len(self.progress) > 0: print('') def onionshare_update_func(self, filename, length): """ Keep track of the bytes uploaded so far for all files. """ - self.onionshare_progress[filename]['uploaded_bytes'] += length - uploaded = self.web.common.human_readable_filesize(self.onionshare_progress[filename]['uploaded_bytes']) - total = self.web.common.human_readable_filesize(self.onionshare_progress[filename]['total_bytes']) + self.progress[filename]['uploaded_bytes'] += length + uploaded = self.web.common.human_readable_filesize(self.progress[filename]['uploaded_bytes']) + total = self.web.common.human_readable_filesize(self.progress[filename]['total_bytes']) print('{}/{} - {} '.format(uploaded, total, filename), end='\r') - # Update the GUI on the download progress - self.web.add_request(self.web.REQUEST_PROGRESS, self.path, { + # Update the GUI on the upload progress + self.web.add_request(Web.REQUEST_PROGRESS, self.path, { 'id': self.upload_id, - 'bytes': self.onionshare_progress[filename]['uploaded_bytes'] + 'progress': self.progress }) diff --git a/onionshare_gui/mode.py b/onionshare_gui/mode.py index 3cfa4b0c..edcedca3 100644 --- a/onionshare_gui/mode.py +++ b/onionshare_gui/mode.py @@ -323,3 +323,15 @@ class Mode(QtWidgets.QWidget): Handle REQUEST_CLOSE_SERVER event. """ pass + + def handle_request_upload_new_file_started(self, event): + """ + Handle REQUEST_UPLOAD_NEW_FILE_STARTED event. + """ + pass + + def handle_request_upload_file_renamed(self, event): + """ + Handle REQUEST_UPLOAD_FILE_RENAMED event. + """ + pass diff --git a/onionshare_gui/onionshare_gui.py b/onionshare_gui/onionshare_gui.py index 9e9d8583..893d2dae 100644 --- a/onionshare_gui/onionshare_gui.py +++ b/onionshare_gui/onionshare_gui.py @@ -387,6 +387,12 @@ class OnionShareGui(QtWidgets.QMainWindow): elif event["type"] == Web.REQUEST_CLOSE_SERVER: mode.handle_request_close_server(event) + elif event["type"] == Web.REQUEST_UPLOAD_NEW_FILE_STARTED: + mode.handle_request_upload_new_file_started(event) + + elif event["type"] == Web.REQUEST_UPLOAD_FILE_RENAMED: + mode.handle_request_upload_file_renamed(event) + 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"])) diff --git a/onionshare_gui/receive_mode/__init__.py b/onionshare_gui/receive_mode/__init__.py index 6fd0031c..000850d2 100644 --- a/onionshare_gui/receive_mode/__init__.py +++ b/onionshare_gui/receive_mode/__init__.py @@ -131,6 +131,24 @@ class ReceiveMode(Mode): """ self.system_tray.showMessage(strings._('systray_page_loaded_title', True), strings._('systray_upload_page_loaded_message', True)) + def handle_request_started(self, event): + """ + Handle REQUEST_STARTED event. + """ + self.uploads.add(event["data"]["id"]) + self.uploads_in_progress += 1 + self.update_uploads_in_progress() + + self.system_tray.showMessage(strings._('systray_upload_started_title', True), strings._('systray_upload_started_message', True)) + + def handle_request_progress(self, event): + """ + Handle REQUEST_PROGRESS event. + """ + self.uploads.update(event["data"]["id"], event["data"]["progress"]) + + # TODO: not done yet + def handle_request_close_server(self, event): """ Handle REQUEST_CLOSE_SERVER event. @@ -138,6 +156,18 @@ class ReceiveMode(Mode): self.stop_server() self.system_tray.showMessage(strings._('systray_close_server_title', True), strings._('systray_close_server_message', True)) + def handle_request_upload_new_file_started(self, event): + """ + Handle REQUEST_UPLOAD_NEW_FILE_STARTED event. + """ + pass + + def handle_request_upload_file_renamed(self, event): + """ + Handle REQUEST_UPLOAD_FILE_RENAMED event. + """ + pass + def reset_info_counters(self): """ Set the info counters back to zero. diff --git a/onionshare_gui/receive_mode/uploads.py b/onionshare_gui/receive_mode/uploads.py index 8d4712a3..a36f2364 100644 --- a/onionshare_gui/receive_mode/uploads.py +++ b/onionshare_gui/receive_mode/uploads.py @@ -17,75 +17,33 @@ GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . """ +from datetime import datetime from PyQt5 import QtCore, QtWidgets, QtGui from onionshare import strings -class Upload(object): - def __init__(self, common, upload_id, total_bytes): +class Upload(QtWidgets.QGroupBox): + def __init__(self, common, upload_id): + super(Upload, self).__init__() self.common = common self.upload_id = upload_id - self.started = time.time() - self.total_bytes = total_bytes + self.started = datetime.now() self.uploaded_bytes = 0 - # Uploads have two modes, in progress and finished. In progess, they display - # the progress bar. When finished, they display info about the files that - # were uploaded. - - # Progress bar - self.progress_bar = QtWidgets.QProgressBar() - self.progress_bar.setTextVisible(True) - self.progress_bar.setAttribute(QtCore.Qt.WA_DeleteOnClose) - self.progress_bar.setAlignment(QtCore.Qt.AlignHCenter) - self.progress_bar.setMinimum(0) - self.progress_bar.setMaximum(total_bytes) - self.progress_bar.setValue(0) - self.progress_bar.setStyleSheet(self.common.css['downloads_uploads_progress_bar']) - self.progress_bar.total_bytes = total_bytes - - # Finished - self.finished = QtWidgets.QGroupBox() - self.finished.hide() + # Set the title of the title of the group box based on the start time + self.setTitle(strings._('gui_upload_in_progress', True).format(self.started.strftime("%b %m, %I:%M%p"))) # Start at 0 - self.update(0) + self.update({}) - def update(self, uploaded_bytes): - self.uploaded_bytes = uploaded_bytes - - self.progress_bar.setValue(uploaded_bytes) - if uploaded_bytes == self.progress_bar.uploaded_bytes: - # Upload is finished, hide the progress bar and show the finished widget - self.progress_bar.hide() - - # TODO: add file information to the finished widget - ended = time.time() - elapsed = ended - self.started - self.finished.show() - - else: - elapsed = time.time() - self.started - if elapsed < 10: - pb_fmt = strings._('gui_download_upload_progress_starting').format( - self.common.human_readable_filesize(downloaded_bytes)) - else: - pb_fmt = strings._('gui_download_upload_progress_eta').format( - self.common.human_readable_filesize(downloaded_bytes), - self.estimated_time_remaining) - - self.progress_bar.setFormat(pb_fmt) - - def cancel(self): - self.progress_bar.setFormat(strings._('gui_canceled')) - - @property - def estimated_time_remaining(self): - return self.common.estimated_time_remaining(self.uploaded_bytes, - self.total_bytes, - self.started) + def update(self, progress): + """ + Using the progress from Web, make sure all the file progress bars exist, + and update their progress + """ + pass class Uploads(QtWidgets.QScrollArea): @@ -123,17 +81,17 @@ class Uploads(QtWidgets.QScrollArea): widget.setLayout(layout) self.setWidget(widget) - def add(self, upload_id, total_bytes): + def add(self, upload_id): """ - Add a new upload progress bar. + Add a new upload. """ # Hide the no_uploads_label self.no_uploads_label.hide() # Add it to the list - uploads = Upload(self.common, upload_id, total_bytes) - self.uploads[upload_id] = download - self.uploads_layout.addWidget(upload.progress_bar) + upload = Upload(self.common, upload_id) + self.uploads[upload_id] = upload + self.uploads_layout.addWidget(upload) # Scroll to the bottom self.vbar.setValue(self.vbar.maximum()) @@ -142,7 +100,8 @@ class Uploads(QtWidgets.QScrollArea): """ Update the progress of an upload progress bar. """ - self.uploads[upload_id].update(uploaded_bytes) + pass + #self.uploads[upload_id].update(uploaded_bytes) def cancel(self, upload_id): """ @@ -155,8 +114,7 @@ class Uploads(QtWidgets.QScrollArea): Reset the uploads back to zero """ for upload in self.uploads.values(): - self.uploads_layout.removeWidget(upload.progress_bar) - upload.progress_bar.close() + self.uploads_layout.removeWidget(upload) self.uploads = {} self.no_uploads_label.show() diff --git a/share/locale/en.json b/share/locale/en.json index 40f30566..4b8c2c04 100644 --- a/share/locale/en.json +++ b/share/locale/en.json @@ -28,6 +28,10 @@ "systray_download_completed_message": "The user finished downloading your files", "systray_download_canceled_title": "OnionShare Download Canceled", "systray_download_canceled_message": "The user canceled the download", + "systray_upload_started_title": "OnionShare Upload Started", + "systray_upload_started_message": "A user started uploading files to your computer", + "systray_upload_completed_title": "OnionShare Upload Finished", + "systray_upload_completed_message": "The user finished uploading files to your computer", "help_local_only": "Do not attempt to use Tor: For development only", "help_stay_open": "Keep onion service running after download has finished", "help_shutdown_timeout": "Shut down the onion service after N seconds", @@ -186,5 +190,7 @@ "systray_upload_page_loaded_message": "A user loaded the upload page", "gui_uploads": "Upload History", "gui_uploads_window_tooltip": "Show/hide uploads", - "gui_no_uploads": "No uploads yet." + "gui_no_uploads": "No uploads yet.", + "gui_upload_in_progress": "Upload in progress, started {}", + "gui_upload_finished": "Uploaded {} to {}" }