Fix logic for handling an upload still in progress when timer runs out. Show thankyou page for last uploader post-timer expiry

This commit is contained in:
Miguel Jacq 2018-10-01 18:42:53 +10:00
parent f653e8cc04
commit 3f32db2cca
No known key found for this signature in database
GPG Key ID: EEA4341C6D97A0B6
3 changed files with 53 additions and 29 deletions

View File

@ -19,6 +19,7 @@ class ReceiveModeWeb(object):
self.web = web self.web = web
self.can_upload = True self.can_upload = True
self.can_stop_share_now = False
self.upload_count = 0 self.upload_count = 0
self.uploads_in_progress = [] self.uploads_in_progress = []
@ -39,6 +40,7 @@ class ReceiveModeWeb(object):
r = make_response(render_template( r = make_response(render_template(
'receive.html', 'receive.html',
upload_action=upload_action)) upload_action=upload_action))
return self.web.add_security_headers(r) return self.web.add_security_headers(r)
@self.web.app.route("/<slug_candidate>") @self.web.app.route("/<slug_candidate>")
@ -138,10 +140,24 @@ class ReceiveModeWeb(object):
for filename in filenames: for filename in filenames:
flash('Sent {}'.format(filename), 'info') flash('Sent {}'.format(filename), 'info')
if self.common.settings.get('public_mode'): if self.can_upload:
return redirect('/') if self.common.settings.get('public_mode'):
path = '/'
else:
path = '/{}'.format(slug_candidate)
return redirect('{}'.format(path))
else: 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']) @self.web.app.route("/<slug_candidate>/upload", methods=['POST'])
def upload(slug_candidate): def upload(slug_candidate):
@ -231,39 +247,36 @@ class ReceiveModeRequest(Request):
if self.path == '/upload': if self.path == '/upload':
self.upload_request = True 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 # A dictionary that maps filenames to the bytes uploaded so far
self.progress = {} self.progress = {}
# Create an upload_id, attach it to the request # Create an upload_id, attach it to the request
self.upload_id = self.web.receive_mode.upload_count 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 # Figure out the content length
try: try:
self.content_length = int(self.headers['Content-Length']) self.content_length = int(self.headers['Content-Length'])
except: except:
self.content_length = 0 self.content_length = 0
print("{}: {}".format( print("{}: {}".format(
datetime.now().strftime("%b %d, %I:%M%p"), datetime.now().strftime("%b %d, %I:%M%p"),
strings._("receive_mode_upload_starting").format(self.web.common.human_readable_filesize(self.content_length)) strings._("receive_mode_upload_starting").format(self.web.common.human_readable_filesize(self.content_length))
)) ))
# append to self.uploads_in_progress # append to self.uploads_in_progress
self.web.receive_mode.uploads_in_progress.append(self.upload_id) self.web.receive_mode.uploads_in_progress.append(self.upload_id)
# Tell the GUI # Tell the GUI
self.web.add_request(self.web.REQUEST_STARTED, self.path, { self.web.add_request(self.web.REQUEST_STARTED, self.path, {
'id': self.upload_id, 'id': self.upload_id,
'content_length': self.content_length 'content_length': self.content_length
}) })
self.previous_file = None self.previous_file = None
else:
self.upload_rejected = True
def _get_file_stream(self, total_content_length, content_type, filename=None, content_length=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() super(ReceiveModeRequest, self).close()
if self.upload_request: if self.upload_request:
if not self.upload_rejected: try:
upload_id = self.upload_id
# Inform the GUI that the upload has finished # Inform the GUI that the upload has finished
self.web.add_request(self.web.REQUEST_UPLOAD_FINISHED, self.path, { self.web.add_request(self.web.REQUEST_UPLOAD_FINISHED, self.path, {
'id': self.upload_id 'id': upload_id
}) })
# remove from self.uploads_in_progress # 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): def file_write_func(self, filename, length):
""" """

View File

@ -51,6 +51,7 @@ class ReceiveMode(Mode):
self.uploads_in_progress = 0 self.uploads_in_progress = 0
self.uploads_completed = 0 self.uploads_completed = 0
self.new_upload = False # For scrolling to the bottom of the uploads list 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 # Information about share, and show uploads button
self.info_in_progress_uploads_count = QtWidgets.QLabel() 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 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 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.stop_server()
self.server_status_label.setText(strings._('close_on_timeout', True)) self.server_status_label.setText(strings._('close_on_timeout', True))
return True return True
@ -110,7 +111,9 @@ class ReceiveMode(Mode):
Starting the server. Starting the server.
""" """
# Reset web counters # Reset web counters
self.can_stop_server = False
self.web.receive_mode.upload_count = 0 self.web.receive_mode.upload_count = 0
self.web.receive_mode.can_upload = True
self.web.error404_count = 0 self.web.error404_count = 0
# Hide and reset the uploads if we have previously shared # 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.add(event["data"]["id"], event["data"]["content_length"])
self.uploads_in_progress += 1 self.uploads_in_progress += 1
self.update_uploads_in_progress() 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)) 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 # Update the 'in progress uploads' info
self.uploads_in_progress -= 1 self.uploads_in_progress -= 1
self.update_uploads_in_progress() self.update_uploads_in_progress()
if self.uploads_in_progress == 0:
self.can_stop_server = True
def on_reload_settings(self): def on_reload_settings(self):
""" """