Refactor upload_logic to work with both normal uploads and ajax uploads

This commit is contained in:
Micah Lee 2019-02-14 10:11:04 -08:00
parent 38c39918d4
commit a22d21c222
No known key found for this signature in database
GPG Key ID: 403C2657CD994F73

View File

@ -56,7 +56,7 @@ class ReceiveModeWeb(object):
return index_logic() return index_logic()
def upload_logic(slug_candidate=''): def upload_logic(slug_candidate='', ajax=False):
""" """
Handle the upload files POST request, though at this point, the files have Handle the upload files POST request, though at this point, the files have
already been uploaded and saved to their correct locations. already been uploaded and saved to their correct locations.
@ -88,39 +88,52 @@ class ReceiveModeWeb(object):
}) })
print(strings._('error_cannot_create_data_dir').format(request.receive_mode_dir)) print(strings._('error_cannot_create_data_dir').format(request.receive_mode_dir))
flash('Error uploading, please inform the OnionShare user', 'error') msg = 'Error uploading, please inform the OnionShare user'
if ajax:
if self.common.settings.get('public_mode'): return json.dumps({"error_flashes": [msg]})
return redirect('/')
else: else:
return redirect('/{}'.format(slug_candidate)) flash(msg, 'error')
if self.common.settings.get('public_mode'):
return redirect('/')
else:
return redirect('/{}'.format(slug_candidate))
# Note that flash strings are in English, and not translated, on purpose, # Note that flash strings are in English, and not translated, on purpose,
# to avoid leaking the locale of the OnionShare user # to avoid leaking the locale of the OnionShare user
if ajax:
info_flashes = []
if len(filenames) == 0: if len(filenames) == 0:
flash('No files uploaded', 'info') msg = 'No files uploaded'
if ajax:
info_flashes.append(msg)
else:
flash(msg, 'info')
else: else:
for filename in filenames: for filename in filenames:
flash('Sent {}'.format(filename), 'info') msg = 'Sent {}'.format(filename)
if ajax:
info_flashes.append(msg)
else:
flash(msg, 'info')
if self.can_upload: if self.can_upload:
if self.common.settings.get('public_mode'): if ajax:
path = '/' return json.dumps({"info_flashes": info_flashes})
else: else:
path = '/{}'.format(slug_candidate) if self.common.settings.get('public_mode'):
path = '/'
return redirect('{}'.format(path)) else:
path = '/{}'.format(slug_candidate)
return redirect('{}'.format(path))
else: else:
# It was the last upload and the timer ran out if ajax:
if self.common.settings.get('public_mode'): return json.dumps({"new_body": render_template('thankyou.html')})
return thankyou_logic(slug_candidate)
else: else:
return thankyou_logic() # It was the last upload and the timer ran out
r = make_response(render_template('thankyou.html'))
def thankyou_logic(slug_candidate=''): return self.web.add_security_headers(r)
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):