-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
77 lines (58 loc) · 2.2 KB
/
server.py
File metadata and controls
77 lines (58 loc) · 2.2 KB
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
from aiohttp import web
from pysafe.config import get_upload_limit, init_config, get_config
from pysafe.tables import new_file, SQLBase, find_file
import aiohttp_sqlalchemy
from asynctor import host_single
from pathlib import Path
import asyncio
import sys
route = web.RouteTableDef()
route.static("/static", "static")
@route.post("/upload")
async def upload_file(request:web.Request):
multipart = await request.multipart()
file = None
while part := await multipart.next():
if part.name == "file":
file = await new_file(request, part, part.filename, get_upload_limit(request.app))
assert file is not None
return web.Response(body=file.url_response().encode(), status=200, content_type="text/html")
@route.get("/uploads/{name}")
async def download_file(request:web.Request):
return await find_file(request, request.match_info["name"])
@route.get("/")
async def home(request:web.Request):
return web.HTTPFound("/static/home.html")
async def app_factory():
app = web.Application()
init_config(app)
config = get_config(app)
if not config.path.exists():
print("making default directory under (uploads) is this what you wanted??")
default_path = Path("uploads")
default_path.mkdir()
config.path = default_path
aiohttp_sqlalchemy.setup(
app,
[
aiohttp_sqlalchemy.bind("sqlite+aiosqlite:///uploads.db"),
],
)
app.add_routes(route)
await aiohttp_sqlalchemy.init_db(app, SQLBase.metadata)
if config.tor:
async with host_single(ctrl_port=9051, server_port=config.port, onion_launched_callback=print):
await web._run_app(app, port=config.port)
else:
await web._run_app(app, port=config.port)
if __name__ == "__main__":
try:
if sys.platform in ["win32", "cygwin", "cli"]:
import winloop # type: ignore
winloop.run(app_factory())
else:
import uvloop # type: ignore
uvloop.run(app_factory())
except ModuleNotFoundError:
# Fallback
asyncio.run(app_factory())