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,7 +88,11 @@ 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:
return json.dumps({"error_flashes": [msg]})
else:
flash(msg, 'error')
if self.common.settings.get('public_mode'): if self.common.settings.get('public_mode'):
return redirect('/') return redirect('/')
@ -97,29 +101,38 @@ class ReceiveModeWeb(object):
# 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 ajax:
return json.dumps({"info_flashes": info_flashes})
else:
if self.common.settings.get('public_mode'): if self.common.settings.get('public_mode'):
path = '/' path = '/'
else: else:
path = '/{}'.format(slug_candidate) path = '/{}'.format(slug_candidate)
return redirect('{}'.format(path)) 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=''):
r = make_response(render_template(
'thankyou.html'))
return self.web.add_security_headers(r) 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'])