mirror of
https://github.com/migatu/conjurer.git
synced 2026-07-14 21:38:38 +00:00
aaa
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
from flask import Flask
|
||||
from file_serv import bp as uploader_bp
|
||||
from ingest import bp as ingest_bp
|
||||
from waitress import serve
|
||||
|
||||
app = Flask(__name__)
|
||||
@@ -8,5 +9,6 @@ PORT_ADDRESS = 49151
|
||||
|
||||
if __name__ == "__main__":
|
||||
app.register_blueprint(uploader_bp, url_prefix="/api")
|
||||
app.register_blueprint(ingest_bp, url_prefix="/api")
|
||||
|
||||
serve(app, host=HOST_ADDRESS, port=PORT_ADDRESS)
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
# ingest_text.py
|
||||
import os
|
||||
import urllib.parse
|
||||
from pathlib import Path
|
||||
|
||||
from flask import Blueprint, abort, jsonify, request
|
||||
|
||||
bp = Blueprint("ingest_text", __name__)
|
||||
BASE = Path(os.getenv("INGEST_DIR", "/home/pi/tmp_git")).resolve()
|
||||
BASE.mkdir(parents=True, exist_ok=True)
|
||||
TOKEN = os.getenv("INGEST_TOKEN", "change-me")
|
||||
|
||||
# prosty bufor kawałków w RAM (na 1 proces)
|
||||
chunks = {}
|
||||
|
||||
|
||||
@bp.get("/api/ingest-text")
|
||||
def ingest_text():
|
||||
if request.args.get("key") != TOKEN:
|
||||
return jsonify(error="unauthorized"), 401
|
||||
|
||||
name = request.args.get("name", "").strip()
|
||||
index = int(request.args.get("index", "1"))
|
||||
total = int(request.args.get("total", "1"))
|
||||
chunk = request.args.get("chunk", "")
|
||||
|
||||
if not name or "/" in name or ".." in name:
|
||||
return jsonify(error="bad name"), 400
|
||||
if not (1 <= index <= total <= 9999):
|
||||
return jsonify(error="bad indexing"), 400
|
||||
|
||||
# gromadzimy w pamięci (możesz podmienić na Redis)
|
||||
key = f"{name}:{total}"
|
||||
entry = chunks.setdefault(key, {})
|
||||
entry[index] = urllib.parse.unquote_plus(chunk)
|
||||
|
||||
if len(entry) == total:
|
||||
# składamy i zapisujemy
|
||||
data = "".join(entry[i] for i in range(1, total + 1))
|
||||
out = (BASE / name).resolve()
|
||||
if not str(out).startswith(str(BASE)):
|
||||
return jsonify(error="bad path"), 400
|
||||
out.write_text(data, encoding="utf-8")
|
||||
del chunks[key]
|
||||
return jsonify(ok=True, saved=str(out), bytes=len(data.encode("utf-8")))
|
||||
else:
|
||||
return jsonify(pending=True, got=len(entry), total=total)
|
||||
Reference in New Issue
Block a user