From 9805919fc7ec395da19979b20226c1de037c7969 Mon Sep 17 00:00:00 2001 From: hiro Date: Thu, 13 Jun 2019 12:41:12 +0200 Subject: [PATCH] Move directory_listing function --- onionshare/web/base_mode.py | 36 +++++++++++++++++++++++++++++++++- onionshare/web/website_mode.py | 33 +------------------------------ 2 files changed, 36 insertions(+), 33 deletions(-) diff --git a/onionshare/web/base_mode.py b/onionshare/web/base_mode.py index 8843d198..46e63e1a 100644 --- a/onionshare/web/base_mode.py +++ b/onionshare/web/base_mode.py @@ -4,7 +4,7 @@ import tempfile import mimetypes from flask import Response, request, render_template, make_response -from .. import strings + from .. import strings class BaseModeWeb(object): """ @@ -45,6 +45,40 @@ class BaseModeWeb(object): """ pass + + def directory_listing(self, path, filenames, filesystem_path=None): + # If filesystem_path is None, this is the root directory listing + files = [] + dirs = [] + + 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 + }) + + r = make_response(render_template('listing.html', + path=path, + files=files, + dirs=dirs, + static_url_path=self.web.static_url_path)) + return self.web.add_security_headers(r) + + def set_file_info(self, filenames, processed_size_callback=None): """ Build a data structure that describes the list of files diff --git a/onionshare/web/website_mode.py b/onionshare/web/website_mode.py index 4c024849..287acbd9 100644 --- a/onionshare/web/website_mode.py +++ b/onionshare/web/website_mode.py @@ -97,38 +97,7 @@ class WebsiteModeWeb(BaseModeWeb): # If the path isn't found, throw a 404 return self.web.error404() - def directory_listing(self, path, filenames, filesystem_path=None): - # If filesystem_path is None, this is the root directory listing - files = [] - dirs = [] - - 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 - }) - - r = make_response(render_template('listing.html', - path=path, - files=files, - dirs=dirs, - static_url_path=self.web.static_url_path)) - return self.web.add_security_headers(r) - + def build_file_list(self, filenames): """ Build a data structure that describes the list of files that make up