clean: _guess_mime → общий mimeutil.py (убраны дубликаты)
This commit is contained in:
+2
-16
@@ -18,6 +18,7 @@ import parser as parser_mod
|
|||||||
import textify
|
import textify
|
||||||
import extractor
|
import extractor
|
||||||
import differ
|
import differ
|
||||||
|
import mimeutil
|
||||||
|
|
||||||
api_bp = Blueprint("api", __name__)
|
api_bp = Blueprint("api", __name__)
|
||||||
|
|
||||||
@@ -107,7 +108,7 @@ def add_supplement(contract_id):
|
|||||||
return jsonify({"error": "empty filename"}), 400
|
return jsonify({"error": "empty filename"}), 400
|
||||||
|
|
||||||
filename = file.filename
|
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()
|
file_bytes = file.read()
|
||||||
|
|
||||||
# 2. Сохранить в documents
|
# 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
|
|
||||||
|
|||||||
@@ -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
@@ -18,6 +18,7 @@ from flask import Blueprint, request, jsonify
|
|||||||
import db
|
import db
|
||||||
import parser
|
import parser
|
||||||
import textify
|
import textify
|
||||||
|
import mimeutil
|
||||||
|
|
||||||
upload_bp = Blueprint("upload", __name__)
|
upload_bp = Blueprint("upload", __name__)
|
||||||
|
|
||||||
@@ -40,7 +41,7 @@ def upload():
|
|||||||
|
|
||||||
# 2. Определить MIME-тип (приоритет: расширение, а не content_type от браузера)
|
# 2. Определить MIME-тип (приоритет: расширение, а не content_type от браузера)
|
||||||
filename = file.filename
|
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()
|
file_bytes = file.read()
|
||||||
|
|
||||||
if not file_bytes:
|
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")
|
|
||||||
|
|||||||
Reference in New Issue
Block a user