2023-10-15 05:31:54 +01:00
|
|
|
from importlib.resources import files
|
2023-10-14 23:29:50 +01:00
|
|
|
|
|
|
|
import msgspec
|
2023-10-17 23:06:27 +01:00
|
|
|
from html5tagger import E
|
|
|
|
from sanic import Sanic, redirect
|
2023-10-14 23:29:50 +01:00
|
|
|
from sanic.log import logger
|
2023-10-15 05:31:54 +01:00
|
|
|
from sanic.response import html
|
2023-10-14 23:29:50 +01:00
|
|
|
|
2023-10-19 00:06:14 +01:00
|
|
|
from . import config, session, watching
|
2023-10-17 23:06:27 +01:00
|
|
|
from .auth import authbp
|
2023-10-19 00:06:14 +01:00
|
|
|
from .fileio import FileServer
|
2023-10-14 23:29:50 +01:00
|
|
|
from .protocol import ErrorMsg, FileRange, StatusMsg
|
|
|
|
|
|
|
|
app = Sanic("cista")
|
|
|
|
fileserver = FileServer()
|
2023-10-17 19:33:31 +01:00
|
|
|
watching.register(app, "/api/watch")
|
2023-10-17 23:06:27 +01:00
|
|
|
app.blueprint(authbp)
|
2023-10-14 23:29:50 +01:00
|
|
|
|
|
|
|
def asend(ws, msg):
|
|
|
|
return ws.send(msg if isinstance(msg, bytes) else msgspec.json.encode(msg).decode())
|
|
|
|
|
|
|
|
@app.before_server_start
|
|
|
|
async def start_fileserver(app, _):
|
2023-10-19 00:06:14 +01:00
|
|
|
config.load_config() # Main process may have loaded it but we haven't
|
|
|
|
app.static("/files", config.config.path, use_content_range=True, stream_large_files=True, directory_view=True)
|
|
|
|
|
2023-10-14 23:29:50 +01:00
|
|
|
await fileserver.start()
|
|
|
|
|
|
|
|
@app.after_server_stop
|
|
|
|
async def stop_fileserver(app, _):
|
|
|
|
await fileserver.stop()
|
|
|
|
|
|
|
|
@app.get("/")
|
|
|
|
async def index_page(request):
|
2023-10-19 00:06:14 +01:00
|
|
|
s = config.config.public or session.get(request)
|
2023-10-17 23:06:27 +01:00
|
|
|
if not s:
|
|
|
|
return redirect("/login")
|
2023-10-14 23:29:50 +01:00
|
|
|
index = files("cista").joinpath("static", "index.html").read_text()
|
2023-10-17 23:06:27 +01:00
|
|
|
flash = request.cookies.flash
|
|
|
|
if flash:
|
|
|
|
index += str(E.div(flash, id="flash"))
|
2023-10-14 23:29:50 +01:00
|
|
|
return html(index)
|
|
|
|
|
|
|
|
@app.websocket('/api/upload')
|
|
|
|
async def upload(request, ws):
|
|
|
|
alink = fileserver.alink
|
|
|
|
url = request.url_for("upload")
|
|
|
|
while True:
|
|
|
|
req = None
|
|
|
|
try:
|
|
|
|
text = await ws.recv()
|
|
|
|
if not isinstance(text, str):
|
|
|
|
raise ValueError(f"Expected JSON control, got binary len(data) = {len(text)}")
|
|
|
|
req = msgspec.json.decode(text, type=FileRange)
|
|
|
|
pos = req.start
|
|
|
|
while pos < req.end and (data := await ws.recv()) and isinstance(data, bytes):
|
|
|
|
pos += await alink(("upload", req.name, pos, data, req.size))
|
|
|
|
if pos != req.end:
|
|
|
|
d = f"{len(data)} bytes" if isinstance(data, bytes) else data
|
|
|
|
raise ValueError(f"Expected {req.end - pos} more bytes, got {d}")
|
|
|
|
# Report success
|
2023-10-17 19:33:31 +01:00
|
|
|
res = StatusMsg(status="ack", req=req)
|
2023-10-14 23:29:50 +01:00
|
|
|
await asend(ws, res)
|
2023-10-17 19:33:31 +01:00
|
|
|
await ws.drain()
|
2023-10-14 23:29:50 +01:00
|
|
|
except Exception as e:
|
2023-10-17 19:33:31 +01:00
|
|
|
res = ErrorMsg(error=str(e), req=req)
|
2023-10-14 23:29:50 +01:00
|
|
|
await asend(ws, res)
|
|
|
|
logger.exception(repr(res), e)
|
|
|
|
return
|
|
|
|
|
|
|
|
@app.websocket('/api/download')
|
|
|
|
async def download(request, ws):
|
|
|
|
alink = fileserver.alink
|
|
|
|
while True:
|
|
|
|
req = None
|
|
|
|
try:
|
2023-10-17 19:33:31 +01:00
|
|
|
print("Waiting for download command")
|
2023-10-14 23:29:50 +01:00
|
|
|
text = await ws.recv()
|
|
|
|
if not isinstance(text, str):
|
|
|
|
raise ValueError(f"Expected JSON control, got binary len(data) = {len(text)}")
|
|
|
|
req = msgspec.json.decode(text, type=FileRange)
|
|
|
|
print("download", req)
|
|
|
|
pos = req.start
|
|
|
|
while pos < req.end:
|
|
|
|
end = min(req.end, pos + (1<<20))
|
|
|
|
data = await alink(("download", req.name, pos, end))
|
|
|
|
await asend(ws, data)
|
|
|
|
pos += len(data)
|
|
|
|
# Report success
|
2023-10-17 19:33:31 +01:00
|
|
|
res = StatusMsg(status="ack", req=req)
|
2023-10-14 23:29:50 +01:00
|
|
|
await asend(ws, res)
|
2023-10-17 19:33:31 +01:00
|
|
|
print(ws, dir(ws))
|
|
|
|
await ws.drain()
|
2023-10-14 23:29:50 +01:00
|
|
|
print(res)
|
|
|
|
|
|
|
|
except Exception as e:
|
2023-10-17 19:33:31 +01:00
|
|
|
res = ErrorMsg(error=str(e), req=req)
|
2023-10-14 23:29:50 +01:00
|
|
|
await asend(ws, res)
|
|
|
|
logger.exception(repr(res), e)
|
|
|
|
return
|