From a1339887fc9a79660ab9fe70e47e9a69e1f11875 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9CNaeel=E2=80=9D?= Date: Mon, 15 Jun 2026 06:45:16 +0400 Subject: [PATCH] =?UTF-8?q?clean:=20=5Fguess=5Fmime=20=E2=86=92=20=D0=BE?= =?UTF-8?q?=D0=B1=D1=89=D0=B8=D0=B9=20mimeutil.py=20(=D1=83=D0=B1=D1=80?= =?UTF-8?q?=D0=B0=D0=BD=D1=8B=20=D0=B4=D1=83=D0=B1=D0=BB=D0=B8=D0=BA=D0=B0?= =?UTF-8?q?=D1=82=D1=8B)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- site/api.py | 18 ++---------------- site/mimeutil.py | 12 ++++++++++++ site/upload.py | 16 ++-------------- 3 files changed, 16 insertions(+), 30 deletions(-) create mode 100644 site/mimeutil.py diff --git a/site/api.py b/site/api.py index 7096d2a..01c6b02 100644 --- a/site/api.py +++ b/site/api.py @@ -18,6 +18,7 @@ import parser as parser_mod import textify import extractor import differ +import mimeutil api_bp = Blueprint("api", __name__) @@ -107,7 +108,7 @@ def add_supplement(contract_id): return jsonify({"error": "empty filename"}), 400 filename = file.filename - mime_type = _guess_mime(filename) or file.content_type or "application/octet-stream" + mime_type = mimeutil.guess_mime(filename) or file.content_type or "application/octet-stream" file_bytes = file.read() # 2. Сохранить в documents @@ -260,18 +261,3 @@ def get_history(contract_id): # ── Вспомогательные ───────────────────────────────────────────── -def _guess_mime(filename: str) -> str: - """MIME-тип по расширению.""" - ext = filename.lower().rsplit(".", 1)[-1] if "." in filename else "" - return { - "docx": "application/vnd.openxmlformats-officedocument.wordprocessingml.document", - "doc": "application/msword", - "pdf": "application/pdf", - "zip": "application/zip", - }.get(ext, "") - - -def _jsonb(val): - """Подготовить значение для JSONB-колонки.""" - import json - return json.dumps(val, ensure_ascii=False, default=str) if val is not None else None diff --git a/site/mimeutil.py b/site/mimeutil.py new file mode 100644 index 0000000..0eb72c1 --- /dev/null +++ b/site/mimeutil.py @@ -0,0 +1,12 @@ +"""Общий MIME-утилит для всего проекта.""" + + +def guess_mime(filename: str) -> str: + """Определить MIME-тип по расширению файла.""" + ext = filename.lower().rsplit(".", 1)[-1] if "." in filename else "" + return { + "docx": "application/vnd.openxmlformats-officedocument.wordprocessingml.document", + "doc": "application/msword", + "pdf": "application/pdf", + "zip": "application/zip", + }.get(ext, "application/octet-stream") diff --git a/site/upload.py b/site/upload.py index 660f7dc..9d3e3a9 100644 --- a/site/upload.py +++ b/site/upload.py @@ -18,6 +18,7 @@ from flask import Blueprint, request, jsonify import db import parser import textify +import mimeutil upload_bp = Blueprint("upload", __name__) @@ -40,7 +41,7 @@ def upload(): # 2. Определить MIME-тип (приоритет: расширение, а не content_type от браузера) filename = file.filename - mime_type = _guess_mime(filename) or file.content_type or "application/octet-stream" + mime_type = mimeutil.guess_mime(filename) or file.content_type or "application/octet-stream" file_bytes = file.read() if not file_bytes: @@ -101,16 +102,3 @@ def upload(): }) -def _guess_mime(filename: str) -> str: - """ - Угадать MIME-тип по расширению файла. - Используется когда браузер не передал content_type. - """ - ext = filename.lower().rsplit(".", 1)[-1] if "." in filename else "" - mime_map = { - "docx": "application/vnd.openxmlformats-officedocument.wordprocessingml.document", - "doc": "application/msword", - "pdf": "application/pdf", - "zip": "application/zip", - } - return mime_map.get(ext, "application/octet-stream")