mirror of
https://github.com/onionshare/onionshare.git
synced 2025-01-25 10:42:58 -03:00
If "Stop sharing automatically" is selected, only allow one download at a time (fixes #248)
This commit is contained in:
parent
0b588e543a
commit
c07f4e5f83
4 changed files with 53 additions and 5 deletions
|
@ -144,6 +144,11 @@ def check_slug_candidate(slug_candidate, slug_compare = None):
|
||||||
if not helpers.constant_time_compare(slug_compare.encode('ascii'), slug_candidate.encode('ascii')):
|
if not helpers.constant_time_compare(slug_compare.encode('ascii'), slug_candidate.encode('ascii')):
|
||||||
abort(404)
|
abort(404)
|
||||||
|
|
||||||
|
|
||||||
|
# If "Stop sharing automatically" is checked (stay_open == False), only allow
|
||||||
|
# one download at a time.
|
||||||
|
download_in_progress = False
|
||||||
|
|
||||||
@app.route("/<slug_candidate>")
|
@app.route("/<slug_candidate>")
|
||||||
def index(slug_candidate):
|
def index(slug_candidate):
|
||||||
"""
|
"""
|
||||||
|
@ -152,14 +157,22 @@ def index(slug_candidate):
|
||||||
check_slug_candidate(slug_candidate)
|
check_slug_candidate(slug_candidate)
|
||||||
|
|
||||||
add_request(REQUEST_LOAD, request.path)
|
add_request(REQUEST_LOAD, request.path)
|
||||||
|
|
||||||
|
# Deny new downloads if "Stop sharing automatically" is checked and there is
|
||||||
|
# currently a download
|
||||||
|
global stay_open, download_in_progress
|
||||||
|
deny_download = not stay_open and download_in_progress
|
||||||
|
if deny_download:
|
||||||
|
return render_template_string(open(helpers.get_resource_path('html/denied.html')).read())
|
||||||
|
|
||||||
|
# If download is allowed to continue, serve download page
|
||||||
return render_template_string(
|
return render_template_string(
|
||||||
open(helpers.get_resource_path('html/index.html')).read(),
|
open(helpers.get_resource_path('html/index.html')).read(),
|
||||||
slug=slug,
|
slug=slug,
|
||||||
file_info=file_info,
|
file_info=file_info,
|
||||||
filename=os.path.basename(zip_filename),
|
filename=os.path.basename(zip_filename),
|
||||||
filesize=zip_filesize,
|
filesize=zip_filesize,
|
||||||
filesize_human=helpers.human_readable_filesize(zip_filesize)
|
filesize_human=helpers.human_readable_filesize(zip_filesize))
|
||||||
)
|
|
||||||
|
|
||||||
# If the client closes the OnionShare window while a download is in progress,
|
# If the client closes the OnionShare window while a download is in progress,
|
||||||
# it should immediately stop serving the file. The client_cancel global is
|
# it should immediately stop serving the file. The client_cancel global is
|
||||||
|
@ -173,6 +186,13 @@ def download(slug_candidate):
|
||||||
"""
|
"""
|
||||||
check_slug_candidate(slug_candidate)
|
check_slug_candidate(slug_candidate)
|
||||||
|
|
||||||
|
# Deny new downloads if "Stop sharing automatically" is checked and there is
|
||||||
|
# currently a download
|
||||||
|
global stay_open, download_in_progress
|
||||||
|
deny_download = not stay_open and download_in_progress
|
||||||
|
if deny_download:
|
||||||
|
return render_template_string(open(helpers.get_resource_path('html/denied.html')).read())
|
||||||
|
|
||||||
global download_count
|
global download_count
|
||||||
|
|
||||||
# each download has a unique id
|
# each download has a unique id
|
||||||
|
@ -195,6 +215,11 @@ def download(slug_candidate):
|
||||||
global client_cancel
|
global client_cancel
|
||||||
client_cancel = False
|
client_cancel = False
|
||||||
|
|
||||||
|
# Starting a new download
|
||||||
|
global stay_open, download_in_progress
|
||||||
|
if not stay_open:
|
||||||
|
download_in_progress = True
|
||||||
|
|
||||||
chunk_size = 102400 # 100kb
|
chunk_size = 102400 # 100kb
|
||||||
|
|
||||||
fp = open(zip_filename, 'rb')
|
fp = open(zip_filename, 'rb')
|
||||||
|
@ -237,7 +262,11 @@ def download(slug_candidate):
|
||||||
if helpers.get_platform() != 'Darwin':
|
if helpers.get_platform() != 'Darwin':
|
||||||
sys.stdout.write("\n")
|
sys.stdout.write("\n")
|
||||||
|
|
||||||
# download is finished, close the server
|
# Download is finished
|
||||||
|
if not stay_open:
|
||||||
|
download_in_progress = False
|
||||||
|
|
||||||
|
# Close the server, if necessary
|
||||||
if not stay_open and not canceled:
|
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:
|
||||||
|
|
18
resources/html/denied.html
Normal file
18
resources/html/denied.html
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<title>OnionShare</title>
|
||||||
|
<style>
|
||||||
|
body {
|
||||||
|
background-color: #222222;
|
||||||
|
color: #ffffff;
|
||||||
|
text-align: center;
|
||||||
|
font-family: sans-serif;
|
||||||
|
padding: 5em 1em;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<p>OnionShare download in progress</p>
|
||||||
|
</body>
|
||||||
|
</html>
|
|
@ -1,6 +1,6 @@
|
||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<title>OnionShare</title>
|
<title>OnionShare</title>
|
||||||
<style type="text/css">
|
<style type="text/css">
|
||||||
body {
|
body {
|
||||||
|
|
3
setup.py
3
setup.py
|
@ -73,7 +73,8 @@ locale = [
|
||||||
|
|
||||||
html = [
|
html = [
|
||||||
'resources/html/index.html',
|
'resources/html/index.html',
|
||||||
'resources/html/404.html',
|
'resources/html/denied.html',
|
||||||
|
'resources/html/404.html'
|
||||||
]
|
]
|
||||||
|
|
||||||
setup(
|
setup(
|
||||||
|
|
Loading…
Add table
Reference in a new issue