When there is only 1 file being shared, don't zip it

This commit is contained in:
Micah Lee 2018-09-20 09:14:56 -07:00
parent dbae501689
commit 324538bdd3
No known key found for this signature in database
GPG Key ID: 403C2657CD994F73
5 changed files with 55 additions and 43 deletions

View File

@ -120,13 +120,13 @@ def main(cwd=None):
print(strings._("preparing_files")) print(strings._("preparing_files"))
try: try:
web.set_file_info(filenames) web.set_file_info(filenames)
app.cleanup_filenames.append(web.zip_filename) app.cleanup_filenames.append(web.download_filename)
except OSError as e: except OSError as e:
print(e.strerror) print(e.strerror)
sys.exit(1) sys.exit(1)
# Warn about sending large files over Tor # Warn about sending large files over Tor
if web.zip_filesize >= 157286400: # 150mb if web.download_filesize >= 157286400: # 150mb
print('') print('')
print(strings._("large_filesize")) print(strings._("large_filesize"))
print('') print('')

View File

@ -102,8 +102,9 @@ class Web(object):
# Information about the file # Information about the file
self.file_info = [] self.file_info = []
self.zip_filename = None self.is_zipped = False
self.zip_filesize = None self.download_filename = None
self.download_filesize = None
self.zip_writer = None self.zip_writer = None
self.security_headers = [ self.security_headers = [
@ -182,17 +183,19 @@ class Web(object):
'send.html', 'send.html',
slug=self.slug, slug=self.slug,
file_info=self.file_info, file_info=self.file_info,
filename=os.path.basename(self.zip_filename), filename=os.path.basename(self.download_filename),
filesize=self.zip_filesize, filesize=self.download_filesize,
filesize_human=self.common.human_readable_filesize(self.zip_filesize))) filesize_human=self.common.human_readable_filesize(self.download_filesize),
is_zipped=self.is_zipped))
else: else:
# If download is allowed to continue, serve download page # If download is allowed to continue, serve download page
r = make_response(render_template( r = make_response(render_template(
'send.html', 'send.html',
file_info=self.file_info, file_info=self.file_info,
filename=os.path.basename(self.zip_filename), filename=os.path.basename(self.download_filename),
filesize=self.zip_filesize, filesize=self.download_filesize,
filesize_human=self.common.human_readable_filesize(self.zip_filesize))) filesize_human=self.common.human_readable_filesize(self.download_filesize),
is_zipped=self.is_zipped))
return self.add_security_headers(r) return self.add_security_headers(r)
@self.app.route("/<slug_candidate>/download") @self.app.route("/<slug_candidate>/download")
@ -231,8 +234,8 @@ class Web(object):
'id': download_id} 'id': download_id}
) )
dirname = os.path.dirname(self.zip_filename) dirname = os.path.dirname(self.download_filename)
basename = os.path.basename(self.zip_filename) basename = os.path.basename(self.download_filename)
def generate(): def generate():
# The user hasn't canceled the download # The user hasn't canceled the download
@ -244,7 +247,7 @@ class Web(object):
chunk_size = 102400 # 100kb chunk_size = 102400 # 100kb
fp = open(self.zip_filename, 'rb') fp = open(self.download_filename, 'rb')
self.done = False self.done = False
canceled = False canceled = False
while not self.done: while not self.done:
@ -264,7 +267,7 @@ class Web(object):
# tell GUI the progress # tell GUI the progress
downloaded_bytes = fp.tell() downloaded_bytes = fp.tell()
percent = (1.0 * downloaded_bytes / self.zip_filesize) * 100 percent = (1.0 * downloaded_bytes / self.download_filesize) * 100
# only output to stdout if running onionshare in CLI mode, or if using Linux (#203, #304) # only output to stdout if running onionshare in CLI mode, or if using Linux (#203, #304)
if not self.gui_mode or self.common.platform == 'Linux' or self.common.platform == 'BSD': if not self.gui_mode or self.common.platform == 'Linux' or self.common.platform == 'BSD':
@ -308,7 +311,7 @@ class Web(object):
pass pass
r = Response(generate()) r = Response(generate())
r.headers.set('Content-Length', self.zip_filesize) r.headers.set('Content-Length', self.download_filesize)
r.headers.set('Content-Disposition', 'attachment', filename=basename) r.headers.set('Content-Disposition', 'attachment', filename=basename)
r = self.add_security_headers(r) r = self.add_security_headers(r)
# guess content type # guess content type
@ -517,6 +520,7 @@ class Web(object):
page will need to display. This includes zipping up the file in order to page will need to display. This includes zipping up the file in order to
get the zip file's name and size. get the zip file's name and size.
""" """
self.common.log("Web", "set_file_info")
self.cancel_compression = False self.cancel_compression = False
# build file info list # build file info list
@ -537,22 +541,30 @@ class Web(object):
self.file_info['files'] = sorted(self.file_info['files'], key=lambda k: k['basename']) self.file_info['files'] = sorted(self.file_info['files'], key=lambda k: k['basename'])
self.file_info['dirs'] = sorted(self.file_info['dirs'], key=lambda k: k['basename']) self.file_info['dirs'] = sorted(self.file_info['dirs'], key=lambda k: k['basename'])
# Zip up the files and folders # Check if there's only 1 file and no folders
self.zip_writer = ZipWriter(self.common, processed_size_callback=processed_size_callback) if len(self.file_info['files']) == 1 and len(self.file_info['dirs']) == 0:
self.zip_filename = self.zip_writer.zip_filename self.is_zipped = False
for info in self.file_info['files']: self.download_filename = self.file_info['files'][0]['filename']
self.zip_writer.add_file(info['filename']) self.download_filesize = self.file_info['files'][0]['size']
# Canceling early? else:
if self.cancel_compression: # Zip up the files and folders
self.zip_writer.close() self.zip_writer = ZipWriter(self.common, processed_size_callback=processed_size_callback)
return False self.download_filename = self.zip_writer.zip_filename
for info in self.file_info['files']:
self.zip_writer.add_file(info['filename'])
# Canceling early?
if self.cancel_compression:
self.zip_writer.close()
return False
for info in self.file_info['dirs']: for info in self.file_info['dirs']:
if not self.zip_writer.add_dir(info['filename']): if not self.zip_writer.add_dir(info['filename']):
return False return False
self.zip_writer.close()
self.download_filesize = os.path.getsize(self.download_filename)
self.is_zipped = True
self.zip_writer.close()
self.zip_filesize = os.path.getsize(self.zip_filename)
return True return True
def _safe_select_jinja_autoescape(self, filename): def _safe_select_jinja_autoescape(self, filename):

