clean: _guess_mime → общий mimeutil.py (убраны дубликаты)

This commit is contained in:
2026-06-15 06:45:16 +04:00
parent 8fc4d6a15a
commit a1339887fc
3 changed files with 16 additions and 30 deletions
+2 -16
View File
@@ -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
+12
View File
@@ -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")
+2 -14
View File
@@ -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")