Files
conjurer/gpt_interface/ingest.py
T
Michal Tuszowski ef6eb84921 Preserve backup_old_docker bot variant at root (for comparison)
Snapshot of the dockerisation-era bot code laid out at the repository
root so it can be diffed directly against the working-copy baseline
(restructure/working-copy-root) to see how the variant differed:

- thin_client.py asyncio entrypoint (vs working_copy bot.py)
- constants.py env-var/credential refactor, communication auth, etc.
- extra gpt_interface/ service, sync.py, watch_script_params.py
- conanjurer_* modules absent in this variant

Docker infrastructure (docker/, docker-compose.yml, .dockerignore)
intentionally omitted - this branch is for code comparison only and is
not intended to be merged.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-28 22:55:53 +02:00

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)