From f6607440ad4405228b95d723143988c453d2bac0 Mon Sep 17 00:00:00 2001 From: Michal Tuszowski Date: Sat, 23 Aug 2025 17:49:47 +0200 Subject: [PATCH] aaa --- gpt_interface/app.py | 2 ++ gpt_interface/ingest.py | 47 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 gpt_interface/ingest.py diff --git a/gpt_interface/app.py b/gpt_interface/app.py index 144ef6f..e789c52 100644 --- a/gpt_interface/app.py +++ b/gpt_interface/app.py @@ -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) diff --git a/gpt_interface/ingest.py b/gpt_interface/ingest.py new file mode 100644 index 0000000..8491d56 --- /dev/null +++ b/gpt_interface/ingest.py @@ -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)