Files

74 lines
2.9 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""
Blueprint: API обфускации (POST /api/drhider).
Принимает multipart/form-data с файлами, возвращает ZIP-архив
с обфусцированными документами.
"""
import io
import traceback
from flask import Blueprint, request, send_file, jsonify
from drhider import obfuscate_files, LLMClient
# ═══════════════════════════════════════════════════════════════════════════
# Blueprint: API DrHider
# ═══════════════════════════════════════════════════════════════════════════
api_bp = Blueprint("api", __name__, url_prefix="/api")
# Максимальный размер тела запроса: 200 MB
MAX_BODY = 200 * 1024 * 1024
@api_bp.route("/drhider", methods=["POST"])
def drhider():
"""Обфускация документов.
Принимает multipart/form-data с полем 'files' (один или несколько файлов).
Возвращает ZIP-архив с обфусцированными документами + mapping.csv.
Поддерживаемые форматы:
.docx, .pdf, .txt, .doc (бинарный — без изменений)
.zip (распаковывается, содержимое обфусцируется)
Returns:
200: ZIP-архив (application/zip)
400: {"ok": false, "error": "..."}
500: {"ok": false, "error": "..."}
"""
# ── Получаем файлы из запроса ──
uploaded = request.files.getlist("files")
if not uploaded:
return jsonify({"ok": False, "error": "Нет файлов"}), 400
# Формируем список для obfuscate_files: [(name, bytes, mimetype), ...]
files = []
for f in uploaded:
if f.filename:
files.append((f.filename, f.read(), f.mimetype or ""))
if not files:
return jsonify({"ok": False, "error": "Нет файлов"}), 400
# ── Обфускация ──
try:
# Создаём LLM-клиент для NER-сканирования
llm = LLMClient()
# Вызываем ядро обфускации
zip_data, _csv = obfuscate_files(files, llm_client=llm)
# Отдаём ZIP-архив
return send_file(
io.BytesIO(zip_data),
mimetype="application/zip",
as_attachment=True,
download_name="drhider_output.zip",
)
except Exception as e:
# Логируем полный трейсбек на сервере
traceback.print_exc()
return jsonify({"ok": False, "error": str(e)}), 500