2020-08-27 19:13:08 -04:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
"""
|
|
|
|
OnionShare | https://onionshare.org/
|
|
|
|
|
2021-02-22 16:35:14 -05:00
|
|
|
Copyright (C) 2014-2021 Micah Lee, et al. <micah@micahflee.com>
|
2020-08-27 19:13:08 -04:00
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
"""
|
|
|
|
|
2019-04-19 08:25:42 -04:00
|
|
|
import os
|
2021-03-10 03:40:06 -05:00
|
|
|
from flask import render_template, make_response
|
2019-04-19 08:25:42 -04:00
|
|
|
|
2019-09-01 18:05:53 -04:00
|
|
|
from .send_base_mode import SendBaseModeWeb
|
2019-04-19 08:25:42 -04:00
|
|
|
|
|
|
|
|
2019-09-01 18:05:53 -04:00
|
|
|
class WebsiteModeWeb(SendBaseModeWeb):
|
2019-04-19 08:25:42 -04:00
|
|
|
"""
|
2019-06-13 06:33:34 -04:00
|
|
|
All of the web logic for website mode
|
2019-04-19 08:25:42 -04:00
|
|
|
"""
|
2019-10-13 00:01:25 -04:00
|
|
|
|
2019-09-01 23:36:30 -04:00
|
|
|
def init(self):
|
2019-09-03 23:52:49 -04:00
|
|
|
pass
|
2019-09-01 23:36:30 -04:00
|
|
|
|
2019-04-19 08:25:42 -04:00
|
|
|
def define_routes(self):
|
|
|
|
"""
|
|
|
|
The web app routes for sharing a website
|
|
|
|
"""
|
2019-10-13 00:01:25 -04:00
|
|
|
|
2021-05-10 18:14:49 -04:00
|
|
|
@self.web.app.route("/", defaults={"path": ""}, methods=["GET"], provide_automatic_options=False)
|
|
|
|
@self.web.app.route("/<path:path>", methods=["GET"], provide_automatic_options=False)
|
2019-05-10 17:52:07 -04:00
|
|
|
def path_public(path):
|
|
|
|
return path_logic(path)
|
2019-04-19 08:25:42 -04:00
|
|
|
|
2019-10-13 00:01:25 -04:00
|
|
|
def path_logic(path=""):
|
2019-04-19 08:25:42 -04:00
|
|
|
"""
|
|
|
|
Render the onionshare website.
|
|
|
|
"""
|
2019-06-14 12:21:12 -04:00
|
|
|
return self.render_logic(path)
|
2019-09-01 18:44:44 -04:00
|
|
|
|
2019-10-13 00:01:25 -04:00
|
|
|
def directory_listing_template(
|
|
|
|
self, path, files, dirs, breadcrumbs, breadcrumbs_leaf
|
|
|
|
):
|
|
|
|
return make_response(
|
|
|
|
render_template(
|
|
|
|
"listing.html",
|
|
|
|
path=path,
|
|
|
|
files=files,
|
|
|
|
dirs=dirs,
|
|
|
|
breadcrumbs=breadcrumbs,
|
|
|
|
breadcrumbs_leaf=breadcrumbs_leaf,
|
|
|
|
static_url_path=self.web.static_url_path,
|
2021-04-12 18:15:51 -04:00
|
|
|
title=self.web.settings.get("general", "title"),
|
2019-10-13 00:01:25 -04:00
|
|
|
)
|
|
|
|
)
|
2019-09-01 18:44:44 -04:00
|
|
|
|
|
|
|
def set_file_info_custom(self, filenames, processed_size_callback):
|
|
|
|
self.common.log("WebsiteModeWeb", "set_file_info_custom")
|
|
|
|
self.web.cancel_compression = True
|
|
|
|
|
2019-10-13 00:01:25 -04:00
|
|
|
def render_logic(self, path=""):
|
2020-11-23 17:52:52 -05:00
|
|
|
# Strip trailing slash
|
|
|
|
path = path.rstrip("/")
|
|
|
|
|
2019-09-01 18:44:44 -04:00
|
|
|
if path in self.files:
|
|
|
|
filesystem_path = self.files[path]
|
|
|
|
|
|
|
|
# If it's a directory
|
|
|
|
if os.path.isdir(filesystem_path):
|
|
|
|
# Is there an index.html?
|
2019-10-13 00:01:25 -04:00
|
|
|
index_path = os.path.join(path, "index.html")
|
2019-09-01 18:44:44 -04:00
|
|
|
if index_path in self.files:
|
|
|
|
# Render it
|
2019-09-15 19:23:19 -04:00
|
|
|
return self.stream_individual_file(self.files[index_path])
|
2019-09-01 18:44:44 -04:00
|
|
|
|
|
|
|
else:
|
|
|
|
# Otherwise, render directory listing
|
|
|
|
filenames = []
|
|
|
|
for filename in os.listdir(filesystem_path):
|
2020-11-23 17:52:52 -05:00
|
|
|
filenames.append(filename)
|
2019-09-01 18:44:44 -04:00
|
|
|
filenames.sort()
|
|
|
|
return self.directory_listing(filenames, path, filesystem_path)
|
|
|
|
|
|
|
|
# If it's a file
|
|
|
|
elif os.path.isfile(filesystem_path):
|
2019-09-02 22:45:14 -04:00
|
|
|
return self.stream_individual_file(filesystem_path)
|
2019-09-01 18:44:44 -04:00
|
|
|
|
|
|
|
# If it's not a directory or file, throw a 404
|
|
|
|
else:
|
2019-09-09 03:22:18 -04:00
|
|
|
history_id = self.cur_history_id
|
|
|
|
self.cur_history_id += 1
|
|
|
|
return self.web.error404(history_id)
|
2019-09-01 18:44:44 -04:00
|
|
|
else:
|
|
|
|
# Special case loading /
|
|
|
|
|
2019-10-13 00:01:25 -04:00
|
|
|
if path == "":
|
|
|
|
index_path = "index.html"
|
2019-09-01 18:44:44 -04:00
|
|
|
if index_path in self.files:
|
|
|
|
# Render it
|
2019-09-02 22:45:14 -04:00
|
|
|
return self.stream_individual_file(self.files[index_path])
|
2019-09-01 18:44:44 -04:00
|
|
|
else:
|
|
|
|
# Root directory listing
|
|
|
|
filenames = list(self.root_files)
|
|
|
|
filenames.sort()
|
2019-09-01 19:02:10 -04:00
|
|
|
return self.directory_listing(filenames, path)
|
2019-09-01 18:44:44 -04:00
|
|
|
|
|
|
|
else:
|
|
|
|
# If the path isn't found, throw a 404
|
2019-09-09 03:22:18 -04:00
|
|
|
history_id = self.cur_history_id
|
|
|
|
self.cur_history_id += 1
|
|
|
|
return self.web.error404(history_id)
|