'use strict'; const Service = require('egg').Service; const COS = require('cos-nodejs-sdk-v5'); const path = require('path'); const crypto = require('crypto'); const { v4: uuidv4 } = require('uuid'); const dayjs = require('dayjs'); class UploadService extends Service { constructor(ctx) { super(ctx); this.initCOS(); } initCOS() { const { secretId, secretKey } = this.config.cos; this.cos = new COS({ SecretId: secretId, SecretKey: secretKey, }); this.ctx.logger.info('COS client initialized'); } /** * 获取上传参数 - 使用与PHP一致的算法 * @param fileInfo */ async getUploadParams(fileInfo) { const { ctx } = this; const warehouseId = ctx.user.store_id; console.log('fileInfo', fileInfo); const { fileName, fileType, fileSize, type, id, 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, fileType, 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: '空间配额已满', }; } console.log('空间计算剩余可用', remaining_bytes - fileSize); // 生成文件名 const cosFileName = this.generateFileName(fileName, fileType, type, md5); // 获取当前时间戳 const now = Math.floor(Date.now() / 1000); const expire = now + this.config.cos.expire; const keyTime = `${now};${expire}`; // 使用与PHP一致的算法生成签名 const { policy, signature } = this.generatePolicyAndSignature(cosFileName, now, expire); // 构建返回参数 return { fileName: cosFileName, uploadUrl: `https://${this.config.cos.bucket}.cos.${this.config.cos.region}.myqcloud.com`, bucket: this.config.cos.bucket, region: this.config.cos.region, signature: { secretId: this.config.cos.secretId, signature, keyTime, key: cosFileName, }, // 添加 data 字段以匹配前端 data: { key: cosFileName, policy, 'q-sign-algorithm': 'sha1', 'q-ak': this.config.cos.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: this.config.cos.expire, // 前端需要的额外参数 frontendConfig: this.getFrontendConfig(fileType, fileSize), isNew: true, }; } /** * 生成策略和签名 - 与PHP算法完全一致 * 参考你的PHP代码: * $signature = hash_hmac('sha1', sha1($policy), hash_hmac('sha1', $keytime, $config['secretKey'])); * @param key * @param startTime * @param expireTime */ generatePolicyAndSignature(key, startTime, expireTime) { const { secretId, secretKey, bucket } = this.config.cos; 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'); // 最终的签名 // 调试信息 console.log('策略原始内容:', policyString); console.log('策略Base64:', policyBase64); console.log('keyTime:', keyTime); console.log('signKey (内层HMAC):', signKey); console.log('policyHash (SHA1):', policyHash); console.log('signature (外层HMAC):', signature); return { policy: policyBase64, signature, policyString, signKey, policyHash, }; } /** * 验证文件信息 * @param root0 * @param root0.fileName * @param root0.fileType * @param root0.fileSize */ validateFileInfo({ fileName, fileType, fileSize }) { const { maxsize, mimetype } = this.config.cos; // 验证文件大小 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 fileName * @param fileType * @param category * @param md5 */ generateFileName(fileName, fileType, category = '', md5) { const { savekey, dirs } = this.config.cos; 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 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 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 fileType * @param fileSize */ getFrontendConfig(fileType, fileSize) { const { chunkSize } = this.config.cos; return { useMultipart: fileSize > 5 * 1024 * 1024, chunkSize, parallelLimit: 3, enableMD5: true, timeout: 30 * 60 * 1000, retryCount: 3, progressInterval: 1000, }; } /** * 服务器端上传文件 * @param fileStream * @param options */ async uploadFile(fileStream, options = {}) { const { bucket, region } = this.config.cos; 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: `${this.config.cos.uploadurl}/${fileName}`, cdnUrl: `${this.config.cos.cdnurl}/${fileName}`, url: `${this.config.cos.cdnurl}/${fileName}`, etag: result.ETag, location: result.Location, uploadAt: new Date().toISOString(), }; } catch (error) { this.ctx.logger.error('文件上传失败:', error); throw error; } } /** * 验证文件 * @param fileStream * @param options */ validateFile(fileStream, options = {}) { const { maxsize, mimetype } = this.config.cos; 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; console.log('上传成功', data); 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('上传成功通知缺少用户信息,使用默认值'); } // 生成完整的访问URL const fileUrl = `${this.config.cos.cdnurl || this.config.cos.uploadurl}/${data.url}`; // 计算哈希值(如果前端没有提供) let md5Hash = data.md5; if (!md5Hash && data.fileContent) { // 如果有文件内容,可以计算哈希 md5Hash = crypto.createHash('md5').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; } // 构建数据库记录 const fileRecord = { warehouse_id: warehouseId || 0, // 默认为0或使用默认仓库 file_key: data.url, file_url: fileUrl, file_name: data.name, file_size: data.size, file_type: resolvedFileType, mime_type: resolvedMimeType, md5_hash: md5Hash || '', 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(), }); } this.ctx.logger.info(`文件已存在,返回已有记录: ${existingFile.file_url}`); return { fileId: existingFile.id, fileUrl: 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); } this.ctx.logger.info(`文件记录保存成功: ${savedFile.id} - ${data.fileName}`); // 返回成功响应 return { code: 0, message: '文件上传成功', data: { fileId: savedFile.id, fileUrl, fileKey: data.fileKey, fileName: data.fileName, fileSize: data.fileSize, mimeType: data.mimeType, 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 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 fileId * @param businessType * @param businessId */ async createFileReference(fileId, businessType, businessId) { const { ctx } = this; try { // 如果有文件引用模型 if (ctx.model.FileReference) { const reference = await ctx.model.FileReference.create({ file_id: fileId, business_type: businessType, business_id: businessId, created_at: new Date(), }); this.ctx.logger.info(`文件引用关系创建成功: fileId=${fileId}, businessType=${businessType}, businessId=${businessId}`); return reference; } return null; } catch (error) { this.ctx.logger.error(`创建文件引用关系失败: ${error.message}`); return null; } } /** * 上传完成回调接口 - 供前端调用 * @param callbackData */ async handleUploadCallback(callbackData) { const { ctx } = this; try { // 解析回调数据 const { key, size, mimeType, etag, location, 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;