View File

@ -47,7 +47,7 @@ class CompressThread(QtCore.QThread):
# Cancelled # Cancelled
pass pass
self.mode.app.cleanup_filenames.append(self.mode.web.zip_filename) self.mode.app.cleanup_filenames.append(self.mode.web.download_filename)
except OSError as e: except OSError as e:
self.error.emit(e.strerror) self.error.emit(e.strerror)

Binary file not shown.

Before

Width:  |  Height:  |  Size: 45 KiB

View File

@ -10,18 +10,18 @@
<body> <body>
<header class="clearfix"> <header class="clearfix">
<div class="right"> <div class="right">
<ul> <ul>
<li>Total size: <strong>{{ filesize_human }}</strong> (compressed)</li> <li>Total size: <strong>{{ filesize_human }}</strong> {% if is_zipped %} (compressed){% endif %}</li>
{% if slug %} {% if slug %}
<li><a class="button" href='/{{ slug }}/download'>Download Files</a></li> <li><a class="button" href='/{{ slug }}/download'>Download Files</a></li>
{% else %} {% else %}
<li><a class="button" href='/download'>Download Files</a></li> <li><a class="button" href='/download'>Download Files</a></li>
{% endif %} {% endif %}
</ul> </ul>
</div> </div>
<img class="logo" src="/static/img/logo.png" title="OnionShare"> <img class="logo" src="/static/img/logo.png" title="OnionShare">
<h1>OnionShare</h1> <h1>OnionShare</h1>
</header> </header>
<table class="file-list" id="file-list"> <table class="file-list" id="file-list">