/** * 获取截图参数 * @param req 请求对象 * @param filename 文件名 * @param upload_path 上传路径 * @returns 截图参数 */ function getParam(req, filename, upload_path) { const clip = req.body.clip; const quality = Math.min(Math.max(req.body.quality || 100, 0), 100); // 限制质量在0-100之间 const omit_background = req.body.omit_background || true; const encoding = req.body.encoding || 'binary'; const type = req.body.type || 'png'; // 验证type参数 if (!['png', 'jpeg'].includes(type)) { throw new Error('Invalid image type. Supported types: png, jpeg'); } // 验证encoding参数 if (!['binary', 'base64'].includes(encoding)) { throw new Error('Invalid encoding. Supported encodings: binary, base64'); } let params = { type: type, clip: clip, omitBackground: omit_background, encoding: encoding, }; if (type === 'jpeg') { params = Object.assign(params, {quality: quality}); } if (encoding === 'binary') { params = Object.assign(params, {path: `${upload_path}/${filename}`}); } console.log("params", new Date().getMilliseconds(), JSON.stringify(params)); return params; } /** * 上传文件 * @param res 响应对象 * @param encoding 编码方式 * @param base64 文件base64数据 * @param type 文件类型 * @param filename 文件名 * @param storageManager 存储管理器 * @param basePath 应用根路径 * @param upload_path 上传路径 * @returns 上传结果 */ async function upload(res, encoding, base64, type, filename, storageManager, basePath, upload_path) { if (encoding === 'base64' && typeof base64 === 'string') { return { name: filename, path: base64, }; } try { const result = await storageManager.upload(basePath, upload_path, filename); console.log("response", new Date().getMilliseconds(), JSON.stringify(result)); return result; } catch (err) { throw err; } } export { getParam, upload };