mirror of
https://github.com/onionshare/onionshare.git
synced 2024-10-01 01:35:40 -04:00
Refactor directory_listing function
This commit is contained in:
parent
9805919fc7
commit
4d733c224a
@ -4,7 +4,7 @@ import tempfile
|
|||||||
import mimetypes
|
import mimetypes
|
||||||
from flask import Response, request, render_template, make_response
|
from flask import Response, request, render_template, make_response
|
||||||
|
|
||||||
from .. import strings
|
from .. import strings
|
||||||
|
|
||||||
class BaseModeWeb(object):
|
class BaseModeWeb(object):
|
||||||
"""
|
"""
|
||||||
@ -46,36 +46,31 @@ class BaseModeWeb(object):
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
def directory_listing(self, path, filenames, filesystem_path=None):
|
def directory_listing(self, path='', filenames=[], filesystem_path=None):
|
||||||
# If filesystem_path is None, this is the root directory listing
|
# If filesystem_path is None, this is the root directory listing
|
||||||
files = []
|
files = []
|
||||||
dirs = []
|
dirs = []
|
||||||
|
r = ''
|
||||||
|
|
||||||
for filename in filenames:
|
if self.web.mode == 'website':
|
||||||
if filesystem_path:
|
files, dirs = build_directory_listing(filenames)
|
||||||
this_filesystem_path = os.path.join(filesystem_path, filename)
|
|
||||||
else:
|
|
||||||
this_filesystem_path = self.files[filename]
|
|
||||||
|
|
||||||
is_dir = os.path.isdir(this_filesystem_path)
|
|
||||||
|
|
||||||
if is_dir:
|
|
||||||
dirs.append({
|
|
||||||
'basename': filename
|
|
||||||
})
|
|
||||||
else:
|
|
||||||
size = os.path.getsize(this_filesystem_path)
|
|
||||||
size_human = self.common.human_readable_filesize(size)
|
|
||||||
files.append({
|
|
||||||
'basename': filename,
|
|
||||||
'size_human': size_human
|
|
||||||
})
|
|
||||||
|
|
||||||
r = make_response(render_template('listing.html',
|
r = make_response(render_template('listing.html',
|
||||||
path=path,
|
path=path,
|
||||||
files=files,
|
files=files,
|
||||||
dirs=dirs,
|
dirs=dirs,
|
||||||
static_url_path=self.web.static_url_path))
|
static_url_path=self.web.static_url_path))
|
||||||
|
|
||||||
|
elif self.web.mode == 'share':
|
||||||
|
r = make_response(render_template(
|
||||||
|
'send.html',
|
||||||
|
file_info=self.file_info,
|
||||||
|
filename=os.path.basename(self.download_filename),
|
||||||
|
filesize=self.filesize,
|
||||||
|
filesize_human=self.common.human_readable_filesize(self.download_filesize),
|
||||||
|
is_zipped=self.is_zipped,
|
||||||
|
static_url_path=self.web.static_url_path))
|
||||||
|
|
||||||
return self.web.add_security_headers(r)
|
return self.web.add_security_headers(r)
|
||||||
|
|
||||||
|
|
||||||
|
@ -44,15 +44,8 @@ class ShareModeWeb(BaseModeWeb):
|
|||||||
else:
|
else:
|
||||||
self.filesize = self.download_filesize
|
self.filesize = self.download_filesize
|
||||||
|
|
||||||
r = make_response(render_template(
|
return self.directory_listing()
|
||||||
'send.html',
|
|
||||||
file_info=self.file_info,
|
|
||||||
filename=os.path.basename(self.download_filename),
|
|
||||||
filesize=self.filesize,
|
|
||||||
filesize_human=self.common.human_readable_filesize(self.download_filesize),
|
|
||||||
is_zipped=self.is_zipped,
|
|
||||||
static_url_path=self.web.static_url_path))
|
|
||||||
return self.web.add_security_headers(r)
|
|
||||||
|
|
||||||
@self.web.app.route("/download")
|
@self.web.app.route("/download")
|
||||||
def download():
|
def download():
|
||||||
|
@ -14,12 +14,9 @@ class WebsiteModeWeb(BaseModeWeb):
|
|||||||
"""
|
"""
|
||||||
def init(self):
|
def init(self):
|
||||||
self.common.log('WebsiteModeWeb', '__init__')
|
self.common.log('WebsiteModeWeb', '__init__')
|
||||||
|
|
||||||
# Reset assets path
|
|
||||||
self.web.app.static_folder=self.common.get_resource_path('share/static')
|
|
||||||
|
|
||||||
self.define_routes()
|
self.define_routes()
|
||||||
|
|
||||||
|
|
||||||
def define_routes(self):
|
def define_routes(self):
|
||||||
"""
|
"""
|
||||||
The web app routes for sharing a website
|
The web app routes for sharing a website
|
||||||
@ -56,6 +53,7 @@ class WebsiteModeWeb(BaseModeWeb):
|
|||||||
# Render it
|
# Render it
|
||||||
dirname = os.path.dirname(self.files[index_path])
|
dirname = os.path.dirname(self.files[index_path])
|
||||||
basename = os.path.basename(self.files[index_path])
|
basename = os.path.basename(self.files[index_path])
|
||||||
|
|
||||||
return send_from_directory(dirname, basename)
|
return send_from_directory(dirname, basename)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
@ -80,6 +78,7 @@ class WebsiteModeWeb(BaseModeWeb):
|
|||||||
return self.web.error404()
|
return self.web.error404()
|
||||||
else:
|
else:
|
||||||
# Special case loading /
|
# Special case loading /
|
||||||
|
|
||||||
if path == '':
|
if path == '':
|
||||||
index_path = 'index.html'
|
index_path = 'index.html'
|
||||||
if index_path in self.files:
|
if index_path in self.files:
|
||||||
@ -97,6 +96,28 @@ class WebsiteModeWeb(BaseModeWeb):
|
|||||||
# If the path isn't found, throw a 404
|
# If the path isn't found, throw a 404
|
||||||
return self.web.error404()
|
return self.web.error404()
|
||||||
|
|
||||||
|
def build_directory_listing(self, filenames):
|
||||||
|
for filename in filenames:
|
||||||
|
if filesystem_path:
|
||||||
|
this_filesystem_path = os.path.join(filesystem_path, filename)
|
||||||
|
else:
|
||||||
|
this_filesystem_path = self.files[filename]
|
||||||
|
|
||||||
|
is_dir = os.path.isdir(this_filesystem_path)
|
||||||
|
|
||||||
|
if is_dir:
|
||||||
|
dirs.append({
|
||||||
|
'basename': filename
|
||||||
|
})
|
||||||
|
else:
|
||||||
|
size = os.path.getsize(this_filesystem_path)
|
||||||
|
size_human = self.common.human_readable_filesize(size)
|
||||||
|
files.append({
|
||||||
|
'basename': filename,
|
||||||
|
'size_human': size_human
|
||||||
|
})
|
||||||
|
return files, dirs
|
||||||
|
|
||||||
|
|
||||||
def build_file_list(self, filenames):
|
def build_file_list(self, filenames):
|
||||||
"""
|
"""
|
||||||
|
Loading…
Reference in New Issue
Block a user