重构基本完成

This commit is contained in:
2025-02-21 11:35:53 +08:00
parent cdd05e95ba
commit 85d0a8fadc
16 changed files with 346 additions and 8 deletions

View File

@ -1,6 +1,18 @@
import os
import shutil
from fastapi import UploadFile
from PIL import Image
"""文件处理相关的util"""
def file_path(*path):
"""
拼接返回文件路径
:param path:
:return:
"""
return_path = os.path.join(*path)
return return_path
def create_folder(*path):
@ -10,6 +22,7 @@ def create_folder(*path):
os.makedirs(folder_path, exist_ok=True)
except Exception as e:
print(f"创建文件夹时错误: {e}")
return folder_path
def save_images(*path, file: UploadFile):
@ -43,3 +56,26 @@ def create_thumbnail(input_image_path, out_image_path, size=(116, 70)):
os.makedirs(os.path.dirname(out_image_path), exist_ok=True)
# 保存生成的缩略图
image.save(out_image_path, 'JPEG')
def copy_and_rename_file(src_file_path, dst_dir, new_name):
"""
复制文件到指定目录并重命名,保持文件的后缀不变。
:param src_file_path: 源文件路径
:param dst_dir: 目标目录
:param new_name: 新文件名(不含扩展名)
"""
# 获取文件的完整文件名(包括扩展名)
base_name = os.path.basename(src_file_path)
# 分离文件名和扩展名
file_name, file_extension = os.path.splitext(base_name)
# 构建新的文件名和目标路径
new_file_name = f"{new_name}{file_extension}"
dst_file_path = os.path.join(dst_dir, new_file_name)
# 确保目标目录存在
os.makedirs(dst_dir, exist_ok=True)
# 复制文件到目标位置并重命名
shutil.copy(src_file_path, dst_file_path)