Start making Web events actually put Upload objects into Uploads

This commit is contained in:
Micah Lee 2018-05-07 23:07:11 -07:00
parent 9d91e07d20
commit 1db37e13f5
6 changed files with 142 additions and 92 deletions

View file

@ -51,6 +51,8 @@ class Web(object):
REQUEST_CANCELED = 4 REQUEST_CANCELED = 4
REQUEST_RATE_LIMIT = 5 REQUEST_RATE_LIMIT = 5
REQUEST_CLOSE_SERVER = 6 REQUEST_CLOSE_SERVER = 6
REQUEST_UPLOAD_NEW_FILE_STARTED = 7
REQUEST_UPLOAD_FILE_RENAMED = 8
def __init__(self, common, gui_mode, receive_mode=False): def __init__(self, common, gui_mode, receive_mode=False):
self.common = common self.common = common
@ -104,6 +106,7 @@ class Web(object):
self.download_count = 0 self.download_count = 0
self.upload_count = 0 self.upload_count = 0
self.error404_count = 0 self.error404_count = 0
# If "Stop After First Download" is checked (stay_open == False), only allow # If "Stop After First Download" is checked (stay_open == False), only allow
@ -141,7 +144,7 @@ class Web(object):
""" """
self.check_slug_candidate(slug_candidate) self.check_slug_candidate(slug_candidate)
self.add_request(self.REQUEST_LOAD, request.path) self.add_request(Web.REQUEST_LOAD, request.path)
# Deny new downloads if "Stop After First Download" is checked and there is # Deny new downloads if "Stop After First Download" is checked and there is
# currently a download # currently a download
@ -184,7 +187,9 @@ class Web(object):
path = request.path path = request.path
# Tell GUI the download started # Tell GUI the download started
self.add_request(self.REQUEST_STARTED, path, {'id': download_id}) self.add_request(Web.REQUEST_STARTED, path, {
'id': download_id}
)
dirname = os.path.dirname(self.zip_filename) dirname = os.path.dirname(self.zip_filename)
basename = os.path.basename(self.zip_filename) basename = os.path.basename(self.zip_filename)
@ -205,7 +210,9 @@ class Web(object):
while not self.done: while not self.done:
# The user has canceled the download, so stop serving the file # The user has canceled the download, so stop serving the file
if self.client_cancel: if self.client_cancel:
self.add_request(self.REQUEST_CANCELED, path, {'id': download_id}) self.add_request(Web.REQUEST_CANCELED, path, {
'id': download_id
})
break break
chunk = fp.read(chunk_size) chunk = fp.read(chunk_size)
@ -225,7 +232,10 @@ class Web(object):
"\r{0:s}, {1:.2f}% ".format(self.common.human_readable_filesize(downloaded_bytes), percent)) "\r{0:s}, {1:.2f}% ".format(self.common.human_readable_filesize(downloaded_bytes), percent))
sys.stdout.flush() sys.stdout.flush()
self.add_request(self.REQUEST_PROGRESS, path, {'id': download_id, 'bytes': downloaded_bytes}) self.add_request(Web.REQUEST_PROGRESS, path, {
'id': download_id,
'bytes': downloaded_bytes
})
self.done = False self.done = False
except: except:
# looks like the download was canceled # looks like the download was canceled
@ -233,7 +243,9 @@ class Web(object):
canceled = True canceled = True
# tell the GUI the download has canceled # tell the GUI the download has canceled
self.add_request(self.REQUEST_CANCELED, path, {'id': download_id}) self.add_request(Web.REQUEST_CANCELED, path, {
'id': download_id
})
fp.close() fp.close()
@ -270,7 +282,7 @@ class Web(object):
The web app routes for receiving files The web app routes for receiving files
""" """
def index_logic(): def index_logic():
self.add_request(self.REQUEST_LOAD, request.path) self.add_request(Web.REQUEST_LOAD, request.path)
r = make_response(render_template( r = make_response(render_template(
'receive.html', 'receive.html',
@ -330,6 +342,15 @@ class Web(object):
else: else:
valid = True valid = True
basename = os.path.basename(local_path)
if f.filename != basename:
# Tell the GUI that the file has changed names
self.add_request(Web.REQUEST_UPLOAD_FILE_RENAMED, request.path, {
'id': request.upload_id,
'old_filename': f.filename,
'new_filename': basename
})
self.common.log('Web', 'receive_routes', '/upload, uploaded {}, saving to {}'.format(f.filename, local_path)) self.common.log('Web', 'receive_routes', '/upload, uploaded {}, saving to {}'.format(f.filename, local_path))
print(strings._('receive_mode_received_file').format(local_path)) print(strings._('receive_mode_received_file').format(local_path))
f.save(local_path) f.save(local_path)
@ -360,7 +381,7 @@ class Web(object):
if self.common.settings.get('receive_allow_receiver_shutdown'): if self.common.settings.get('receive_allow_receiver_shutdown'):
self.force_shutdown() self.force_shutdown()
r = make_response(render_template('closed.html')) r = make_response(render_template('closed.html'))
self.add_request(self.REQUEST_CLOSE_SERVER, request.path) self.add_request(Web.REQUEST_CLOSE_SERVER, request.path)
return self.add_security_headers(r) return self.add_security_headers(r)
else: else:
return redirect('/{}'.format(slug_candidate)) return redirect('/{}'.format(slug_candidate))
@ -397,14 +418,14 @@ class Web(object):
return "" return ""
def error404(self): def error404(self):
self.add_request(self.REQUEST_OTHER, request.path) self.add_request(Web.REQUEST_OTHER, request.path)
if request.path != '/favicon.ico': if request.path != '/favicon.ico':
self.error404_count += 1 self.error404_count += 1
# In receive mode, with public mode enabled, skip rate limiting 404s # In receive mode, with public mode enabled, skip rate limiting 404s
if not (self.receive_mode and self.common.settings.get('receive_public_mode')): if not (self.receive_mode and self.common.settings.get('receive_public_mode')):
if self.error404_count == 20: if self.error404_count == 20:
self.add_request(self.REQUEST_RATE_LIMIT, request.path) self.add_request(Web.REQUEST_RATE_LIMIT, request.path)
self.force_shutdown() self.force_shutdown()
print(strings._('error_rate_limit')) print(strings._('error_rate_limit'))
@ -610,6 +631,7 @@ class ReceiveModeWSGIMiddleware(object):
environ['web'] = self.web environ['web'] = self.web
return self.app(environ, start_response) return self.app(environ, start_response)
class ReceiveModeTemporaryFile(object): class ReceiveModeTemporaryFile(object):
""" """
A custom TemporaryFile that tells ReceiveModeRequest every time data gets A custom TemporaryFile that tells ReceiveModeRequest every time data gets
@ -649,29 +671,45 @@ class ReceiveModeRequest(Request):
self.web = environ['web'] self.web = environ['web']
# A dictionary that maps filenames to the bytes uploaded so far # A dictionary that maps filenames to the bytes uploaded so far
self.onionshare_progress = {} self.progress = {}
# Is this a valid upload request?
self.upload_request = False
if self.method == 'POST':
if self.web.common.settings.get('receive_public_mode'):
if self.path == '/upload':
self.upload_request = True
else:
if self.path == '/{}/upload'.format(self.web.slug):
self.upload_request = True
# If this is an upload request, create an upload_id (attach it to the request)
self.upload_id = self.web.upload_count
self.web.upload_count += 1
# Tell the GUI
self.web.add_request(Web.REQUEST_STARTED, self.path, {
'id': self.upload_id
})
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):
""" """
This gets called for each file that gets uploaded, and returns an file-like This gets called for each file that gets uploaded, and returns an file-like
writable stream. writable stream.
""" """
# Each upload has a unique id. Now that the upload is starting, attach its # Tell the GUI about the new file upload
# upload_id to the request self.web.add_request(Web.REQUEST_UPLOAD_NEW_FILE_STARTED, self.path, {
self.upload_id = self.web.upload_count 'id': self.upload_id,
self.web.upload_count += 1 'filename': filename,
'total_bytes': total_content_length
# Tell GUI the upload started
self.web.add_request(self.web.REQUEST_STARTED, self.path, {
'id': self.upload_id
}) })
self.onionshare_progress[filename] = { self.progress[filename] = {
'total_bytes': total_content_length, 'total_bytes': total_content_length,
'uploaded_bytes': 0 'uploaded_bytes': 0
} }
if len(self.onionshare_progress) > 0: if len(self.progress) > 0:
print('') print('')
return ReceiveModeTemporaryFile(filename, self.onionshare_update_func) return ReceiveModeTemporaryFile(filename, self.onionshare_update_func)
@ -681,20 +719,20 @@ class ReceiveModeRequest(Request):
When closing the request, print a newline if this was a file upload. When closing the request, print a newline if this was a file upload.
""" """
super(ReceiveModeRequest, self).close() super(ReceiveModeRequest, self).close()
if len(self.onionshare_progress) > 0: if len(self.progress) > 0:
print('') print('')
def onionshare_update_func(self, filename, length): def onionshare_update_func(self, filename, length):
""" """
Keep track of the bytes uploaded so far for all files. Keep track of the bytes uploaded so far for all files.
""" """
self.onionshare_progress[filename]['uploaded_bytes'] += length self.progress[filename]['uploaded_bytes'] += length
uploaded = self.web.common.human_readable_filesize(self.onionshare_progress[filename]['uploaded_bytes']) uploaded = self.web.common.human_readable_filesize(self.progress[filename]['uploaded_bytes'])
total = self.web.common.human_readable_filesize(self.onionshare_progress[filename]['total_bytes']) total = self.web.common.human_readable_filesize(self.progress[filename]['total_bytes'])
print('{}/{} - {} '.format(uploaded, total, filename), end='\r') print('{}/{} - {} '.format(uploaded, total, filename), end='\r')
# Update the GUI on the download progress # Update the GUI on the upload progress
self.web.add_request(self.web.REQUEST_PROGRESS, self.path, { self.web.add_request(Web.REQUEST_PROGRESS, self.path, {
'id': self.upload_id, 'id': self.upload_id,
'bytes': self.onionshare_progress[filename]['uploaded_bytes'] 'progress': self.progress
}) })

View file

@ -323,3 +323,15 @@ class Mode(QtWidgets.QWidget):
Handle REQUEST_CLOSE_SERVER event. Handle REQUEST_CLOSE_SERVER event.
""" """
pass pass
def handle_request_upload_new_file_started(self, event):
"""
Handle REQUEST_UPLOAD_NEW_FILE_STARTED event.
"""
pass
def handle_request_upload_file_renamed(self, event):
"""
Handle REQUEST_UPLOAD_FILE_RENAMED event.
"""
pass

View file

@ -387,6 +387,12 @@ class OnionShareGui(QtWidgets.QMainWindow):
elif event["type"] == Web.REQUEST_CLOSE_SERVER: elif event["type"] == Web.REQUEST_CLOSE_SERVER:
mode.handle_request_close_server(event) mode.handle_request_close_server(event)
elif event["type"] == Web.REQUEST_UPLOAD_NEW_FILE_STARTED:
mode.handle_request_upload_new_file_started(event)
elif event["type"] == Web.REQUEST_UPLOAD_FILE_RENAMED:
mode.handle_request_upload_file_renamed(event)
elif event["path"] != '/favicon.ico': elif event["path"] != '/favicon.ico':
self.status_bar.showMessage('[#{0:d}] {1:s}: {2:s}'.format(mode.web.error404_count, strings._('other_page_loaded', True), event["path"])) self.status_bar.showMessage('[#{0:d}] {1:s}: {2:s}'.format(mode.web.error404_count, strings._('other_page_loaded', True), event["path"]))

View file

@ -131,6 +131,24 @@ class ReceiveMode(Mode):
""" """
self.system_tray.showMessage(strings._('systray_page_loaded_title', True), strings._('systray_upload_page_loaded_message', True)) self.system_tray.showMessage(strings._('systray_page_loaded_title', True), strings._('systray_upload_page_loaded_message', True))
def handle_request_started(self, event):
"""
Handle REQUEST_STARTED event.
"""
self.uploads.add(event["data"]["id"])
self.uploads_in_progress += 1
self.update_uploads_in_progress()
self.system_tray.showMessage(strings._('systray_upload_started_title', True), strings._('systray_upload_started_message', True))
def handle_request_progress(self, event):
"""
Handle REQUEST_PROGRESS event.
"""
self.uploads.update(event["data"]["id"], event["data"]["progress"])
# TODO: not done yet
def handle_request_close_server(self, event): def handle_request_close_server(self, event):
""" """
Handle REQUEST_CLOSE_SERVER event. Handle REQUEST_CLOSE_SERVER event.
@ -138,6 +156,18 @@ class ReceiveMode(Mode):
self.stop_server() self.stop_server()
self.system_tray.showMessage(strings._('systray_close_server_title', True), strings._('systray_close_server_message', True)) self.system_tray.showMessage(strings._('systray_close_server_title', True), strings._('systray_close_server_message', True))
def handle_request_upload_new_file_started(self, event):
"""
Handle REQUEST_UPLOAD_NEW_FILE_STARTED event.
"""
pass
def handle_request_upload_file_renamed(self, event):
"""
Handle REQUEST_UPLOAD_FILE_RENAMED event.
"""
pass
def reset_info_counters(self): def reset_info_counters(self):
""" """
Set the info counters back to zero. Set the info counters back to zero.

View file

@ -17,75 +17,33 @@ GNU General Public License for more details.
You should have received a copy of the GNU General Public License You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
""" """
from datetime import datetime
from PyQt5 import QtCore, QtWidgets, QtGui from PyQt5 import QtCore, QtWidgets, QtGui
from onionshare import strings from onionshare import strings
class Upload(object): class Upload(QtWidgets.QGroupBox):
def __init__(self, common, upload_id, total_bytes): def __init__(self, common, upload_id):
super(Upload, self).__init__()
self.common = common self.common = common
self.upload_id = upload_id self.upload_id = upload_id
self.started = time.time() self.started = datetime.now()
self.total_bytes = total_bytes
self.uploaded_bytes = 0 self.uploaded_bytes = 0
# Uploads have two modes, in progress and finished. In progess, they display # Set the title of the title of the group box based on the start time
# the progress bar. When finished, they display info about the files that self.setTitle(strings._('gui_upload_in_progress', True).format(self.started.strftime("%b %m, %I:%M%p")))
# were uploaded.
# Progress bar
self.progress_bar = QtWidgets.QProgressBar()
self.progress_bar.setTextVisible(True)
self.progress_bar.setAttribute(QtCore.Qt.WA_DeleteOnClose)
self.progress_bar.setAlignment(QtCore.Qt.AlignHCenter)
self.progress_bar.setMinimum(0)
self.progress_bar.setMaximum(total_bytes)
self.progress_bar.setValue(0)
self.progress_bar.setStyleSheet(self.common.css['downloads_uploads_progress_bar'])
self.progress_bar.total_bytes = total_bytes
# Finished
self.finished = QtWidgets.QGroupBox()
self.finished.hide()
# Start at 0 # Start at 0
self.update(0) self.update({})
def update(self, uploaded_bytes): def update(self, progress):
self.uploaded_bytes = uploaded_bytes """
Using the progress from Web, make sure all the file progress bars exist,
self.progress_bar.setValue(uploaded_bytes) and update their progress
if uploaded_bytes == self.progress_bar.uploaded_bytes: """
# Upload is finished, hide the progress bar and show the finished widget pass
self.progress_bar.hide()
# TODO: add file information to the finished widget
ended = time.time()
elapsed = ended - self.started
self.finished.show()
else:
elapsed = time.time() - self.started
if elapsed < 10:
pb_fmt = strings._('gui_download_upload_progress_starting').format(
self.common.human_readable_filesize(downloaded_bytes))
else:
pb_fmt = strings._('gui_download_upload_progress_eta').format(
self.common.human_readable_filesize(downloaded_bytes),
self.estimated_time_remaining)
self.progress_bar.setFormat(pb_fmt)
def cancel(self):
self.progress_bar.setFormat(strings._('gui_canceled'))
@property
def estimated_time_remaining(self):
return self.common.estimated_time_remaining(self.uploaded_bytes,
self.total_bytes,
self.started)
class Uploads(QtWidgets.QScrollArea): class Uploads(QtWidgets.QScrollArea):
@ -123,17 +81,17 @@ class Uploads(QtWidgets.QScrollArea):
widget.setLayout(layout) widget.setLayout(layout)
self.setWidget(widget) self.setWidget(widget)
def add(self, upload_id, total_bytes): def add(self, upload_id):
""" """
Add a new upload progress bar. Add a new upload.
""" """
# Hide the no_uploads_label # Hide the no_uploads_label
self.no_uploads_label.hide() self.no_uploads_label.hide()
# Add it to the list # Add it to the list
uploads = Upload(self.common, upload_id, total_bytes) upload = Upload(self.common, upload_id)
self.uploads[upload_id] = download self.uploads[upload_id] = upload
self.uploads_layout.addWidget(upload.progress_bar) self.uploads_layout.addWidget(upload)
# Scroll to the bottom # Scroll to the bottom
self.vbar.setValue(self.vbar.maximum()) self.vbar.setValue(self.vbar.maximum())
@ -142,7 +100,8 @@ class Uploads(QtWidgets.QScrollArea):
""" """
Update the progress of an upload progress bar. Update the progress of an upload progress bar.
""" """
self.uploads[upload_id].update(uploaded_bytes) pass
#self.uploads[upload_id].update(uploaded_bytes)
def cancel(self, upload_id): def cancel(self, upload_id):
""" """
@ -155,8 +114,7 @@ class Uploads(QtWidgets.QScrollArea):
Reset the uploads back to zero Reset the uploads back to zero
""" """
for upload in self.uploads.values(): for upload in self.uploads.values():
self.uploads_layout.removeWidget(upload.progress_bar) self.uploads_layout.removeWidget(upload)
upload.progress_bar.close()
self.uploads = {} self.uploads = {}
self.no_uploads_label.show() self.no_uploads_label.show()

View file

@ -28,6 +28,10 @@
"systray_download_completed_message": "The user finished downloading your files", "systray_download_completed_message": "The user finished downloading your files",
"systray_download_canceled_title": "OnionShare Download Canceled", "systray_download_canceled_title": "OnionShare Download Canceled",
"systray_download_canceled_message": "The user canceled the download", "systray_download_canceled_message": "The user canceled the download",
"systray_upload_started_title": "OnionShare Upload Started",
"systray_upload_started_message": "A user started uploading files to your computer",
"systray_upload_completed_title": "OnionShare Upload Finished",
"systray_upload_completed_message": "The user finished uploading files to your computer",
"help_local_only": "Do not attempt to use Tor: For development only", "help_local_only": "Do not attempt to use Tor: For development only",
"help_stay_open": "Keep onion service running after download has finished", "help_stay_open": "Keep onion service running after download has finished",
"help_shutdown_timeout": "Shut down the onion service after N seconds", "help_shutdown_timeout": "Shut down the onion service after N seconds",
@ -186,5 +190,7 @@
"systray_upload_page_loaded_message": "A user loaded the upload page", "systray_upload_page_loaded_message": "A user loaded the upload page",
"gui_uploads": "Upload History", "gui_uploads": "Upload History",
"gui_uploads_window_tooltip": "Show/hide uploads", "gui_uploads_window_tooltip": "Show/hide uploads",
"gui_no_uploads": "No uploads yet." "gui_no_uploads": "No uploads yet.",
"gui_upload_in_progress": "Upload in progress, started {}",
"gui_upload_finished": "Uploaded {} to {}"
} }