diff --git a/app.py b/app.py index e33d02d..f68cb5d 100644 --- a/app.py +++ b/app.py @@ -56,6 +56,39 @@ def ai_request(): except Exception as e: return jsonify({"error": str(e)}), 500 +@app.route("/AI/", methods=["GET"]) +def req_status(id): + try: + with sqlite3.connect(DB_PATH) as conn: + row = conn.execute( + "SELECT status, result FROM requests WHERE id = ?", (id,) + ).fetchone() + except Exception as e: + return jsonify({"error": str(e)}), 500 + + if row is None: + return jsonify({"error": f"task {id} not found"}), 404 + + status, result = row + + if status not in ("OK", "ОК"): + return jsonify({"id": id, "status": status}), 200 + + if not result: + return jsonify({"error": f"task {id} is OK but has no result file"}), 500 + + path = Path(result) + if not path.is_absolute(): + path = RESULT_DIR / path + + if not path.is_file(): + return jsonify({"error": f"result file not found: {path}"}), 404 + + try: + return send_file(path, as_attachment=True, download_name=path.name) + except Exception as e: + return jsonify({"error": str(e)}), 500 + @app.route('/health', methods=['GET']) def health_check(): """Проверка работоспособности API"""