- 新增storage_service.py封装MinIO操作 - 修改upload.py使用storage_service上传文件 - 修改course_service.py使用storage_service删除文件 - 适配preview.py支持从MinIO获取文件 - 适配knowledge_analysis_v2.py支持MinIO存储 - 在config.py添加MinIO配置项 - 添加minio依赖到requirements.txt 支持特性: - 自动降级到本地存储(MinIO不可用时) - 保持URL格式兼容(/static/uploads/) - 文件自动缓存到本地(用于预览和分析) Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
"""
|
||||
文件预览API
|
||||
提供课程资料的在线预览功能
|
||||
|
||||
支持MinIO和本地文件系统两种存储后端
|
||||
"""
|
||||
import logging
|
||||
from pathlib import Path
|
||||
@@ -15,6 +17,7 @@ from app.core.config import settings
|
||||
from app.models.user import User
|
||||
from app.models.course import CourseMaterial
|
||||
from app.services.document_converter import document_converter
|
||||
from app.services.storage_service import storage_service
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
router = APIRouter()
|
||||
@@ -81,10 +84,12 @@ def get_preview_type(file_ext: str) -> str:
|
||||
return PreviewType.DOWNLOAD
|
||||
|
||||
|
||||
def get_file_path_from_url(file_url: str) -> Optional[Path]:
|
||||
async def get_file_path_from_url(file_url: str) -> Optional[Path]:
|
||||
"""
|
||||
从文件URL获取本地文件路径
|
||||
|
||||
支持MinIO和本地文件系统。如果文件在MinIO中,会先下载到本地缓存。
|
||||
|
||||
Args:
|
||||
file_url: 文件URL(如 /static/uploads/courses/1/xxx.pdf)
|
||||
|
||||
@@ -94,11 +99,12 @@ def get_file_path_from_url(file_url: str) -> Optional[Path]:
|
||||
try:
|
||||
# 移除 /static/uploads/ 前缀
|
||||
if file_url.startswith('/static/uploads/'):
|
||||
relative_path = file_url.replace('/static/uploads/', '')
|
||||
full_path = Path(settings.UPLOAD_PATH) / relative_path
|
||||
return full_path
|
||||
object_name = file_url.replace('/static/uploads/', '')
|
||||
# 使用storage_service获取文件路径(自动处理MinIO下载)
|
||||
return await storage_service.get_file_path(object_name)
|
||||
return None
|
||||
except Exception:
|
||||
except Exception as e:
|
||||
logger.error(f"获取文件路径失败: {e}")
|
||||
return None
|
||||
|
||||
|
||||
@@ -158,7 +164,7 @@ async def get_material_preview(
|
||||
# 根据预览类型处理
|
||||
if preview_type == PreviewType.TEXT:
|
||||
# 文本类型,读取文件内容
|
||||
file_path = get_file_path_from_url(material.file_url)
|
||||
file_path = await get_file_path_from_url(material.file_url)
|
||||
if file_path and file_path.exists():
|
||||
try:
|
||||
with open(file_path, 'r', encoding='utf-8') as f:
|
||||
@@ -176,7 +182,7 @@ async def get_material_preview(
|
||||
|
||||
elif preview_type == PreviewType.EXCEL_HTML:
|
||||
# Excel文件转换为HTML预览
|
||||
file_path = get_file_path_from_url(material.file_url)
|
||||
file_path = await get_file_path_from_url(material.file_url)
|
||||
if file_path and file_path.exists():
|
||||
converted_url = document_converter.convert_excel_to_html(
|
||||
str(file_path),
|
||||
@@ -200,7 +206,7 @@ async def get_material_preview(
|
||||
|
||||
elif preview_type == PreviewType.PDF and document_converter.is_convertible(file_ext):
|
||||
# Office文档,需要转换为PDF
|
||||
file_path = get_file_path_from_url(material.file_url)
|
||||
file_path = await get_file_path_from_url(material.file_url)
|
||||
if file_path and file_path.exists():
|
||||
# 执行转换
|
||||
converted_url = document_converter.convert_to_pdf(
|
||||
|
||||
@@ -1,5 +1,9 @@
|
||||
"""
|
||||
文件上传API接口
|
||||
|
||||
支持两种存储后端:
|
||||
1. MinIO对象存储(生产环境推荐)
|
||||
2. 本地文件系统(开发环境或降级方案)
|
||||
"""
|
||||
import os
|
||||
import shutil
|
||||
@@ -17,6 +21,7 @@ from app.models.user import User
|
||||
from app.models.course import Course
|
||||
from app.schemas.base import ResponseModel
|
||||
from app.core.logger import get_logger
|
||||
from app.services.storage_service import storage_service
|
||||
|
||||
logger = get_logger(__name__)
|
||||
|
||||
@@ -93,16 +98,13 @@ async def upload_file(
|
||||
# 生成唯一文件名
|
||||
unique_filename = generate_unique_filename(file.filename)
|
||||
|
||||
# 获取上传路径
|
||||
upload_path = get_upload_path(file_type)
|
||||
file_path = upload_path / unique_filename
|
||||
|
||||
# 保存文件
|
||||
with open(file_path, "wb") as f:
|
||||
f.write(contents)
|
||||
|
||||
# 生成文件访问URL
|
||||
file_url = f"/static/uploads/{file_type}/{unique_filename}"
|
||||
# 使用storage_service上传文件
|
||||
object_name = f"{file_type}/{unique_filename}"
|
||||
file_url = await storage_service.upload(
|
||||
contents,
|
||||
object_name,
|
||||
content_type=file.content_type
|
||||
)
|
||||
|
||||
logger.info(
|
||||
"文件上传成功",
|
||||
@@ -111,6 +113,7 @@ async def upload_file(
|
||||
saved_filename=unique_filename,
|
||||
file_size=file_size,
|
||||
file_type=file_type,
|
||||
storage="minio" if storage_service.is_minio_enabled else "local",
|
||||
)
|
||||
|
||||
return ResponseModel(
|
||||
@@ -184,17 +187,13 @@ async def upload_course_material(
|
||||
# 生成唯一文件名
|
||||
unique_filename = generate_unique_filename(file.filename)
|
||||
|
||||
# 创建课程专属目录
|
||||
course_upload_path = Path(settings.UPLOAD_PATH) / "courses" / str(course_id)
|
||||
course_upload_path.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
# 保存文件
|
||||
file_path = course_upload_path / unique_filename
|
||||
with open(file_path, "wb") as f:
|
||||
f.write(contents)
|
||||
|
||||
# 生成文件访问URL
|
||||
file_url = f"/static/uploads/courses/{course_id}/{unique_filename}"
|
||||
# 使用storage_service上传文件
|
||||
object_name = f"courses/{course_id}/{unique_filename}"
|
||||
file_url = await storage_service.upload(
|
||||
contents,
|
||||
object_name,
|
||||
content_type=file.content_type
|
||||
)
|
||||
|
||||
logger.info(
|
||||
"课程资料上传成功",
|
||||
@@ -203,6 +202,7 @@ async def upload_course_material(
|
||||
original_filename=file.filename,
|
||||
saved_filename=unique_filename,
|
||||
file_size=file_size,
|
||||
storage="minio" if storage_service.is_minio_enabled else "local",
|
||||
)
|
||||
|
||||
return ResponseModel(
|
||||
@@ -243,24 +243,24 @@ async def delete_file(
|
||||
detail="无效的文件URL"
|
||||
)
|
||||
|
||||
# 转换为实际文件路径
|
||||
relative_path = file_url.replace("/static/uploads/", "")
|
||||
file_path = Path(settings.UPLOAD_PATH) / relative_path
|
||||
# 从URL中提取对象名称
|
||||
object_name = file_url.replace("/static/uploads/", "")
|
||||
|
||||
# 检查文件是否存在
|
||||
if not file_path.exists():
|
||||
if not await storage_service.exists(object_name):
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail="文件不存在"
|
||||
)
|
||||
|
||||
# 删除文件
|
||||
os.remove(file_path)
|
||||
# 使用storage_service删除文件
|
||||
await storage_service.delete(object_name)
|
||||
|
||||
logger.info(
|
||||
"文件删除成功",
|
||||
user_id=current_user.id,
|
||||
file_url=file_url,
|
||||
storage="minio" if storage_service.is_minio_enabled else "local",
|
||||
)
|
||||
|
||||
return ResponseModel(data=True, message="文件删除成功")
|
||||
|
||||
Reference in New Issue
Block a user