2026-04-27 16:09:26 +08:00

726 lines
23 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

'use strict';
const Service = require('egg').Service;
const COS = require('cos-nodejs-sdk-v5');
const path = require('path');
const crypto = require('crypto');
const dayjs = require('dayjs');
class UploadService extends Service {
constructor(ctx) {
super(ctx);
this.initCOS();
}
/**
* 获取腾讯云公共配置。
*
* 说明:优先读取独立的腾讯云公共配置,若不存在则回退到兼容的 COS 配置,
* 以保证历史配置方式仍然可用。
*
* @return {Object} 腾讯云公共配置对象
*/
getTencentCloudConfig() {
const tencentCloudConfig = this.config.tencentCloud || {};
const cosConfig = this.config.cos || {};
return {
appId: tencentCloudConfig.appId ?? cosConfig.appId,
secretId: tencentCloudConfig.secretId ?? cosConfig.secretId,
secretKey: tencentCloudConfig.secretKey ?? cosConfig.secretKey,
};
}
/**
* 获取 COS 产品配置。
*
* @return {Object} COS 产品配置对象
*/
getCosConfig() {
return this.config.cos || {};
}
initCOS() {
const { secretId, secretKey } = this.getTencentCloudConfig();
this.cos = new COS({
SecretId: secretId,
SecretKey: secretKey,
});
this.ctx.logger.info('COS client initialized');
}
/**
* 获取上传参数 - 使用与PHP一致的算法
* @param {Object} fileInfo 上传参数对象
*/
async getUploadParams(fileInfo) {
const { ctx } = this;
const cosConfig = this.getCosConfig();
const tencentCloudConfig = this.getTencentCloudConfig();
const warehouseId = ctx.user.store_id;
const { fileName, fileType, fileSize, type, md5 } = fileInfo;
if (md5) {
const existingFile = await ctx.model.File.findOne({
where: {
md5_hash: md5,
warehouse_id: warehouseId,
is_deleted: false,
},
});
if (existingFile) {
this.ctx.logger.info(`文件已存在,返回已有记录: ${existingFile.file_url}`);
return {
fileId: existingFile.id,
fileUrl: existingFile.file_url,
fileKey: existingFile.file_key,
fileName: existingFile.file_name,
isNew: false,
};
}
}
// 验证文件
this.validateFileInfo({ fileName, fileSize });
const warehouse_quota = await this.app.model.query(
'SELECT * FROM vw_warehouse_quota WHERE warehouse_id = :warehouseId',
{
replacements: { warehouseId },
type: this.app.model.QueryTypes.SELECT,
}
);
const remaining_bytes = warehouse_quota[0].remaining_bytes;
if (remaining_bytes - fileSize < 0) {
return {
error: true,
msg: '空间配额已满',
};
}
// 生成文件名
const cosFileName = this.generateFileName(fileName, fileType, type, md5);
// 获取当前时间戳
const now = Math.floor(Date.now() / 1000);
const expire = now + cosConfig.expire;
const keyTime = `${now};${expire}`;
// 使用与PHP一致的算法生成签名
const { policy, signature } = this.generatePolicyAndSignature(cosFileName, now, expire);
// 构建返回参数
return {
fileName: cosFileName,
uploadUrl: `https://${cosConfig.bucket}.cos.${cosConfig.region}.myqcloud.com`,
bucket: cosConfig.bucket,
region: cosConfig.region,
signature: {
secretId: tencentCloudConfig.secretId,
signature,
keyTime,
key: cosFileName,
},
// 添加 data 字段以匹配前端
data: {
key: cosFileName,
policy,
'q-sign-algorithm': 'sha1',
'q-ak': tencentCloudConfig.secretId,
'q-sign-time': keyTime,
'q-key-time': keyTime,
'q-signature': signature,
success_action_status: '200',
'x-cos-security-token': '',
'x-cos-meta-filename': encodeURIComponent(fileName),
'x-cos-meta-filetype': fileType,
category: type || '',
},
// 用于前端验证
fileInfo: {
name: fileName,
url: cosFileName,
type: fileType,
size: fileSize,
md5,
},
// 有效期
expires: cosConfig.expire,
// 前端需要的额外参数
frontendConfig: this.getFrontendConfig(fileType, fileSize),
isNew: true,
};
}
/**
* 生成策略和签名 - 与PHP算法完全一致
* 参考你的PHP代码:
* $signature = hash_hmac('sha1', sha1($policy), hash_hmac('sha1', $keytime, $config['secretKey']));
* @param {string} key 文件 key
* @param {number} startTime 开始时间戳
* @param {number} expireTime 过期时间戳
*/
generatePolicyAndSignature(key, startTime, expireTime) {
const { secretId, secretKey } = this.getTencentCloudConfig();
const keyTime = `${startTime};${expireTime}`;
// 1. 生成策略 - 与PHP完全一致
const policy = {
expiration: new Date(expireTime * 1000).toISOString(),
conditions: [
{ 'q-sign-algorithm': 'sha1' },
{ 'q-ak': secretId },
{ 'q-sign-time': keyTime },
],
};
const policyString = JSON.stringify(policy);
const policyBase64 = Buffer.from(policyString).toString('base64');
// 2. 计算签名 - 与PHP算法完全一致
// 步骤1: 计算内层HMAC: hash_hmac('sha1', $keytime, $config['secretKey'])
const innerHmac = crypto.createHmac('sha1', secretKey);
innerHmac.update(keyTime);
const signKey = innerHmac.digest('hex'); // PHP中的 hash_hmac('sha1', $keytime, $secretKey)
// 步骤2: 计算SHA1(policy)
const policySha1 = crypto.createHash('sha1');
policySha1.update(policyString);
const policyHash = policySha1.digest('hex'); // PHP中的 sha1($policy)
// 步骤3: 外层HMAC: hash_hmac('sha1', sha1($policy), signKey)
const outerHmac = crypto.createHmac('sha1', signKey);
outerHmac.update(policyHash);
const signature = outerHmac.digest('hex'); // 最终的签名
// 调试信息
return {
policy: policyBase64,
signature,
policyString,
signKey,
policyHash,
};
}
/**
* 验证文件信息
* @param {Object} root0 文件信息对象
* @param {string} root0.fileName 文件名
* @param {number} root0.fileSize 文件大小
*/
validateFileInfo({ fileName, fileSize }) {
const { maxsize, mimetype } = this.getCosConfig();
// 验证文件大小
const maxSizeBytes = this.parseSizeToBytes(maxsize);
if (fileSize > maxSizeBytes) {
throw new Error(`文件大小不能超过 ${this.formatFileSize(maxSizeBytes)}`);
}
// 验证文件类型
const allowedTypes = mimetype.split(',');
const fileExt = path.extname(fileName).toLowerCase().replace('.', '');
if (!fileExt || !allowedTypes.includes(fileExt)) {
throw new Error(`不支持的文件类型 ${fileExt},仅支持: ${allowedTypes.join(', ')}`);
}
return true;
}
/**
* 生成文件唯一名称
* @param {string} fileName 原始文件名
* @param {string} fileType 文件类型
* @param {string} category 分类
* @param {string} md5 文件 MD5
*/
generateFileName(fileName, fileType, category = '', md5) {
const { savekey, dirs } = this.getCosConfig();
const ext = path.extname(fileName);
const name = path.basename(fileName, ext);
const timestamp = dayjs().format('YYYYMMDDHHmmss');
const random = Math.random().toString(36).substring(2, 10);
const { ctx } = this;
// 根据文件类型或指定的分类获取目录
let dir = dirs.default;
if (category && dirs[category]) {
dir = dirs[category];
} else {
const typeKey = this.config.fileTypes[fileType] || 'default';
dir = dirs[typeKey] || dirs.default;
}
// 替换模板变量
const result = savekey
.replace('{warehouseId}', ctx.user.store_id)
.replace('{dir}', dir)
.replace('{year}', dayjs().format('YYYY'))
.replace('{mon}', dayjs().format('MM'))
.replace('{day}', dayjs().format('DD'))
.replace('{timestamp}', timestamp)
.replace('{random}', random)
.replace('{filename}', name)
.replace('{filemd5}', md5)
.replace('{.suffix}', ext);
// 添加目录前缀并规范化路径
const fullPath = path.posix.join(result).replace(/\/\//g, '/');
return fullPath;
}
/**
* 解析文件大小字符串为字节数
* @param {string|number} sizeStr 文件大小字符串或字节数
*/
parseSizeToBytes(sizeStr) {
if (typeof sizeStr === 'number') {
return sizeStr;
}
const units = {
B: 1,
KB: 1024,
MB: 1024 * 1024,
GB: 1024 * 1024 * 1024,
TB: 1024 * 1024 * 1024 * 1024,
};
const match = sizeStr.match(/^(\d+(?:\.\d+)?)\s*([KMGT]?B)$/i);
if (match) {
const size = parseFloat(match[1]);
const unit = match[2].toUpperCase();
return size * (units[unit] || 1);
}
return 100 * 1024 * 1024;
}
/**
* 格式化文件大小
* @param {number} bytes 字节数
*/
formatFileSize(bytes) {
if (bytes === 0) return '0 B';
const k = 1024;
const sizes = [ 'B', 'KB', 'MB', 'GB', 'TB' ];
const i = Math.floor(Math.log(bytes) / Math.log(k));
return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i];
}
/**
* 获取前端配置
* @param {string} fileType 文件类型
* @param {number} fileSize 文件大小
*/
getFrontendConfig(fileType, fileSize) {
const { chunkSize } = this.getCosConfig();
return {
useMultipart: fileSize > 5 * 1024 * 1024,
chunkSize,
parallelLimit: 3,
enableMD5: true,
timeout: 30 * 60 * 1000,
retryCount: 3,
progressInterval: 1000,
};
}
/**
* 服务器端上传文件
* @param {Object} fileStream 文件流对象
* @param {Object} options 上传选项
*/
async uploadFile(fileStream, options = {}) {
const { bucket, region, uploadurl, cdnurl } = this.getCosConfig();
try {
this.validateFile(fileStream, options);
const fileName = options.key || this.generateFileName(
fileStream.filename,
fileStream.mimeType,
options.category
);
const result = await this.cos.putObject({
Bucket: bucket,
Region: region,
Key: fileName,
Body: fileStream,
ContentLength: fileStream.fileSize,
onProgress: options.onProgress,
});
return {
fileName,
cosUrl: `${uploadurl}/${fileName}`,
cdnUrl: `${cdnurl}/${fileName}`,
url: `${cdnurl}/${fileName}`,
etag: result.ETag,
location: result.Location,
uploadAt: new Date().toISOString(),
};
} catch (error) {
this.ctx.logger.error('文件上传失败:', error);
throw error;
}
}
/**
* 验证文件
* @param {Object} fileStream 文件流对象
* @param {Object} options 上传选项
*/
validateFile(fileStream, options = {}) {
const { maxsize, mimetype } = this.getCosConfig();
const { filename, fileSize } = fileStream;
const maxSizeBytes = this.parseSizeToBytes(maxsize);
if (fileSize > maxSizeBytes) {
throw new Error(`文件大小不能超过 ${this.formatFileSize(maxSizeBytes)}`);
}
const allowedTypes = (options.mimetype || mimetype).split(',');
const fileExt = path.extname(filename).toLowerCase().replace('.', '');
if (!allowedTypes.includes(fileExt)) {
throw new Error(`不支持的文件类型 ${fileExt},仅支持: ${allowedTypes.join(', ')}`);
}
return true;
}
/**
* 上传成功通知 - 记录到数据库并返回文件URL
* @param {Object} data - 上传成功的数据
* @param {string} data.fileKey - 文件在COS的key
* @param {string} data.fileName - 原始文件名
* @param {string} data.fileType - 文件类型
* @param {string} data.mimeType - MIME类型
* @param {number} data.fileSize - 文件大小
* @param {string} data.md5Hash - 文件MD5(可选)
* @param {string} data.sha1Hash - 文件SHA1(可选)
* @param {string} data.category - 业务分类
* @param {string} data.uploaderType - 上传者类型
* @param {string} data.uploaderDept - 上传者部门
*/
async uploadSuccessNotify(data) {
const { ctx } = this;
const cosConfig = this.getCosConfig();
try {
// 从上下文获取用户信息(根据你的实际用户认证方式)
const warehouseId = ctx.user?.store_id || ctx.query.warehouseId || ctx.request.body.warehouseId;
const userId = ctx.user?.id || ctx.query.userId || ctx.request.body.userId;
const userName = ctx.user?.real_name || ctx.user?.username || '系统用户';
// 如果没有用户信息,使用默认值或抛出错误
if (!warehouseId || !userId) {
this.ctx.logger.warn('上传成功通知缺少用户信息,使用默认值');
}
const resolvedFileKey = String(data.url || data.fileKey || '');
const resolvedFileName = String(data.fileName || data.name || path.basename(resolvedFileKey || 'file'));
const resolvedFileSize = Number(data.fileSize || data.size || 0);
// 生成完整的访问URL
const fileUrl = `${cosConfig.cdnurl || cosConfig.uploadurl}/${resolvedFileKey}`;
// 对外通知时对路径段做 URL 编码(保留 / 分隔符),兼容含中文文件名的历史记录
const encodeFileUrl = rawUrl => {
const slashIdx = rawUrl.indexOf('/', rawUrl.indexOf('//') + 2);
if (slashIdx === -1) return rawUrl;
const base = rawUrl.slice(0, slashIdx);
const keyPart = rawUrl.slice(slashIdx + 1);
return `${base}/${keyPart.split('/').map(seg => encodeURIComponent(seg)).join('/')}`;
};
const fileUrlEncoded = encodeFileUrl(fileUrl);
// 计算哈希值(如果前端没有提供)
let md5Hash = data.md5Hash || data.md5 || '';
let sha1Hash = data.sha1Hash || data.sha1 || '';
if (!md5Hash && data.fileContent) {
md5Hash = crypto.createHash('md5').update(data.fileContent).digest('hex');
}
if (!sha1Hash && data.fileContent) {
sha1Hash = crypto.createHash('sha1').update(data.fileContent).digest('hex');
}
const resolvedFileType = String(data.fileType || data.category || data.type || 'document');
const resolvedMimeType = String(data.mimeType || data.type || this.getMimeType(data.fileName || data.name || 'file.xlsx'));
const defaultExpireDays = data.businessType === 'receivingReport' ? 90 : 0;
const requestedExpireDays = Number(data.expireDays || defaultExpireDays || 0);
let resolvedExpireTime = data.expireTime || data.expire_time || null;
if (!resolvedExpireTime && Number.isFinite(requestedExpireDays) && requestedExpireDays > 0) {
resolvedExpireTime = new Date(Date.now() + requestedExpireDays * 24 * 60 * 60 * 1000);
}
if (resolvedExpireTime) {
const expireDate = resolvedExpireTime instanceof Date ? resolvedExpireTime : new Date(resolvedExpireTime);
resolvedExpireTime = Number.isNaN(expireDate.getTime()) ? null : expireDate;
}
if (!md5Hash) {
const fallbackFingerprint = JSON.stringify({
warehouseId: warehouseId || 0,
url: resolvedFileKey,
fileName: resolvedFileName,
fileSize: resolvedFileSize,
mimeType: resolvedMimeType,
});
md5Hash = crypto.createHash('md5').update(fallbackFingerprint).digest('hex');
}
if (!sha1Hash) {
sha1Hash = crypto.createHash('sha1').update(`${md5Hash}:${resolvedFileKey || fileUrl}`).digest('hex');
}
// 构建数据库记录
const fileRecord = {
warehouse_id: warehouseId || 0, // 默认为0或使用默认仓库
file_key: resolvedFileKey,
file_url: fileUrl,
file_name: resolvedFileName,
file_size: resolvedFileSize,
file_type: resolvedFileType,
mime_type: resolvedMimeType,
md5_hash: md5Hash,
sha1_hash: sha1Hash,
storage_mode: 'cos',
storage_class: 'standard',
uploader_id: userId?.toString() || '0',
uploader_name: userName,
uploader_dept: data.uploaderDept || '',
expire_time: resolvedExpireTime || null,
created_at: new Date(),
};
// 检查是否已存在相同文件(根据MD5去重)
if (md5Hash) {
const existingFile = await ctx.model.File.findOne({
where: {
md5_hash: md5Hash,
warehouse_id: warehouseId,
is_deleted: false,
},
});
if (existingFile) {
if (resolvedExpireTime && (!existingFile.expire_time || new Date(existingFile.expire_time).getTime() < resolvedExpireTime.getTime())) {
await existingFile.update({
expire_time: resolvedExpireTime,
updated_at: new Date(),
});
}
if (data.businessType && data.businessId) {
await this.createFileReference(existingFile.id, data.businessType, data.businessId, {
warehouseId,
createdBy: userName,
});
}
this.ctx.logger.info(`文件已存在,返回已有记录: ${existingFile.file_url}`);
return {
fileId: existingFile.id,
fileUrl: encodeFileUrl(existingFile.file_url),
fileKey: existingFile.file_key,
fileName: existingFile.file_name,
expireTime: resolvedExpireTime || existingFile.expire_time || null,
isNew: false,
};
}
}
// 保存到数据库
const savedFile = await ctx.model.File.create(fileRecord);
// 根据业务类型创建引用关系(如果需要)
if (data.businessType && data.businessId) {
await this.createFileReference(savedFile.id, data.businessType, data.businessId, {
warehouseId,
createdBy: userName,
});
}
this.ctx.logger.info(`文件记录保存成功: ${savedFile.id} - ${resolvedFileName}`);
// 返回成功响应
return {
code: 0,
message: '文件上传成功',
data: {
fileId: savedFile.id,
fileUrl: fileUrlEncoded,
fileKey: resolvedFileKey,
fileName: resolvedFileName,
fileSize: resolvedFileSize,
mimeType: resolvedMimeType,
isNew: true,
createdAt: savedFile.created_at,
},
};
} catch (error) {
this.ctx.logger.error('上传成功通知处理失败:', error);
// 根据错误类型返回相应错误
if (error.name === 'SequelizeUniqueConstraintError') {
return {
code: 1001,
message: '文件已存在(唯一约束冲突)',
data: null,
};
}
return {
code: 500,
message: `文件记录保存失败: ${error.message}`,
data: null,
};
}
}
/**
* 根据文件名获取MIME类型
* @param {string} fileName 文件名
*/
getMimeType(fileName) {
const ext = path.extname(fileName).toLowerCase();
const mimeTypes = {
'.jpg': 'image/jpeg',
'.jpeg': 'image/jpeg',
'.png': 'image/png',
'.gif': 'image/gif',
'.bmp': 'image/bmp',
'.webp': 'image/webp',
'.pdf': 'application/pdf',
'.doc': 'application/msword',
'.docx': 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
'.xls': 'application/vnd.ms-excel',
'.xlsx': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
'.ppt': 'application/vnd.ms-powerpoint',
'.pptx': 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
'.zip': 'application/zip',
'.rar': 'application/x-rar-compressed',
'.mp4': 'video/mp4',
'.mp3': 'audio/mpeg',
'.txt': 'text/plain',
};
return mimeTypes[ext] || 'application/octet-stream';
}
/**
* 创建文件业务引用关系
* @param {number|string} fileId 文件 ID
* @param {string} businessType 业务类型
* @param {number|string} businessId 业务 ID
* @param {Object} [options] 可选参数
* @param {number|string} [options.warehouseId] 仓库 ID
* @param {string} [options.createdBy] 创建人
* @param {number} [options.displayOrder] 显示顺序
*/
async createFileReference(fileId, businessType, businessId, options = {}) {
const { ctx } = this;
try {
if (!ctx.model.FileReference) {
return null;
}
const warehouseId = Number(
options.warehouseId || ctx.user?.store_id || ctx.query.warehouseId || ctx.request.body.warehouseId || 0
) || 0;
const createdBy = String(
options.createdBy || ctx.user?.real_name || ctx.user?.username || ctx.user?.id || 'system'
).trim() || 'system';
const normalizedBusinessType = String(businessType || '').trim();
const normalizedBusinessId = String(businessId || '').trim();
const displayOrder = Number(options.displayOrder || 0) || 0;
if (!fileId || !normalizedBusinessType || !normalizedBusinessId) {
this.ctx.logger.warn('创建文件引用关系参数不完整,已跳过', {
fileId,
businessType: normalizedBusinessType,
businessId: normalizedBusinessId,
});
return null;
}
if (typeof ctx.model.FileReference.findOne === 'function') {
const existingReference = await ctx.model.FileReference.findOne({
where: {
file_id: fileId,
warehouse_id: warehouseId,
business_type: normalizedBusinessType,
business_id: normalizedBusinessId,
},
});
if (existingReference) {
return existingReference;
}
}
const reference = await ctx.model.FileReference.create({
file_id: fileId,
warehouse_id: warehouseId,
business_type: normalizedBusinessType,
business_id: normalizedBusinessId,
display_order: displayOrder,
created_by: createdBy,
created_at: new Date(),
});
this.ctx.logger.info(`文件引用关系创建成功: fileId=${fileId}, businessType=${normalizedBusinessType}, businessId=${normalizedBusinessId}`);
return reference;
} catch (error) {
this.ctx.logger.error(`创建文件引用关系失败: ${error.message}`);
return null;
}
}
/**
* 上传完成回调接口 - 供前端调用
* @param {Object} callbackData 回调数据
*/
async handleUploadCallback(callbackData) {
const { ctx } = this;
try {
// 解析回调数据
const { key, size, mimeType, businessInfo } = callbackData;
// 构建文件信息
const fileInfo = {
fileKey: key,
fileName: key.substring(key.lastIndexOf('/') + 1),
fileSize: size,
mimeType,
category: businessInfo?.type || 'default',
uploaderType: ctx.user?.role || 'system_api',
uploaderDept: ctx.user?.department || '',
businessType: businessInfo?.type,
businessId: businessInfo?.id,
};
// 调用上传成功通知
const result = await this.uploadSuccessNotify(fileInfo);
return result;
} catch (error) {
this.ctx.logger.error('上传回调处理失败:', error);
throw error;
}
}
}
module.exports = UploadService;