mirror of
https://github.com/onionshare/onionshare.git
synced 2025-08-19 03:18:11 -04:00
added progress download progress notifications (#36)
This commit is contained in:
parent
62c1045aa6
commit
68bba73a8c
5 changed files with 63 additions and 10 deletions
|
@ -7,6 +7,7 @@ include onionshare/404.html
|
||||||
include onionshare/strings.json
|
include onionshare/strings.json
|
||||||
include onionshare_gui/templates/index.html
|
include onionshare_gui/templates/index.html
|
||||||
include onionshare_gui/static/jquery-1.11.1.min.js
|
include onionshare_gui/static/jquery-1.11.1.min.js
|
||||||
|
include onionshare_gui/static/helpers.js
|
||||||
include onionshare_gui/static/onionshare.js
|
include onionshare_gui/static/onionshare.js
|
||||||
include onionshare_gui/static/style.css
|
include onionshare_gui/static/style.css
|
||||||
include onionshare_gui/static/loader.gif
|
include onionshare_gui/static/loader.gif
|
||||||
|
|
|
@ -27,30 +27,62 @@ def set_file_info(new_filename, new_filehash, new_filesize):
|
||||||
|
|
||||||
REQUEST_LOAD = 0
|
REQUEST_LOAD = 0
|
||||||
REQUEST_DOWNLOAD = 1
|
REQUEST_DOWNLOAD = 1
|
||||||
REQUEST_OTHER = 2
|
REQUEST_PROGRESS = 2
|
||||||
|
REQUEST_OTHER = 3
|
||||||
request_q = Queue.Queue()
|
request_q = Queue.Queue()
|
||||||
|
|
||||||
def add_request(type):
|
download_count = 0
|
||||||
|
|
||||||
|
def add_request(type, path, data=None):
|
||||||
global request_q
|
global request_q
|
||||||
request_q.put({
|
request_q.put({
|
||||||
'type': type,
|
'type': type,
|
||||||
'path': request.path
|
'path': path,
|
||||||
|
'data': data
|
||||||
})
|
})
|
||||||
|
|
||||||
@app.route("/{0}".format(slug))
|
@app.route("/{0}".format(slug))
|
||||||
def index():
|
def index():
|
||||||
global filename, filesize, filehash, slug, strings, REQUEST_LOAD
|
global filename, filesize, filehash, slug, strings, REQUEST_LOAD
|
||||||
add_request(REQUEST_LOAD)
|
add_request(REQUEST_LOAD, request.path)
|
||||||
return render_template_string(open('{0}/index.html'.format(os.path.dirname(__file__))).read(),
|
return render_template_string(open('{0}/index.html'.format(os.path.dirname(__file__))).read(),
|
||||||
slug=slug, filename=os.path.basename(filename), filehash=filehash, filesize=filesize, strings=strings)
|
slug=slug, filename=os.path.basename(filename), filehash=filehash, filesize=filesize, strings=strings)
|
||||||
|
|
||||||
@app.route("/{0}/download".format(slug))
|
@app.route("/{0}/download".format(slug))
|
||||||
def download():
|
def download():
|
||||||
global filename, request_q, REQUEST_DOWNLOAD
|
global filename, filesize, request_q, download_count
|
||||||
add_request(REQUEST_DOWNLOAD)
|
global REQUEST_DOWNLOAD, REQUEST_PROGRESS
|
||||||
|
|
||||||
|
# each download has a unique id
|
||||||
|
download_id = download_count
|
||||||
|
download_count += 1
|
||||||
|
|
||||||
|
# tell GUI the download started
|
||||||
|
path = request.path
|
||||||
|
add_request(REQUEST_DOWNLOAD, path, { 'id':download_id })
|
||||||
|
|
||||||
dirname = os.path.dirname(filename)
|
dirname = os.path.dirname(filename)
|
||||||
basename = os.path.basename(filename)
|
basename = os.path.basename(filename)
|
||||||
return send_from_directory(dirname, basename, as_attachment=True)
|
|
||||||
|
def generate():
|
||||||
|
chunk_size = 102400 # 100kb
|
||||||
|
|
||||||
|
fp = open(filename, 'rb')
|
||||||
|
done = False
|
||||||
|
while not done:
|
||||||
|
chunk = fp.read(102400)
|
||||||
|
if chunk == '':
|
||||||
|
done = True
|
||||||
|
yield chunk
|
||||||
|
|
||||||
|
# tell GUI the progress
|
||||||
|
add_request(REQUEST_PROGRESS, path, { 'id':download_id, 'bytes':fp.tell() })
|
||||||
|
fp.close()
|
||||||
|
|
||||||
|
r = Response(generate())
|
||||||
|
r.headers.add('Content-Length', filesize)
|
||||||
|
r.headers.add('Content-Disposition', 'attachment', filename=basename)
|
||||||
|
return r
|
||||||
|
|
||||||
@app.errorhandler(404)
|
@app.errorhandler(404)
|
||||||
def page_not_found(e):
|
def page_not_found(e):
|
||||||
|
|
11
onionshare_gui/static/helpers.js
Normal file
11
onionshare_gui/static/helpers.js
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
function human_readable_filesize(bytes, si) {
|
||||||
|
var thresh = si ? 1000 : 1024;
|
||||||
|
if(bytes < thresh) return bytes + ' B';
|
||||||
|
var units = si ? ['kB','MB','GB','TB','PB','EB','ZB','YB'] : ['KiB','MiB','GiB','TiB','PiB','EiB','ZiB','YiB'];
|
||||||
|
var u = -1;
|
||||||
|
do {
|
||||||
|
bytes /= thresh;
|
||||||
|
++u;
|
||||||
|
} while(bytes >= thresh);
|
||||||
|
return bytes.toFixed(1)+' '+units[u];
|
||||||
|
};
|
|
@ -21,7 +21,8 @@ $(function(){
|
||||||
|
|
||||||
var REQUEST_LOAD = 0;
|
var REQUEST_LOAD = 0;
|
||||||
var REQUEST_DOWNLOAD = 1;
|
var REQUEST_DOWNLOAD = 1;
|
||||||
var REQUEST_OTHER = 2;
|
var REQUEST_PROGRESS = 2;
|
||||||
|
var REQUEST_OTHER = 3;
|
||||||
function check_for_requests() {
|
function check_for_requests() {
|
||||||
$.ajax({
|
$.ajax({
|
||||||
url: '/check_for_requests',
|
url: '/check_for_requests',
|
||||||
|
@ -31,7 +32,14 @@ $(function(){
|
||||||
if(r.type == REQUEST_LOAD) {
|
if(r.type == REQUEST_LOAD) {
|
||||||
update($('<span>').addClass('weblog').html(onionshare.strings['download_page_loaded']));
|
update($('<span>').addClass('weblog').html(onionshare.strings['download_page_loaded']));
|
||||||
} else if(r.type == REQUEST_DOWNLOAD) {
|
} else if(r.type == REQUEST_DOWNLOAD) {
|
||||||
update($('<span>').addClass('weblog').html(onionshare.strings['download_started']));
|
var $download = $('<span>')
|
||||||
|
.attr('id', 'download-'+r.data.id)
|
||||||
|
.addClass('weblog').html(onionshare.strings['download_started'])
|
||||||
|
.append($('<span>').addClass('progress'));
|
||||||
|
update($download);
|
||||||
|
} else if(r.type == REQUEST_PROGRESS) {
|
||||||
|
var percent = Math.floor((r.data.bytes / onionshare.filesize) * 100);
|
||||||
|
$('#download-'+r.data.id+' .progress').html(' '+human_readable_filesize(r.data.bytes)+', '+percent+'%');
|
||||||
} else {
|
} else {
|
||||||
if(r.path != '/favicon.ico')
|
if(r.path != '/favicon.ico')
|
||||||
update($('<span>').addClass('weblog-error').html(onionshare.strings['other_page_loaded']+': '+r.path));
|
update($('<span>').addClass('weblog-error').html(onionshare.strings['other_page_loaded']+': '+r.path));
|
||||||
|
@ -65,7 +73,7 @@ $(function(){
|
||||||
|
|
||||||
$('#loading').remove();
|
$('#loading').remove();
|
||||||
|
|
||||||
$('#filesize .value').html(onionshare.filesize+' bytes');
|
$('#filesize .value').html(human_readable_filesize(onionshare.filesize));
|
||||||
$('#filehash .value').html(onionshare.filehash);
|
$('#filehash .value').html(onionshare.filehash);
|
||||||
$('#filesize').show(500);
|
$('#filesize').show(500);
|
||||||
$('#filehash').show(500);
|
$('#filehash').show(500);
|
||||||
|
|
|
@ -29,6 +29,7 @@
|
||||||
</ul>
|
</ul>
|
||||||
</ul>
|
</ul>
|
||||||
<script src="static/jquery-1.11.1.min.js"></script>
|
<script src="static/jquery-1.11.1.min.js"></script>
|
||||||
|
<script src="static/helpers.js"></script>
|
||||||
<script src="static/onionshare.js"></script>
|
<script src="static/onionshare.js"></script>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue