mirror of
https://github.com/onionshare/onionshare.git
synced 2025-06-06 22:09:15 -04:00
command line version displays download progress (#53), and hidden service page displays file size as human readable
This commit is contained in:
parent
4cee9738b6
commit
1b28a4be49
2 changed files with 29 additions and 4 deletions
|
@ -69,7 +69,7 @@
|
||||||
<p><a class="button" href='/{{ slug }}/download'>{{ filename }} ▼</a></p>
|
<p><a class="button" href='/{{ slug }}/download'>{{ filename }} ▼</a></p>
|
||||||
|
|
||||||
<div class="metadata">
|
<div class="metadata">
|
||||||
<p>{{strings.filesize}}: <strong>{{ filesize }} bytes</strong></p>
|
<p>{{strings.filesize}}: <strong title="{{ filesize }} bytes">{{ filesize_human }}</strong></p>
|
||||||
<p>{{strings.sha1_checksum}}: <strong>{{ filehash }}</strong></p>
|
<p>{{strings.sha1_checksum}}: <strong>{{ filehash }}</strong></p>
|
||||||
</div>
|
</div>
|
||||||
</body>
|
</body>
|
||||||
|
|
|
@ -43,12 +43,31 @@ def add_request(type, path, data=None):
|
||||||
'data': data
|
'data': data
|
||||||
})
|
})
|
||||||
|
|
||||||
|
def human_readable_filesize(b):
|
||||||
|
thresh = 1024.0
|
||||||
|
if b < thresh:
|
||||||
|
return '{0} B'.format(b)
|
||||||
|
units = ['KiB','MiB','GiB','TiB','PiB','EiB','ZiB','YiB']
|
||||||
|
u = 0
|
||||||
|
b /= thresh
|
||||||
|
while b >= thresh:
|
||||||
|
b /= thresh
|
||||||
|
u += 1
|
||||||
|
return '{0} {1}'.format(round(b, 1), units[u])
|
||||||
|
|
||||||
@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, request.path)
|
add_request(REQUEST_LOAD, request.path)
|
||||||
return render_template_string(open('{0}/index.html'.format(onionshare_dir)).read(),
|
return render_template_string(
|
||||||
slug=slug, filename=os.path.basename(filename), filehash=filehash, filesize=filesize, strings=strings)
|
open('{0}/index.html'.format(onionshare_dir)).read(),
|
||||||
|
slug=slug,
|
||||||
|
filename=os.path.basename(filename),
|
||||||
|
filehash=filehash,
|
||||||
|
filesize=filesize,
|
||||||
|
filesize_human=human_readable_filesize(filesize),
|
||||||
|
strings=strings
|
||||||
|
)
|
||||||
|
|
||||||
@app.route("/{0}/download".format(slug))
|
@app.route("/{0}/download".format(slug))
|
||||||
def download():
|
def download():
|
||||||
|
@ -79,8 +98,14 @@ def download():
|
||||||
yield chunk
|
yield chunk
|
||||||
|
|
||||||
# tell GUI the progress
|
# tell GUI the progress
|
||||||
add_request(REQUEST_PROGRESS, path, { 'id':download_id, 'bytes':fp.tell() })
|
downloaded_bytes = fp.tell()
|
||||||
|
percent = round((1.0 * downloaded_bytes / filesize) * 100, 2);
|
||||||
|
sys.stdout.write("\r{0}, {1}% ".format(human_readable_filesize(downloaded_bytes), percent))
|
||||||
|
sys.stdout.flush()
|
||||||
|
add_request(REQUEST_PROGRESS, path, { 'id':download_id, 'bytes':downloaded_bytes })
|
||||||
|
|
||||||
fp.close()
|
fp.close()
|
||||||
|
sys.stdout.write("\n")
|
||||||
|
|
||||||
r = Response(generate())
|
r = Response(generate())
|
||||||
r.headers.add('Content-Length', filesize)
|
r.headers.add('Content-Length', filesize)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue