Closing the GUI window causes any current download to stop immediately (fixes #262)

This commit is contained in:
Micah Lee 2016-04-10 17:43:37 -07:00
parent f5496ba20c
commit 7b8e9e1e6f
2 changed files with 22 additions and 0 deletions

View File

@ -161,6 +161,10 @@ def index(slug_candidate):
filesize_human=helpers.human_readable_filesize(zip_filesize)
)
# If the client closes the OnionShare window while a download is in progress,
# it should immediately stop serving the file. The client_cancel global is
# used to tell the download function that the client is canceling the download.
client_cancel = False
@app.route("/<slug_candidate>/download")
def download(slug_candidate):
@ -187,12 +191,21 @@ def download(slug_candidate):
basename = os.path.basename(zip_filename)
def generate():
# The user hasn't canceled the download
global client_cancel
client_cancel = False
chunk_size = 102400 # 100kb
fp = open(zip_filename, 'rb')
done = False
canceled = False
while not done:
# The user has canceled the download, so stop serving the file
if client_cancel:
add_request(REQUEST_CANCELED, path, {'id': download_id})
break;
chunk = fp.read(chunk_size)
if chunk == b'':
done = True
@ -285,6 +298,12 @@ def stop(port):
"""
Stop the flask web server by loading /shutdown.
"""
# If the user cancels the download, let the download function know to stop
# serving the file
global client_cancel
client_cancel = True
# to stop flask, load http://127.0.0.1:<port>/<shutdown_slug>/shutdown
try:
if transparent_torification:

View File

@ -243,6 +243,9 @@ class OnionShareGui(QtWidgets.QMainWindow):
"""
self.status_bar.clearMessage()
def closeEvent(self, e):
self.stop_server()
def alert(msg, icon=QtWidgets.QMessageBox.NoIcon):
"""