| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- import os
- import random
- from datetime import datetime
- from fastapi import UploadFile
- from core.settings import upload_settings
- class UploadUtil:
- """
- 上传工具类
- """
-
- @classmethod
- def generate_random_number(cls):
- """
- 生成3位数字构成的字符串
-
- :return: 3位数字构成的字符串
- """
- random_number = random.randint(1, 999)
-
- return f'{random_number:03}'
-
- @classmethod
- def check_file_exists(cls, filepath: str):
- """
- 检查文件是否存在
-
- :param filepath: 文件路径
- :return: 校验结果
- """
- return os.path.exists(filepath)
-
- @classmethod
- def check_file_extension(cls, file: UploadFile):
- """
- 检查文件后缀是否合法
-
- :param file: 文件对象
- :return: 校验结果
- """
- file_extension = file.filename.rsplit('.', 1)[-1]
- if file_extension in upload_settings.DEFAULT_ALLOWED_EXTENSION:
- return True
- return False
-
- @classmethod
- def check_file_timestamp(cls, filename: str):
- """
- 校验文件时间戳是否合法
-
- :param filename: 文件名称
- :return: 校验结果
- """
- timestamp = filename.rsplit('.', 1)[0].split('_')[-1].split(upload_settings.upload_machine)[0]
- try:
- datetime.strptime(timestamp, '%Y%m%d%H%M%S')
- return True
- except ValueError:
- return False
-
- @classmethod
- def check_file_machine(cls, filename: str):
- """
- 校验文件机器码是否合法
-
- :param filename: 文件名称
- :return: 校验结果
- """
- if filename.rsplit('.', 1)[0][-4] == upload_settings.upload_machine:
- return True
- return False
-
- @classmethod
- def check_file_random_code(cls, filename: str):
- """
- 校验文件随机码是否合法
-
- :param filename: 文件名称
- :return: 校验结果
- """
- valid_code_list = [f'{i:03}' for i in range(1, 999)]
- if filename.rsplit('.', 1)[0][-3:] in valid_code_list:
- return True
- return False
-
- @classmethod
- def generate_file(cls, filepath: str):
- """
- 根据文件生成二进制数据
-
- :param filepath: 文件路径
- :yield: 二进制数据
- """
- with open(filepath, 'rb') as response_file:
- yield from response_file
-
- @classmethod
- def delete_file(cls, filepath: str):
- """
- 根据文件路径删除对应文件
-
- :param filepath: 文件路径
- """
- os.remove(filepath)
|