refactor: P2 core+common 建设(应用工厂/扩展/工具层迁入 src/)
【结构】 - src/core/:database/response/jwt/password/security/redis_client/mail/scheduler - src/common/:base_model/base_db/forms(base_form+fields)/constants/utils - src/models_registry.py:模型聚合器(Flask-Migrate 全量表识别) 【绞杀者 shim】extensions/、utils/、application/constants|forms|models 打兼容转发 【兼容修复】应用工厂抽取至 application/factory.py(P2 前置,P1 已完成) 【基线修复】application/models(25) + middleware(7) 原被嵌套 .gitignore(*) 误排除出版本库,已补入(base_model/base_db 现为 shim,原版在 src/common/) 【登记既有缺陷】 1. flask db CLI 因 .env FLASK_VERSION 与 --version 冲突报错 → Makefile 已加 FLASK_SKIP_DOTENV=1 2. 本机 Redis 未设密码但 REDIS_AUTH=True,/captcha 报 AUTH 错误(环境问题) 【验收】22 模型表全量识别;src/ flake8 零告警;pytest 5 passed;应用启动; /login 表单校验、/user/page 鉴权拦截冒烟正常 Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude
parent
92130ef070
commit
658ee6d596
+4
-94
@@ -1,98 +1,8 @@
|
||||
"""
|
||||
富文本与文件处理模块
|
||||
兼容 shim(绞杀者迁移期间保留)
|
||||
=============================================================
|
||||
|
||||
提供文件URL处理、临时文件迁移和富文本编辑器内容处理:
|
||||
- get_file_url: 拼接文件域名生成完整URL
|
||||
- save_file: 将临时目录文件迁移到正式存储目录
|
||||
- save_content: 处理富文本HTML内容,提取并保存其中的图片文件
|
||||
富文本工具已迁移至 src.common.utils.rich_text。
|
||||
本文件仅作兼容转发,最终在 P7(切尾)阶段删除。
|
||||
"""
|
||||
|
||||
import logging
|
||||
import os
|
||||
import re
|
||||
import time
|
||||
import html
|
||||
|
||||
from config import FLASK_FILE_URL, FLASK_UPLOAD_DIR, FLASK_TEMP_PATH
|
||||
from utils.file import mkdir
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def get_file_url(path):
|
||||
"""
|
||||
获取完整的文件访问URL
|
||||
|
||||
Args:
|
||||
path: 文件的相对路径,可以是空字符串、None或有效路径
|
||||
|
||||
Returns:
|
||||
完整的文件访问URL,如果path为空则返回None
|
||||
"""
|
||||
if not path:
|
||||
return None
|
||||
return FLASK_FILE_URL + path
|
||||
|
||||
|
||||
def save_file(url, directory):
|
||||
"""
|
||||
保存文件到指定目录
|
||||
|
||||
处理本站临时文件迁移:将临时目录中的文件移动到正式存储目录。
|
||||
|
||||
Args:
|
||||
url: 文件的完整URL或相对路径
|
||||
directory: 目标子目录名称,用于分类存储
|
||||
|
||||
Returns:
|
||||
文件的相对路径,失败时返回空字符串
|
||||
"""
|
||||
if not url:
|
||||
return "文件地址不能为空"
|
||||
|
||||
if url.find(FLASK_FILE_URL) != -1:
|
||||
if url.find("/temp") != -1:
|
||||
path = FLASK_UPLOAD_DIR + "/" + directory + "/" + time.strftime('%Y%m%d')
|
||||
mkdir(path)
|
||||
|
||||
old_path = url.replace(FLASK_FILE_URL, FLASK_UPLOAD_DIR)
|
||||
new_path = FLASK_UPLOAD_DIR + "/" + directory + old_path.replace(FLASK_TEMP_PATH, "")
|
||||
|
||||
os.rename(old_path, new_path)
|
||||
return new_path.replace(FLASK_UPLOAD_DIR, "")
|
||||
else:
|
||||
return url.replace(FLASK_FILE_URL, "")
|
||||
else:
|
||||
logger.warning(f"远程文件暂不支持保存: {url}")
|
||||
|
||||
return ""
|
||||
|
||||
|
||||
def save_content(content, title, directory):
|
||||
"""
|
||||
保存富文本编辑器内容
|
||||
|
||||
处理富文本HTML内容,提取其中的图片文件并保存,
|
||||
同时将文件alt属性设置为文章标题(已做HTML转义防XSS)。
|
||||
|
||||
Args:
|
||||
content: 富文本HTML内容
|
||||
title: 文章标题,用于设置文件alt属性
|
||||
directory: 文件存储的子目录名称
|
||||
|
||||
Returns:
|
||||
处理后的富文本内容,文件地址已被替换
|
||||
"""
|
||||
image_urls = re.findall('img src="(.*?)"', content, re.S)
|
||||
|
||||
for url in image_urls:
|
||||
image = save_file(url, directory)
|
||||
if image:
|
||||
content = content.replace(url, "[IMG_URL]" + image)
|
||||
|
||||
title = html.escape(str(title) if title else "", quote=True)
|
||||
if "alt=\"\"" in content and title:
|
||||
content = content.replace("alt=\"\"", "alt=\"" + title + "\"")
|
||||
|
||||
return content
|
||||
from src.common.utils.rich_text import * # noqa: F401,F403
|
||||
|
||||
Reference in New Issue
Block a user