From d267cc597dfc7c111c1c8e18f5957548eb4eb351 Mon Sep 17 00:00:00 2001 From: Miguel Jacq Date: Mon, 1 Oct 2018 18:42:53 +1000 Subject: [PATCH] Fix logic for handling an upload still in progress when timer runs out. Show thankyou page for last uploader post-timer expiry --- onionshare/web/receive_mode.py | 74 ++++++++++++------- onionshare_gui/receive_mode/__init__.py | 8 +- .../templates/{closed.html => thankyou.html} | 0 3 files changed, 53 insertions(+), 29 deletions(-) rename share/templates/{closed.html => thankyou.html} (100%) diff --git a/onionshare/web/receive_mode.py b/onionshare/web/receive_mode.py index 73762573..5035f68a 100644 --- a/onionshare/web/receive_mode.py +++ b/onionshare/web/receive_mode.py @@ -19,6 +19,7 @@ class ReceiveModeWeb(object): self.web = web self.can_upload = True + self.can_stop_share_now = False self.upload_count = 0 self.uploads_in_progress = [] @@ -39,6 +40,7 @@ class ReceiveModeWeb(object): r = make_response(render_template( 'receive.html', upload_action=upload_action)) + return self.web.add_security_headers(r) @self.web.app.route("/") @@ -138,10 +140,24 @@ class ReceiveModeWeb(object): for filename in filenames: flash('Sent {}'.format(filename), 'info') - if self.common.settings.get('public_mode'): - return redirect('/') + if self.can_upload: + if self.common.settings.get('public_mode'): + path = '/' + else: + path = '/{}'.format(slug_candidate) + + return redirect('{}'.format(path)) else: - return redirect('/{}'.format(slug_candidate)) + # It was the last upload and the timer ran out + if self.common.settings.get('public_mode'): + return thankyou_logic(slug_candidate) + else: + return thankyou_logic() + + def thankyou_logic(slug_candidate=''): + r = make_response(render_template( + 'thankyou.html')) + return self.web.add_security_headers(r) @self.web.app.route("//upload", methods=['POST']) def upload(slug_candidate): @@ -231,39 +247,36 @@ class ReceiveModeRequest(Request): if self.path == '/upload': self.upload_request = True - if self.upload_request: + if self.upload_request and self.web.receive_mode.can_upload: # A dictionary that maps filenames to the bytes uploaded so far self.progress = {} # Create an upload_id, attach it to the request self.upload_id = self.web.receive_mode.upload_count - if self.web.receive_mode.can_upload: - self.web.receive_mode.upload_count += 1 + self.web.receive_mode.upload_count += 1 - # Figure out the content length - try: - self.content_length = int(self.headers['Content-Length']) - except: - self.content_length = 0 + # Figure out the content length + try: + self.content_length = int(self.headers['Content-Length']) + except: + self.content_length = 0 - print("{}: {}".format( - datetime.now().strftime("%b %d, %I:%M%p"), - strings._("receive_mode_upload_starting").format(self.web.common.human_readable_filesize(self.content_length)) - )) + print("{}: {}".format( + datetime.now().strftime("%b %d, %I:%M%p"), + strings._("receive_mode_upload_starting").format(self.web.common.human_readable_filesize(self.content_length)) + )) - # append to self.uploads_in_progress - self.web.receive_mode.uploads_in_progress.append(self.upload_id) + # append to self.uploads_in_progress + self.web.receive_mode.uploads_in_progress.append(self.upload_id) - # Tell the GUI - self.web.add_request(self.web.REQUEST_STARTED, self.path, { - 'id': self.upload_id, - 'content_length': self.content_length - }) + # Tell the GUI + self.web.add_request(self.web.REQUEST_STARTED, self.path, { + 'id': self.upload_id, + 'content_length': self.content_length + }) - self.previous_file = None - else: - self.upload_rejected = True + self.previous_file = None def _get_file_stream(self, total_content_length, content_type, filename=None, content_length=None): """ @@ -284,14 +297,19 @@ class ReceiveModeRequest(Request): """ super(ReceiveModeRequest, self).close() if self.upload_request: - if not self.upload_rejected: + try: + upload_id = self.upload_id # Inform the GUI that the upload has finished self.web.add_request(self.web.REQUEST_UPLOAD_FINISHED, self.path, { - 'id': self.upload_id + 'id': upload_id }) # remove from self.uploads_in_progress - self.web.receive_mode.uploads_in_progress.remove(self.upload_id) + self.web.receive_mode.uploads_in_progress.remove(upload_id) + + except AttributeError: + # We may not have got an upload_id (e.g uploads were rejected) + pass def file_write_func(self, filename, length): """ diff --git a/onionshare_gui/receive_mode/__init__.py b/onionshare_gui/receive_mode/__init__.py index faa65ffe..c10622e4 100644 --- a/onionshare_gui/receive_mode/__init__.py +++ b/onionshare_gui/receive_mode/__init__.py @@ -51,6 +51,7 @@ class ReceiveMode(Mode): self.uploads_in_progress = 0 self.uploads_completed = 0 self.new_upload = False # For scrolling to the bottom of the uploads list + self.can_stop_server = False # for communicating to the auto-stop timer # Information about share, and show uploads button self.info_in_progress_uploads_count = QtWidgets.QLabel() @@ -93,7 +94,7 @@ class ReceiveMode(Mode): The shutdown timer expired, should we stop the server? Returns a bool """ # If there were no attempts to upload files, or all uploads are done, we can stop - if self.web.receive_mode.upload_count == 0 or not self.web.receive_mode.uploads_in_progress: + if self.web.receive_mode.upload_count == 0 or self.can_stop_server: self.server_status.stop_server() self.server_status_label.setText(strings._('close_on_timeout', True)) return True @@ -110,7 +111,9 @@ class ReceiveMode(Mode): Starting the server. """ # Reset web counters + self.can_stop_server = False self.web.receive_mode.upload_count = 0 + self.web.receive_mode.can_upload = True self.web.error404_count = 0 # Hide and reset the uploads if we have previously shared @@ -144,6 +147,7 @@ class ReceiveMode(Mode): self.uploads.add(event["data"]["id"], event["data"]["content_length"]) self.uploads_in_progress += 1 self.update_uploads_in_progress() + self.can_stop_server = False self.system_tray.showMessage(strings._('systray_upload_started_title', True), strings._('systray_upload_started_message', True)) @@ -177,6 +181,8 @@ class ReceiveMode(Mode): # Update the 'in progress uploads' info self.uploads_in_progress -= 1 self.update_uploads_in_progress() + if self.uploads_in_progress == 0: + self.can_stop_server = True def on_reload_settings(self): """ diff --git a/share/templates/closed.html b/share/templates/thankyou.html similarity index 100% rename from share/templates/closed.html rename to share/templates/thankyou.html