fix: 修复本地文件无法上传的问题;修复秒传错误的问题。

hsyh
NoahLan 7 months ago
parent 244f9447f1
commit eeeb82ae9b

@ -23,6 +23,8 @@ class LocalStorage(StorageInterface):
def upload(self, file_stream: BinaryIO, key: str, mime_type: Optional[str] = None) -> Dict:
"""上传文件到本地"""
# 本地文件存储需要移除前缀
key = key.lstrip(self.storage_type + ":")
abs_path = self._abs_path(key)
os.makedirs(os.path.dirname(abs_path), exist_ok=True)

@ -7,7 +7,7 @@ from io import BytesIO
from typing import Dict, Optional
from flask import current_app, url_for
from sqlalchemy import select
from sqlalchemy import select, exists
from iti.applications.common.enums import StatusEnum
from iti.applications.common.exceptions.biz_exp import BizException
@ -40,9 +40,12 @@ class SysFileService:
resolved_storage_type = cls._resolve_storage_type(storage_type, directory_id)
existing = db.session.scalar(
select(SysFile)
.filter_by(file_hash=file_hash, status=StatusEnum.ENABLED.value)
.limit(1)
select(
exists(SysFile).where(
SysFile.file_hash == file_hash,
SysFile.status == StatusEnum.ENABLED.value,
)
)
)
if existing and existing.storage_type == resolved_storage_type:
# 秒传:更新已有记录
@ -56,7 +59,9 @@ class SysFileService:
ext = os.path.splitext(file.filename or "")[1]
# 为支持多存储类型,前缀加上存储类型,使用冒号分隔,避免唯一索引冲突
file_key = f"{resolved_storage_type}:{datetime.now():%Y%m%d}/{file_hash}{ext}"
upload_result = storage.upload(BytesIO(file_bytes), file_key, getattr(file, "mimetype", None))
upload_result = storage.upload(
BytesIO(file_bytes), file_key, getattr(file, "mimetype", None)
)
new_file = SysFile(
filename=file.filename,
@ -175,7 +180,9 @@ class SysFileService:
file_size=file_size,
extension=os.path.splitext(upload_data.get("filename") or "")[1],
storage_type=storage_type,
storage_info=upload_data.get("storage_info") if storage_type != "local" else None,
storage_info=upload_data.get("storage_info")
if storage_type != "local"
else None,
directory_id=upload_data.get("directory_id"),
metadata_=metadata if metadata else None,
status=StatusEnum.ENABLED.value,
@ -250,7 +257,9 @@ class SysFileService:
return storage.get_preview_url(file_obj.file_key, expires=3600)
@classmethod
def get_thumbnail_url(cls, file_id: str, width: int = 200, height: int = 200, mode: str = "fit") -> Optional[str]:
def get_thumbnail_url(
cls, file_id: str, width: int = 200, height: int = 200, mode: str = "fit"
) -> Optional[str]:
"""获取缩略图URL
- local: 返回后端缩略图路由
@ -278,7 +287,9 @@ class SysFileService:
)
@classmethod
def get_thumbnail(cls, file_id: str, width: int = 200, height: int = 200, mode: str = "fit") -> BytesIO:
def get_thumbnail(
cls, file_id: str, width: int = 200, height: int = 200, mode: str = "fit"
) -> BytesIO:
"""生成缩略图"""
file_obj = cls.get_file_by_id(file_id)

Loading…
Cancel
Save