diff options
author | Micah Lee <micah@micahflee.com> | 2020-11-23 14:52:52 -0800 |
---|---|---|
committer | Micah Lee <micah@micahflee.com> | 2020-11-23 14:52:52 -0800 |
commit | abca27cb5841783b298479b3a037f97b89b7ead7 (patch) | |
tree | 608c69e712e36d819c16d5648d381a16c05211c3 /cli | |
parent | e8419e660cf293e43ce70763d75408ed669bcd97 (diff) | |
download | onionshare-abca27cb5841783b298479b3a037f97b89b7ead7.tar.gz onionshare-abca27cb5841783b298479b3a037f97b89b7ead7.zip |
Allow directory listing work with or without trailing slash (removing trailing slash by default), and make directory listing links absolute instead of relative
Diffstat (limited to 'cli')
-rw-r--r-- | cli/onionshare_cli/resources/templates/listing.html | 110 | ||||
-rw-r--r-- | cli/onionshare_cli/web/send_base_mode.py | 27 | ||||
-rw-r--r-- | cli/onionshare_cli/web/website_mode.py | 8 |
3 files changed, 80 insertions, 65 deletions
diff --git a/cli/onionshare_cli/resources/templates/listing.html b/cli/onionshare_cli/resources/templates/listing.html index ea050710..e1dd6d9f 100644 --- a/cli/onionshare_cli/resources/templates/listing.html +++ b/cli/onionshare_cli/resources/templates/listing.html @@ -1,55 +1,59 @@ <!DOCTYPE html> <html> - <head> - <title>OnionShare</title> - <meta charset="utf-8" /> - <meta name="viewport" content="width=device-width, initial-scale=1"> - <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" /> - </head> - <body> - - <header class="clearfix"> - <img class="logo" src="{{ static_url_path }}/img/logo.png" title="OnionShare"> - <h1>OnionShare</h1> - </header> - - {% if breadcrumbs %} - <ul class="breadcrumbs"> - {% for breadcrumb in breadcrumbs %}<li><a href="{{ breadcrumb[1] }}">{{ breadcrumb[0] }}</a> <span class="sep">‣</span></li>{% endfor %}<li>{{ breadcrumbs_leaf }}</li> - </ul> - {% endif %} - - <table class="file-list" id="file-list"> - <tr> - <th id="filename-header">Filename</th> - <th id="size-header">Size</th> - <th></th> - </tr> - - {% for info in dirs %} - <tr> - <td> - <img width="30" height="30" title="" alt="" src="{{ static_url_path }}/img/web_folder.png" /> - <a href="{{ info.basename }}"> - {{ info.basename }} - </a> - </td> - <td>—</td> - </tr> - {% endfor %} - - {% for info in files %} - <tr> - <td> - <img width="30" height="30" title="" alt="" src="{{ static_url_path }}/img/web_file.png" /> - <a href="{{ info.basename }}"> - {{ info.basename }} - </a> - </td> - <td>{{ info.size_human }}</td> - </tr> - {% endfor %} - </table> - </body> -</html> + +<head> + <title>OnionShare</title> + <meta charset="utf-8" /> + <meta name="viewport" content="width=device-width, initial-scale=1"> + <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" /> +</head> + +<body> + + <header class="clearfix"> + <img class="logo" src="{{ static_url_path }}/img/logo.png" title="OnionShare"> + <h1>OnionShare</h1> + </header> + + {% if breadcrumbs %} + <ul class="breadcrumbs"> + {% for breadcrumb in breadcrumbs %}<li><a href="{{ breadcrumb[1] }}">{{ breadcrumb[0] }}</a> <span + class="sep">‣</span></li>{% endfor %}<li>{{ breadcrumbs_leaf }}</li> + </ul> + {% endif %} + + <table class="file-list" id="file-list"> + <tr> + <th id="filename-header">Filename</th> + <th id="size-header">Size</th> + <th></th> + </tr> + + {% for info in dirs %} + <tr> + <td> + <img width="30" height="30" title="" alt="" src="{{ static_url_path }}/img/web_folder.png" /> + <a href="{{ info.link }}"> + {{ info.basename }} + </a> + </td> + <td>—</td> + </tr> + {% endfor %} + + {% for info in files %} + <tr> + <td> + <img width="30" height="30" title="" alt="" src="{{ static_url_path }}/img/web_file.png" /> + <a href="{{ info.link }}"> + {{ info.basename }} + </a> + </td> + <td>{{ info.size_human }}</td> + </tr> + {% endfor %} + </table> +</body> + +</html>
\ No newline at end of file diff --git a/cli/onionshare_cli/web/send_base_mode.py b/cli/onionshare_cli/web/send_base_mode.py index f9db28c6..9913996e 100644 --- a/cli/onionshare_cli/web/send_base_mode.py +++ b/cli/onionshare_cli/web/send_base_mode.py @@ -85,7 +85,7 @@ class SendBaseModeWeb: # If it's a directory, add it recursively elif os.path.isdir(filename): - self.root_files[basename + "/"] = filename + self.root_files[basename] = filename for root, _, nested_filenames in os.walk(filename): # Normalize the root path. So if the directory name is "/home/user/Documents/some_folder", @@ -96,7 +96,7 @@ class SendBaseModeWeb: ).rstrip("/") # Add the dir itself - self.files[normalized_root + "/"] = root + self.files[normalized_root] = root # Add the files in this dir for nested_filename in nested_filenames: @@ -117,19 +117,21 @@ class SendBaseModeWeb: ) breadcrumbs = [("☗", "/")] - parts = path.split("/")[:-1] + parts = path.split("/") + if parts[-1] == "": + parts = parts[:-1] 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] # 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( path, files, dirs, breadcrumbs, breadcrumbs_leaf ) 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 = [] dirs = [] @@ -142,11 +144,20 @@ class SendBaseModeWeb: is_dir = os.path.isdir(this_filesystem_path) if is_dir: - dirs.append({"basename": filename}) + dirs.append( + {"link": os.path.join(f"/{path}", filename), "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}) + files.append( + { + "link": os.path.join(f"/{path}", filename), + "basename": filename, + "size_human": size_human, + } + ) + return files, dirs def stream_individual_file(self, filesystem_path): diff --git a/cli/onionshare_cli/web/website_mode.py b/cli/onionshare_cli/web/website_mode.py index ef37ab12..352f4f3c 100644 --- a/cli/onionshare_cli/web/website_mode.py +++ b/cli/onionshare_cli/web/website_mode.py @@ -71,6 +71,9 @@ class WebsiteModeWeb(SendBaseModeWeb): self.web.cancel_compression = True def render_logic(self, path=""): + # Strip trailing slash + path = path.rstrip("/") + if path in self.files: filesystem_path = self.files[path] @@ -86,10 +89,7 @@ class WebsiteModeWeb(SendBaseModeWeb): # Otherwise, render directory listing filenames = [] 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() return self.directory_listing(filenames, path, filesystem_path) |