mirror of
https://github.com/onionshare/onionshare.git
synced 2025-01-25 10:42:58 -03:00
Add new WSGI middleware just to attach the Web object to environ, and improve the UI of file upload progress
This commit is contained in:
parent
f7640416eb
commit
bd7305ab16
2 changed files with 30 additions and 3 deletions
|
@ -66,6 +66,8 @@ class Web(object):
|
||||||
# Are we using receive mode?
|
# Are we using receive mode?
|
||||||
self.receive_mode = receive_mode
|
self.receive_mode = receive_mode
|
||||||
if self.receive_mode:
|
if self.receive_mode:
|
||||||
|
# Use custom WSGI middleware, to modify environ
|
||||||
|
self.app.wsgi_app = ReceiveModeWSGIMiddleware(self.app.wsgi_app, self)
|
||||||
# Use a custom Request class to track upload progess
|
# Use a custom Request class to track upload progess
|
||||||
self.app.request_class = ReceiveModeRequest
|
self.app.request_class = ReceiveModeRequest
|
||||||
|
|
||||||
|
@ -318,7 +320,8 @@ class Web(object):
|
||||||
if len(filenames) == 0:
|
if len(filenames) == 0:
|
||||||
flash('No files uploaded')
|
flash('No files uploaded')
|
||||||
else:
|
else:
|
||||||
flash('Uploaded {}'.format(', '.join(filenames)))
|
for filename in filenames:
|
||||||
|
flash('Uploaded {}'.format(filename))
|
||||||
|
|
||||||
return redirect('/{}'.format(slug_candidate))
|
return redirect('/{}'.format(slug_candidate))
|
||||||
|
|
||||||
|
@ -535,6 +538,19 @@ class ZipWriter(object):
|
||||||
self.z.close()
|
self.z.close()
|
||||||
|
|
||||||
|
|
||||||
|
class ReceiveModeWSGIMiddleware(object):
|
||||||
|
"""
|
||||||
|
Custom WSGI middleware in order to attach the Web object to environ, so
|
||||||
|
ReceiveModeRequest can access it.
|
||||||
|
"""
|
||||||
|
def __init__(self, app, web):
|
||||||
|
self.app = app
|
||||||
|
self.web = web
|
||||||
|
|
||||||
|
def __call__(self, environ, start_response):
|
||||||
|
environ['web'] = self.web
|
||||||
|
return self.app(environ, start_response)
|
||||||
|
|
||||||
class ReceiveModeTemporaryFile(object):
|
class ReceiveModeTemporaryFile(object):
|
||||||
"""
|
"""
|
||||||
A custom TemporaryFile that tells ReceiveModeRequest every time data gets
|
A custom TemporaryFile that tells ReceiveModeRequest every time data gets
|
||||||
|
@ -571,6 +587,7 @@ class ReceiveModeRequest(Request):
|
||||||
"""
|
"""
|
||||||
def __init__(self, environ, populate_request=True, shallow=False):
|
def __init__(self, environ, populate_request=True, shallow=False):
|
||||||
super(ReceiveModeRequest, self).__init__(environ, populate_request, shallow)
|
super(ReceiveModeRequest, self).__init__(environ, populate_request, shallow)
|
||||||
|
self.web = environ['web']
|
||||||
|
|
||||||
# A dictionary that maps filenames to the bytes uploaded so far
|
# A dictionary that maps filenames to the bytes uploaded so far
|
||||||
self.onionshare_progress = {}
|
self.onionshare_progress = {}
|
||||||
|
@ -580,13 +597,22 @@ class ReceiveModeRequest(Request):
|
||||||
This gets called for each file that gets uploaded, and returns an file-like
|
This gets called for each file that gets uploaded, and returns an file-like
|
||||||
writable stream.
|
writable stream.
|
||||||
"""
|
"""
|
||||||
|
if len(self.onionshare_progress) > 0:
|
||||||
print('')
|
print('')
|
||||||
self.onionshare_progress[filename] = 0
|
self.onionshare_progress[filename] = 0
|
||||||
return ReceiveModeTemporaryFile(filename, self.onionshare_update_func)
|
return ReceiveModeTemporaryFile(filename, self.onionshare_update_func)
|
||||||
|
|
||||||
|
def close(self):
|
||||||
|
"""
|
||||||
|
When closing the request, print a newline if this was a file upload.
|
||||||
|
"""
|
||||||
|
super(ReceiveModeRequest, self).close()
|
||||||
|
if len(self.onionshare_progress) > 0:
|
||||||
|
print('')
|
||||||
|
|
||||||
def onionshare_update_func(self, filename, length):
|
def onionshare_update_func(self, filename, length):
|
||||||
"""
|
"""
|
||||||
Keep track of the bytes uploaded so far for all files.
|
Keep track of the bytes uploaded so far for all files.
|
||||||
"""
|
"""
|
||||||
self.onionshare_progress[filename] += length
|
self.onionshare_progress[filename] += length
|
||||||
print('\r{}: {} '.format(filename, self.onionshare_progress[filename]), end='')
|
print('{} - {} '.format(self.web.common.human_readable_filesize(self.onionshare_progress[filename]), filename), end='\r')
|
||||||
|
|
|
@ -135,6 +135,7 @@ ul.flashes {
|
||||||
margin: 0;
|
margin: 0;
|
||||||
padding: 0;
|
padding: 0;
|
||||||
color: #cc0000;
|
color: #cc0000;
|
||||||
|
text-align: left;
|
||||||
}
|
}
|
||||||
|
|
||||||
ul.flashes li {
|
ul.flashes li {
|
||||||
|
|
Loading…
Add table
Reference in a new issue