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