mirror of
https://github.com/onionshare/onionshare.git
synced 2025-06-20 04:24:21 -04:00
handles canceled downloads properly (fixes #81)
This commit is contained in:
parent
8892126155
commit
f5ddd23b70
4 changed files with 25 additions and 8 deletions
|
@ -36,6 +36,7 @@
|
||||||
"gui_stop_server": "Stop Server",
|
"gui_stop_server": "Stop Server",
|
||||||
"gui_copy_url": "Copy URL",
|
"gui_copy_url": "Copy URL",
|
||||||
"gui_downloads": "Downloads:",
|
"gui_downloads": "Downloads:",
|
||||||
|
"gui_canceled": "Canceled",
|
||||||
"gui_copied_url": "Copied URL to clipboard",
|
"gui_copied_url": "Copied URL to clipboard",
|
||||||
"gui_starting_server1": "Starting Tor hidden service...",
|
"gui_starting_server1": "Starting Tor hidden service...",
|
||||||
"gui_starting_server2": "Crunching files...",
|
"gui_starting_server2": "Crunching files...",
|
||||||
|
|
|
@ -63,6 +63,7 @@ REQUEST_LOAD = 0
|
||||||
REQUEST_DOWNLOAD = 1
|
REQUEST_DOWNLOAD = 1
|
||||||
REQUEST_PROGRESS = 2
|
REQUEST_PROGRESS = 2
|
||||||
REQUEST_OTHER = 3
|
REQUEST_OTHER = 3
|
||||||
|
REQUEST_CANCELED = 4
|
||||||
q = Queue.Queue()
|
q = Queue.Queue()
|
||||||
|
|
||||||
def add_request(type, path, data=None):
|
def add_request(type, path, data=None):
|
||||||
|
@ -137,11 +138,13 @@ def download(slug_candidate):
|
||||||
|
|
||||||
fp = open(zip_filename, 'rb')
|
fp = open(zip_filename, 'rb')
|
||||||
done = False
|
done = False
|
||||||
|
canceled = False
|
||||||
while not done:
|
while not done:
|
||||||
chunk = fp.read(102400)
|
chunk = fp.read(102400)
|
||||||
if chunk == '':
|
if chunk == '':
|
||||||
done = True
|
done = True
|
||||||
else:
|
else:
|
||||||
|
try:
|
||||||
yield chunk
|
yield chunk
|
||||||
|
|
||||||
# tell GUI the progress
|
# tell GUI the progress
|
||||||
|
@ -150,12 +153,19 @@ def download(slug_candidate):
|
||||||
sys.stdout.write("\r{0}, {1}% ".format(helpers.human_readable_filesize(downloaded_bytes), percent))
|
sys.stdout.write("\r{0}, {1}% ".format(helpers.human_readable_filesize(downloaded_bytes), percent))
|
||||||
sys.stdout.flush()
|
sys.stdout.flush()
|
||||||
add_request(REQUEST_PROGRESS, path, { 'id':download_id, 'bytes':downloaded_bytes })
|
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()
|
fp.close()
|
||||||
sys.stdout.write("\n")
|
sys.stdout.write("\n")
|
||||||
|
|
||||||
# download is finished, close the server
|
# download is finished, close the server
|
||||||
if not stay_open:
|
if not stay_open and not canceled:
|
||||||
print strings._("closing_automatically")
|
print strings._("closing_automatically")
|
||||||
if shutdown_func is None:
|
if shutdown_func is None:
|
||||||
raise RuntimeError('Not running with the Werkzeug Server')
|
raise RuntimeError('Not running with the Werkzeug Server')
|
||||||
|
|
|
@ -66,3 +66,6 @@ class Downloads(QtGui.QVBoxLayout):
|
||||||
else:
|
else:
|
||||||
pb.setFormat("{0}, %p%".format(helpers.human_readable_filesize(downloaded_bytes)))
|
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'))
|
||||||
|
|
|
@ -194,6 +194,9 @@ class OnionShareGui(QtGui.QWidget):
|
||||||
if not web.get_stay_open():
|
if not web.get_stay_open():
|
||||||
self.server_status.stop_server()
|
self.server_status.stop_server()
|
||||||
|
|
||||||
|
elif event["type"] == web.REQUEST_CANCELED:
|
||||||
|
self.downloads.cancel_download(event["data"]["id"])
|
||||||
|
|
||||||
elif event["path"] != '/favicon.ico':
|
elif event["path"] != '/favicon.ico':
|
||||||
self.status_bar.showMessage('{0}: {1}'.format(strings._('other_page_loaded', True), event["path"]))
|
self.status_bar.showMessage('{0}: {1}'.format(strings._('other_page_loaded', True), event["path"]))
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue