mirror of
https://github.com/migatu/conjurer.git
synced 2026-07-14 21:38:38 +00:00
53 lines
1.6 KiB
Python
53 lines
1.6 KiB
Python
# 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 = None
|
|
with open("/home/pi/gpt_cont_api_key", "r") as f:
|
|
TOKEN = f.read().strip()
|
|
|
|
|
|
# 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 TOKEN is None:
|
|
exit("No API token set!")
|
|
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)
|