- 新增海报生成接口,支持从网页URL或HTML内容生成海报图像 - 新增PDF生成接口,支持从网页URL或HTML内容生成PDF文档 - 添加Swagger API文档注释,完善接口描述和参数说明 - 实现HTML内容参数支持,允许直接传入HTML结构生成海报/PDF - 添加输入验证和标准化响应格式 - 引入DOMPurify库对HTML内容进行安全过滤 - 更新环境变量配置,支持API密钥认证和CORS设置 - 优化上传逻辑,统一返回标准响应结构 - 添加构建脚本支持Docker镜像打包和推送
77 lines
1.9 KiB
JavaScript
77 lines
1.9 KiB
JavaScript
/**
|
|
* 获取截图参数
|
|
* @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
|
|
};
|