diff options
author | Micah Lee <micah@micahflee.com> | 2018-12-05 09:13:54 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-12-05 09:13:54 -0800 |
commit | c7ef1bba39c07ab23e6b745392fb1f0c84e959cf (patch) | |
tree | 91e66286287f91c9298ee7642a0208e1fab364e8 /onionshare | |
parent | ad7a858ab07f793eaa76abfbcd1d22490f894e66 (diff) | |
parent | d15e00061a4488aa3ac513671a99ce1213f90a51 (diff) | |
download | onionshare-c7ef1bba39c07ab23e6b745392fb1f0c84e959cf.tar.gz onionshare-c7ef1bba39c07ab23e6b745392fb1f0c84e959cf.zip |
Merge pull request #752 from mig5/receiver-mode-gui-hold-timeout-share-open-til-upload-finished
Hold a share open if its timer has expired but a file is still uploading.
Diffstat (limited to 'onionshare')
-rw-r--r-- | onionshare/__init__.py | 7 | ||||
-rw-r--r-- | onionshare/settings.py | 1 | ||||
-rw-r--r-- | onionshare/web/receive_mode.py | 106 | ||||
-rw-r--r-- | onionshare/web/web.py | 6 |
4 files changed, 71 insertions, 49 deletions
diff --git a/onionshare/__init__.py b/onionshare/__init__.py index 1e81333e..0d064639 100644 --- a/onionshare/__init__.py +++ b/onionshare/__init__.py @@ -209,6 +209,13 @@ def main(cwd=None): print(strings._("close_on_timeout")) web.stop(app.port) break + if mode == 'receive': + if web.receive_mode.upload_count == 0 or not web.receive_mode.uploads_in_progress: + print(strings._("close_on_timeout")) + web.stop(app.port) + break + else: + web.receive_mode.can_upload = False # Allow KeyboardInterrupt exception to be handled with threads # https://stackoverflow.com/questions/3788208/python-threading-ignores-keyboardinterrupt-exception time.sleep(0.2) diff --git a/onionshare/settings.py b/onionshare/settings.py index 42f7259c..02f644af 100644 --- a/onionshare/settings.py +++ b/onionshare/settings.py @@ -94,7 +94,6 @@ class Settings(object): 'slug': '', 'hidservauth_string': '', 'downloads_dir': self.build_default_downloads_dir(), - 'receive_allow_receiver_shutdown': True, 'locale': None # this gets defined in fill_in_defaults() } self._settings = {} diff --git a/onionshare/web/receive_mode.py b/onionshare/web/receive_mode.py index 3149029f..6985f38a 100644 --- a/onionshare/web/receive_mode.py +++ b/onionshare/web/receive_mode.py @@ -17,7 +17,9 @@ class ReceiveModeWeb(object): self.web = web + self.can_upload = True self.upload_count = 0 + self.uploads_in_progress = [] self.define_routes() @@ -30,25 +32,25 @@ class ReceiveModeWeb(object): if self.common.settings.get('public_mode'): upload_action = '/upload' - close_action = '/close' else: upload_action = '/{}/upload'.format(self.web.slug) - close_action = '/{}/close'.format(self.web.slug) r = make_response(render_template( 'receive.html', - upload_action=upload_action, - close_action=close_action, - receive_allow_receiver_shutdown=self.common.settings.get('receive_allow_receiver_shutdown'))) + upload_action=upload_action)) return self.web.add_security_headers(r) @self.web.app.route("/<slug_candidate>") def index(slug_candidate): + if not self.can_upload: + return self.web.error403() self.web.check_slug_candidate(slug_candidate) return index_logic() @self.web.app.route("/") def index_public(): + if not self.can_upload: + return self.web.error403() if not self.common.settings.get('public_mode'): return self.web.error404() return index_logic() @@ -144,43 +146,41 @@ 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("/<slug_candidate>/upload", methods=['POST']) def upload(slug_candidate): + if not self.can_upload: + return self.web.error403() self.web.check_slug_candidate(slug_candidate) return upload_logic(slug_candidate) @self.web.app.route("/upload", methods=['POST']) def upload_public(): + if not self.can_upload: + return self.web.error403() if not self.common.settings.get('public_mode'): return self.web.error404() return upload_logic() - def close_logic(slug_candidate=''): - if self.common.settings.get('receive_allow_receiver_shutdown'): - self.web.force_shutdown() - r = make_response(render_template('closed.html')) - self.web.add_request(self.web.REQUEST_CLOSE_SERVER, request.path) - return self.web.add_security_headers(r) - else: - return redirect('/{}'.format(slug_candidate)) - - @self.web.app.route("/<slug_candidate>/close", methods=['POST']) - def close(slug_candidate): - self.web.check_slug_candidate(slug_candidate) - return close_logic(slug_candidate) - - @self.web.app.route("/close", methods=['POST']) - def close_public(): - if not self.common.settings.get('public_mode'): - return self.web.error404() - return close_logic() - class ReceiveModeWSGIMiddleware(object): """ @@ -256,28 +256,34 @@ class ReceiveModeRequest(Request): # 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 - self.web.receive_mode.upload_count += 1 + # Prevent new uploads if we've said so (timer expired) + if self.web.receive_mode.can_upload: - # Figure out the content length - try: - self.content_length = int(self.headers['Content-Length']) - except: - self.content_length = 0 + # Create an upload_id, attach it to the request + self.upload_id = self.web.receive_mode.upload_count - 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)) - )) + self.web.receive_mode.upload_count += 1 - # Tell the GUI - self.web.add_request(self.web.REQUEST_STARTED, self.path, { - 'id': self.upload_id, - 'content_length': self.content_length - }) + # 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)) + )) + + # Tell the GUI + self.web.add_request(self.web.REQUEST_STARTED, self.path, { + 'id': self.upload_id, + 'content_length': self.content_length + }) + + self.web.receive_mode.uploads_in_progress.append(self.upload_id) - self.previous_file = None + self.previous_file = None def _get_file_stream(self, total_content_length, content_type, filename=None, content_length=None): """ @@ -297,11 +303,15 @@ class ReceiveModeRequest(Request): Closing the request. """ super(ReceiveModeRequest, self).close() - if self.upload_request: + 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 }) + self.web.receive_mode.uploads_in_progress.remove(upload_id) + except AttributeError: + pass def file_write_func(self, filename, length): """ diff --git a/onionshare/web/web.py b/onionshare/web/web.py index a423b2e1..5ecbad27 100644 --- a/onionshare/web/web.py +++ b/onionshare/web/web.py @@ -142,6 +142,12 @@ class Web(object): r = make_response(render_template('404.html'), 404) return self.add_security_headers(r) + def error403(self): + self.add_request(Web.REQUEST_OTHER, request.path) + + r = make_response(render_template('403.html'), 403) + return self.add_security_headers(r) + def add_security_headers(self, r): """ Add security headers to a request |