1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
|
# files.altafcreator.com is a READ-ONLY website of files I want to share.
# This code is not safe for user-uploaded files.
import fastapi
from fastapi.middleware.cors import CORSMiddleware
import fastapi.staticfiles
import fastapi.responses
from pathlib import Path
import html
from bs4 import BeautifulSoup
import datetime
from dotenv import load_dotenv
from os import getenv
import markdown_renderer as md # markdown_renderer.py
import requests
print("Hello, world!")
load_dotenv()
FOLDER_PATH = Path(getenv("FOLDER_PATH")).resolve()
# FOLDER_PATH = "/var/files"
app = fastapi.FastAPI(title="altaf-files", decscription="altaf-files backend", version="1.0")
origins = [
"http://localhost",
"http://localhost:9092",
"https://files.altafcreator.com",
]
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
HTML_FOLDER_TEMPLATE = ""
with open("index.html") as f:
HTML_FOLDER_TEMPLATE = f.read()
@app.middleware("http")
async def force_trailing_slash(request: fastapi.Request, call_next):
path = request.url.path
if not path.endswith("/") and not Path(path).suffix:
new_url = str(request.url.replace(path=path + "/"))
return fastapi.responses.RedirectResponse(url=new_url, status_code=301)
response = await call_next(request)
return response
@app.get("/{path:path}")
def folder(path: str):
target_path = (FOLDER_PATH / path).resolve()
if not target_path.is_relative_to(FOLDER_PATH):
return fastapi.responses.Response(status_code=403, content="Access denied.")
if target_path.is_file():
if target_path.suffix == ".md":
return fastapi.responses.Response(content=markdown(absolute_path=target_path, url_path=path), media_type="text/html", status_code=200)
if target_path.suffix == ".nextcloudmd":
content = ""
with target_path.open(mode="r") as f:
content = f.read()
r = requests.get(content.rstrip(), stream=True)
return fastapi.responses.Response(content=markdown(mdstr=r.text, url_path=path), media_type="text/html", status_code=200)
return fastapi.responses.FileResponse(target_path)
elif not target_path.is_dir():
return fastapi.responses.Response(status_code=404)
files = []
directories = []
for item in sorted(target_path.iterdir()):
if (target_path / item.name).resolve().is_file():
files.append(item.name)
else:
directories.append(item.name)
soup = BeautifulSoup(HTML_FOLDER_TEMPLATE, "html.parser")
for d in directories:
add_item_html(d, path, True, soup)
for f in files:
add_item_html(f, path, False, soup)
render_breadcrumb(soup, path)
soup.select_one("#file-content-section").decompose()
return fastapi.responses.Response(content=str(soup), media_type="text/html", status_code=200)
def markdown(mdstr: str = None, absolute_path: Path = None, url_path: str = None):
content = ""
if absolute_path:
with absolute_path.open(mode="r") as f:
content = f.read()
else:
content = mdstr
_, mdhtml = md.md_to_html(content)
soup = BeautifulSoup(HTML_FOLDER_TEMPLATE, "html.parser")
soup.select_one("#list-container-section").decompose()
md_soup = BeautifulSoup(mdhtml, "html.parser")
soup.select_one("#file-content").append(md_soup)
render_breadcrumb(soup, url_path)
return str(soup)
def add_item_html(child_path: str, current_path: str, is_directory: bool, soup: BeautifulSoup) -> None:
href_path = ""
if current_path == "":
href_path = f"/{child_path}"
else:
href_path = f"/{current_path.rstrip('/')}/{child_path}"
symbol = "📄"
if is_directory:
symbol = "📂"
absolute_path = Path(f"{str(FOLDER_PATH).rstrip('/')}/{current_path.rstrip('/')}/{child_path}").resolve()
if not absolute_path.exists():
return ""
size = "—"
if not is_directory:
size = format_size(absolute_path.stat().st_size)
date_modified = str(datetime.datetime.fromtimestamp(absolute_path.stat().st_mtime))[:10].replace("-", "/")
new_btn = soup.new_tag("a")
new_btn["href"] = href_path
new_div = soup.new_tag("div")
new_btn.append(new_div)
new_span_name = soup.new_tag("span")
new_span_name.string = f"{symbol} {child_path}"
new_div.append(new_span_name)
new_span_date = soup.new_tag("span")
new_span_date["class"] = "list-metadata list-date"
new_div.append(new_span_date)
new_span_date.string = date_modified
new_span_size = soup.new_tag("span")
new_span_size["class"] = "list-metadata"
new_span_size.string = size
new_div.append(new_span_size)
soup.select_one("#list-container").append(new_btn)
def render_breadcrumb(soup: BeautifulSoup, path: str) -> None:
local_path_list = path.split("/")
home_btn = soup.new_tag("a")
home_btn["class"] = "link"
home_btn["href"] = "/"
home_btn.string = "🏠 altaf-files"
soup.select_one("#breadcrumb-container").append(home_btn)
for i in range(len(local_path_list)):
new_sep = soup.new_tag("span")
new_sep.string = "/"
soup.select_one("#breadcrumb-container").append(new_sep)
new_btn = soup.new_tag("a")
new_btn["class"] = "link"
new_btn["href"] = "/" + "/".join(local_path_list[0:i + 1])
new_btn.string = local_path_list[i]
soup.select_one("#breadcrumb-container").append(new_btn)
def format_size(byte_size):
for unit in ['B', 'KiB', 'MiB', 'GiB', 'TiB']:
if byte_size < 1024.0:
return f"{round(byte_size, 1)} {unit}"
byte_size /= 1024.0
|