appointment_system/modules/server/fileServer.js
2025-12-11 22:50:18 +08:00

39 lines
1.4 KiB
JavaScript

import { AppServer } from '../api/AppServer.js'
import UploadFactory from '../../utils/IServer/UploadFactory.js'
import Config from '../Config.js'
/**
* 初始化文件上传服务
* @param {String} preset - 配置预设名称,可选值:'normal'(默认), 'avatar', 'certification'
* @returns {IUploadService} 上传服务实例
*
* @example
* // 使用默认配置(普通图片)
* const uploadService = initFileService()
*
* // 使用头像配置(高质量)
* const uploadService = initFileService('avatar')
*
* // 使用认证配置
* const uploadService = initFileService('certification')
*/
export function initFileService(preset = 'normal') {
// 初始化上传服务(使用工厂模式)
const appServer = new AppServer();
// 获取配置(优先使用预设配置,否则使用默认配置)
const presetConfig = Config.UPLOAD_PRESETS[preset] || {}
const uploadConfig = {
...Config.UPLOAD_CONFIG, // 基础配置
...presetConfig // 覆盖预设配置
}
// 通过工厂创建上传服务(从配置读取服务类型)
const uploadService = UploadFactory.create(
Config.UPLOAD_SERVICE_TYPE, // 服务类型(从配置读取)
async (key) => await appServer.GetCosSign(key), // 签名获取函数
uploadConfig // 配置(从配置文件读取)
);
return uploadService;
}