handles canceled downloads properly (fixes #81)

This commit is contained in:
Micah Lee 2014-09-22 20:22:30 +00:00
parent 8892126155
commit f5ddd23b70
4 changed files with 25 additions and 8 deletions

View File

@ -36,6 +36,7 @@
"gui_stop_server": "Stop Server",
"gui_copy_url": "Copy URL",
"gui_downloads": "Downloads:",
"gui_canceled": "Canceled",
"gui_copied_url": "Copied URL to clipboard",
"gui_starting_server1": "Starting Tor hidden service...",
"gui_starting_server2": "Crunching files...",

View File

@ -63,6 +63,7 @@ REQUEST_LOAD = 0
REQUEST_DOWNLOAD = 1
REQUEST_PROGRESS = 2
REQUEST_OTHER = 3
REQUEST_CANCELED = 4
q = Queue.Queue()
def add_request(type, path, data=None):
@ -137,25 +138,34 @@ def download(slug_candidate):
fp = open(zip_filename, 'rb')
done = False
canceled = False
while not done:
chunk = fp.read(102400)
if chunk == '':
done = True
else:
yield chunk
try:
yield chunk
# tell GUI the progress
downloaded_bytes = fp.tell()
percent = round((1.0 * downloaded_bytes / zip_filesize) * 100, 2);
sys.stdout.write("\r{0}, {1}% ".format(helpers.human_readable_filesize(downloaded_bytes), percent))
sys.stdout.flush()
add_request(REQUEST_PROGRESS, path, { 'id':download_id, 'bytes':downloaded_bytes })
# tell GUI the progress
downloaded_bytes = fp.tell()
percent = round((1.0 * downloaded_bytes / zip_filesize) * 100, 2);
sys.stdout.write("\r{0}, {1}% ".format(helpers.human_readable_filesize(downloaded_bytes), percent))
sys.stdout.flush()
add_request(REQUEST_PROGRESS, path, { 'id':download_id, 'bytes':downloaded_bytes })
except:
# looks like the download was canceled
done = True
canceled = True
# tell the GUI the download has canceled
add_request(REQUEST_CANCELED, path, { 'id':download_id })
fp.close()
sys.stdout.write("\n")
# download is finished, close the server
if not stay_open:
if not stay_open and not canceled:
print strings._("closing_automatically")
if shutdown_func is None:
raise RuntimeError('Not running with the Werkzeug Server')

View File

@ -66,3 +66,6 @@ class Downloads(QtGui.QVBoxLayout):
else:
pb.setFormat("{0}, %p%".format(helpers.human_readable_filesize(downloaded_bytes)))
def cancel_download(self, download_id):
pb = self.progress_bars[download_id]
pb.setFormat(strings._('gui_canceled'))

View File

@ -194,6 +194,9 @@ class OnionShareGui(QtGui.QWidget):
if not web.get_stay_open():
self.server_status.stop_server()
elif event["type"] == web.REQUEST_CANCELED:
self.downloads.cancel_download(event["data"]["id"])
elif event["path"] != '/favicon.ico':
self.status_bar.showMessage('{0}: {1}'.format(strings._('other_page_loaded', True), event["path"]))