Merge pull request #1230 from micahflee/1053_website_folders

Allow directory listing work to with or without trailing slash
This commit is contained in:
Micah Lee 2020-11-27 14:19:18 -08:00 committed by GitHub
commit 3b06d0a08a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 75 additions and 60 deletions

View File

@ -1,5 +1,6 @@
<!DOCTYPE html> <!DOCTYPE html>
<html> <html>
<head> <head>
<title>OnionShare</title> <title>OnionShare</title>
<meta charset="utf-8" /> <meta charset="utf-8" />
@ -7,6 +8,7 @@
<link href="{{ static_url_path }}/img/favicon.ico" rel="icon" type="image/x-icon" /> <link href="{{ static_url_path }}/img/favicon.ico" rel="icon" type="image/x-icon" />
<link href="{{ static_url_path }}/css/style.css" rel="stylesheet" type="text/css" /> <link href="{{ static_url_path }}/css/style.css" rel="stylesheet" type="text/css" />
</head> </head>
<body> <body>
<header class="clearfix"> <header class="clearfix">
@ -16,7 +18,8 @@
{% if breadcrumbs %} {% if breadcrumbs %}
<ul class="breadcrumbs"> <ul class="breadcrumbs">
{% for breadcrumb in breadcrumbs %}<li><a href="{{ breadcrumb[1] }}">{{ breadcrumb[0] }}</a> <span class="sep">&#8227;</span></li>{% endfor %}<li>{{ breadcrumbs_leaf }}</li> {% for breadcrumb in breadcrumbs %}<li><a href="{{ breadcrumb[1] }}">{{ breadcrumb[0] }}</a> <span
class="sep">&#8227;</span></li>{% endfor %}<li>{{ breadcrumbs_leaf }}</li>
</ul> </ul>
{% endif %} {% endif %}
@ -31,7 +34,7 @@
<tr> <tr>
<td> <td>
<img width="30" height="30" title="" alt="" src="{{ static_url_path }}/img/web_folder.png" /> <img width="30" height="30" title="" alt="" src="{{ static_url_path }}/img/web_folder.png" />
<a href="{{ info.basename }}"> <a href="{{ info.link }}">
{{ info.basename }} {{ info.basename }}
</a> </a>
</td> </td>
@ -43,7 +46,7 @@
<tr> <tr>
<td> <td>
<img width="30" height="30" title="" alt="" src="{{ static_url_path }}/img/web_file.png" /> <img width="30" height="30" title="" alt="" src="{{ static_url_path }}/img/web_file.png" />
<a href="{{ info.basename }}"> <a href="{{ info.link }}">
{{ info.basename }} {{ info.basename }}
</a> </a>
</td> </td>
@ -52,4 +55,5 @@
{% endfor %} {% endfor %}
</table> </table>
</body> </body>
</html> </html>

View File

@ -85,7 +85,7 @@ class SendBaseModeWeb:
# If it's a directory, add it recursively # If it's a directory, add it recursively
elif os.path.isdir(filename): elif os.path.isdir(filename):
self.root_files[basename + "/"] = filename self.root_files[basename] = filename
for root, _, nested_filenames in os.walk(filename): for root, _, nested_filenames in os.walk(filename):
# Normalize the root path. So if the directory name is "/home/user/Documents/some_folder", # Normalize the root path. So if the directory name is "/home/user/Documents/some_folder",
@ -96,7 +96,7 @@ class SendBaseModeWeb:
).rstrip("/") ).rstrip("/")
# Add the dir itself # Add the dir itself
self.files[normalized_root + "/"] = root self.files[normalized_root] = root
# Add the files in this dir # Add the files in this dir
for nested_filename in nested_filenames: for nested_filename in nested_filenames:
@ -117,19 +117,21 @@ class SendBaseModeWeb:
) )
breadcrumbs = [("", "/")] breadcrumbs = [("", "/")]
parts = path.split("/")[:-1] parts = path.split("/")
if parts[-1] == "":
parts = parts[:-1]
for i in range(len(parts)): for i in range(len(parts)):
breadcrumbs.append((parts[i], f"/{'/'.join(parts[0 : i + 1])}/")) breadcrumbs.append((parts[i], f"/{'/'.join(parts[0 : i + 1])}"))
breadcrumbs_leaf = breadcrumbs.pop()[0] breadcrumbs_leaf = breadcrumbs.pop()[0]
# If filesystem_path is None, this is the root directory listing # If filesystem_path is None, this is the root directory listing
files, dirs = self.build_directory_listing(filenames, filesystem_path) files, dirs = self.build_directory_listing(path, filenames, filesystem_path)
r = self.directory_listing_template( r = self.directory_listing_template(
path, files, dirs, breadcrumbs, breadcrumbs_leaf path, files, dirs, breadcrumbs, breadcrumbs_leaf
) )
return self.web.add_security_headers(r) return self.web.add_security_headers(r)
def build_directory_listing(self, filenames, filesystem_path): def build_directory_listing(self, path, filenames, filesystem_path):
files = [] files = []
dirs = [] dirs = []
@ -142,11 +144,20 @@ class SendBaseModeWeb:
is_dir = os.path.isdir(this_filesystem_path) is_dir = os.path.isdir(this_filesystem_path)
if is_dir: if is_dir:
dirs.append({"basename": filename}) dirs.append(
{"link": os.path.join(f"/{path}", filename), "basename": filename}
)
else: else:
size = os.path.getsize(this_filesystem_path) size = os.path.getsize(this_filesystem_path)
size_human = self.common.human_readable_filesize(size) size_human = self.common.human_readable_filesize(size)
files.append({"basename": filename, "size_human": size_human}) files.append(
{
"link": os.path.join(f"/{path}", filename),
"basename": filename,
"size_human": size_human,
}
)
return files, dirs return files, dirs
def stream_individual_file(self, filesystem_path): def stream_individual_file(self, filesystem_path):

View File

@ -71,6 +71,9 @@ class WebsiteModeWeb(SendBaseModeWeb):
self.web.cancel_compression = True self.web.cancel_compression = True
def render_logic(self, path=""): def render_logic(self, path=""):
# Strip trailing slash
path = path.rstrip("/")
if path in self.files: if path in self.files:
filesystem_path = self.files[path] filesystem_path = self.files[path]
@ -86,9 +89,6 @@ class WebsiteModeWeb(SendBaseModeWeb):
# Otherwise, render directory listing # Otherwise, render directory listing
filenames = [] filenames = []
for filename in os.listdir(filesystem_path): for filename in os.listdir(filesystem_path):
if os.path.isdir(os.path.join(filesystem_path, filename)):
filenames.append(filename + "/")
else:
filenames.append(filename) filenames.append(filename)
filenames.sort() filenames.sort()
return self.directory_listing(filenames, path, filesystem_path) return self.directory_listing(filenames, path, filesystem_path)