-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
323 lines (256 loc) · 9.96 KB
/
main.py
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
import json
import typing
import base64
import io
import fastapi
import fastapi.responses
import fastapi.staticfiles
import fastapi.templating
import starlette.middleware.sessions
import PIL.Image
import core
import core.article
import core.user
import core.utils
import core.settings
import core.editor_data
app = fastapi.FastAPI()
templates = fastapi.templating.Jinja2Templates(directory = "templates", autoescape = False)
app.mount("/static", fastapi.staticfiles.StaticFiles(directory = "static"), name = "static")
app.add_middleware(
starlette.middleware.sessions.SessionMiddleware,
secret_key = core.settings.instance.secret_key
)
@app.get("/view", response_class = fastapi.responses.RedirectResponse)
@app.get("/view/", response_class = fastapi.responses.RedirectResponse)
@app.get("/view/{title}", response_class = fastapi.responses.HTMLResponse)
def view(request: fastapi.Request, title: typing.Optional[str] = None):
if title == None:
return fastapi.responses.RedirectResponse("/", status_code = 303)
article = core.article.Article(title)
article.load()
article.render_markdown()
user, username = core.user.get_user_from_request(request)
is_editable = article.is_accessable('edit', user)
is_deletable = article.is_accessable('delete', user)
is_movable = article.is_accessable('move', user)
context: dict[str, typing.Any] = {
'request': request,
'title': title,
'article': article,
'settings': core.settings.instance,
'user': user,
'is_editable': is_editable,
'is_deletable': is_deletable,
'is_movable': is_movable
}
response = templates.TemplateResponse(f"{core.settings.instance.skin}/view.html", context)
return response
@app.get("/source", response_class = fastapi.responses.RedirectResponse)
@app.get("/source/", response_class = fastapi.responses.RedirectResponse)
@app.get("/source/{title}", response_class = fastapi.responses.HTMLResponse)
def view_source(request: fastapi.Request, title: typing.Optional[str] = None):
if title == None:
return fastapi.responses.RedirectResponse("/", status_code = 303)
article = core.article.Article(title)
article.load()
user, username = core.user.get_user_from_request(request)
context: dict[str, typing.Any] = {
'request': request,
'title': f"View source for {title}",
'article': article,
'settings': core.settings.instance,
'user': user,
'editor_data': core.editor_data.instance
}
response = templates.TemplateResponse(f"{core.settings.instance.skin}/source.html", context)
return response
@app.get("/edit", response_class = fastapi.responses.RedirectResponse)
@app.get("/edit/", response_class = fastapi.responses.RedirectResponse)
@app.get("/edit/{title}", response_class = fastapi.responses.HTMLResponse)
def edit(request: fastapi.Request, title: typing.Optional[str] = None):
if title == None:
return fastapi.responses.RedirectResponse("/", status_code = 303)
article = core.article.Article(title)
article.load()
user, username = core.user.get_user_from_request(request)
is_editable = article.is_accessable('edit', user)
if not is_editable:
return fastapi.responses.RedirectResponse(f"/source/{title}")
context: dict[str, typing.Any] = {
'request': request,
'title': f"Edit {title}",
'article': article,
'settings': core.settings.instance,
'user': user,
'editor_data': core.editor_data.instance
}
response = templates.TemplateResponse(f"{core.settings.instance.skin}/edit.html", context)
return response
@app.get("/edit-save", response_class = fastapi.responses.RedirectResponse)
@app.get("/edit-save/", response_class = fastapi.responses.RedirectResponse)
@app.post("/edit-save/{title}", response_class = fastapi.responses.RedirectResponse, status_code = 303)
def edit_save(
request: fastapi.Request,
title: typing.Optional[str] = None,
raw_data: str = fastapi.Form(None)
):
if title == None:
return fastapi.responses.RedirectResponse("/", status_code = 303)
user, username = core.user.get_user_from_request(request)
article = core.article.Article(title)
article.load()
is_editable = article.is_accessable('edit', user)
if is_editable:
if raw_data is None:
raw_data = ""
raw_data = raw_data.replace('\r', '').strip()
if not article.existence or raw_data != article.raw_data:
article.raw_data = raw_data
article.save(username)
return f"/view/{title}"
@app.get("/delete", response_class = fastapi.responses.RedirectResponse)
@app.get("/delete/", response_class = fastapi.responses.RedirectResponse)
@app.get("/delete/{title}", response_class = fastapi.responses.RedirectResponse, status_code = 303)
def delete(request: fastapi.Request, title: typing.Optional[str] = None):
if title == None:
return fastapi.responses.RedirectResponse("/", status_code = 303)
user, username = core.user.get_user_from_request(request)
article = core.article.Article(title)
is_deletable = article.is_accessable('delete', user)
if is_deletable:
article.delete()
return f"/view/{title}"
@app.get("/search-go/", response_class = fastapi.responses.RedirectResponse)
def search_go(request: fastapi.Request, search_text: str = ""):
if core.article.Article.find_article(search_text):
return fastapi.responses.RedirectResponse(f"/view/{search_text}", status_code = 303)
return fastapi.responses.RedirectResponse(f"/search/?query={search_text}", status_code = 303)
@app.get("/search", response_class = fastapi.responses.HTMLResponse)
@app.get("/search/", response_class = fastapi.responses.HTMLResponse)
def search(request: fastapi.Request, query: str = ""):
result = core.article.Article.search_files(query)
context: dict[str, typing.Any] = {
'request': request,
'settings': core.settings.instance,
'title' : f"Search for {query}",
'result': result,
'text': query,
'article_existence': core.article.Article.find_article(query),
}
response = templates.TemplateResponse(f"{core.settings.instance.skin}/search.html", context)
return response
@app.get("/is_user_exist/", response_class = fastapi.responses.HTMLResponse)
def is_user_exist(request: fastapi.Request, username: str):
user = core.user.User(username)
result = dict()
result['is_user_exist'] = user.is_user_exist()
return json.dumps(result)
@app.get("/can_login/", response_class = fastapi.responses.HTMLResponse)
def can_login(request: fastapi.Request, username: str, hash_a: str, hash_b: str):
user = core.user.User(username)
result = dict()
result['can_login'] = user.can_login(hash_a, hash_b)
return json.dumps(result)
@app.get("/login/", response_class = fastapi.responses.HTMLResponse)
def login(request: fastapi.Request):
context: dict[str, typing.Any] = {
'request': request,
'settings': core.settings.instance,
'editor_data': core.editor_data.instance
}
if 'username' in request.session:
redirect_url = "/"
return fastapi.responses.RedirectResponse(redirect_url, status_code = 303)
if 'referer' in request.headers:
http_referer = request.headers['referer']
request.session['referer'] = http_referer
response = templates.TemplateResponse(f"{core.settings.instance.skin}/login.html", context)
return response
@app.post("/login-submit/", response_class = fastapi.responses.RedirectResponse, status_code = 303)
def login_submit(
response: fastapi.Response,
request: fastapi.Request,
username: str = fastapi.Form(),
password: str = fastapi.Form(),
hash_a: str = fastapi.Form(),
hash_b: str = fastapi.Form(),
):
user = core.user.User(username)
result = user.login(hash_a, hash_b, request)
redirect_url = request.session['referer'] if result else "/login/"
if result:
if not redirect_url:
return "/"
request.session.pop('referer', None)
else:
print(request.session['referer'])
return redirect_url
@app.get("/logout/", response_class = fastapi.responses.RedirectResponse, status_code = 303)
def logout(request: fastapi.Request):
redirect_url = request.headers['referer']
request.session.pop('username', None)
request.session.pop('is_admin', None)
if not redirect_url:
return "/"
return redirect_url
@app.get("/join/", response_class = fastapi.responses.HTMLResponse)
def join(request: fastapi.Request):
user, username = core.user.get_user_from_request(request)
context: dict[str, typing.Any] = {
'request': request,
'title': "Join",
'settings': core.settings.instance,
'user': user,
}
if 'username' in request.session:
return fastapi.responses.RedirectResponse("/", status_code = 303)
response = templates.TemplateResponse(f"{core.settings.instance.skin}/join.html", context)
return response
@app.post("/join-submit/", response_class = fastapi.responses.RedirectResponse, status_code = 303)
def join_submit(
request: fastapi.Request,
username: str = fastapi.Form(),
password: str = fastapi.Form(),
confirm_password: str = fastapi.Form(),
hash_a: str = fastapi.Form(),
hash_b: str = fastapi.Form(),
email: str = fastapi.Form()
):
user = core.user.User(username, hash_a, hash_b, email)
join_result = user.join()
if join_result:
return "/login/"
return "/join/"
@app.post("/compress-image/", response_class = fastapi.responses.HTMLResponse)
def compress_image(request: fastapi.Request, image_file: fastapi.UploadFile = fastapi.Form()):
format: str
animated: bool = False
match image_file.content_type:
case "image/apng" | "image/gif" | "image/png" | "image/webp":
format = "webp"
case "image/jpeg":
format = "jpeg"
case "image/svg+xml":
format = "svg+xml"
case _:
return ""
if format == "svg+xml":
image_data = image_file.file.read()
else:
image = PIL.Image.open(image_file.file)
buffer = io.BytesIO()
ext = image.format
if "duration" in image.info:
image.save(buffer, format = format, save_all = True, duration = image.info['duration'])
else:
image.save(buffer, format = format)
image_data = buffer.getvalue()
image_str = f"data:image/{format};base64," + base64.b64encode(image_data).decode("utf-8")
result = dict()
result['compressed_image'] = image_str
return json.dumps(result)
@app.get("/", response_class = fastapi.responses.RedirectResponse, status_code = 308)
def root(request: fastapi.Request):
title = core.settings.instance.mainpage
return f"/view/{title}"