feat(storage): group archives by the BPM order field

This commit is contained in:
阿白叔 committed 2026-10-17 18:59:06 +08:00
1 parent 607b240aa3
commit fff2a29ec0
13 files changed
+163 -18

No files matched your search

+5
View File
@@ -16,6 +16,11 @@ function buildFilename(metadata = {}) {
function validateSnapshot(snapshot) {
if (!snapshot || typeof snapshot.html !== 'string' || !snapshot.html.trim() || snapshot.html.length > 12_000_000) throw new Error('页面内容为空或超过大小限制。');
buildFilename(snapshot.metadata);
if (snapshot.bpmOrder != null && (
typeof snapshot.bpmOrder !== 'object' || Array.isArray(snapshot.bpmOrder) ||
!/^OFR_(?:LSCP|BS|KD|WY)_Applicant\.OrderNum$/.test(snapshot.bpmOrder.field) ||
typeof snapshot.bpmOrder.value !== 'string' || snapshot.bpmOrder.value !== snapshot.metadata.order
)) throw new Error('BPM 订单号字段与导出订单号不一致,请重新采集。');
if (snapshot.styles !== undefined && (!Array.isArray(snapshot.styles) || snapshot.styles.some(s => typeof s !== 'string'))) throw new Error('页面样式格式不正确。');
if (snapshot.images !== undefined && (!Array.isArray(snapshot.images) || snapshot.images.length > 200)) throw new Error('页面图片格式不正确。');
}
+1 -1
View File
@@ -57,7 +57,7 @@ function createServer(options) {
const jobId = crypto.randomBytes(16).toString('hex');
if (jobs.size >= 20) jobs.delete(jobs.keys().next().value);
jobs.set(jobId,{status:'running',jobId});
try { options.store?.recordJob({jobId,status:'running',metadata:snapshot.metadata}); }
try { options.store?.recordJob({jobId,status:'running',metadata:snapshot.metadata,...(snapshot.bpmOrder?{bpmOrder:{field:snapshot.bpmOrder.field,value:snapshot.bpmOrder.value}}:{})}); }
catch(error) {busy=false;jobs.delete(jobId);throw error;}
reply(202,{status:'running',jobId});
renderSnapshot(snapshot, options).then(result => {
+13
View File
@@ -0,0 +1,13 @@
'use strict';
const fail=message=>Object.assign(new Error(message),{status:400});
function orderFolder(job){
const raw=job.metadata?.order;
if(typeof raw!=='string'||!raw.trim())throw fail('缺少从 BPM 表单识别的订单号,请核实订单详情后重新导出。');
const order=raw.trim();
if(job.bpmOrder!=null&&(!/^OFR_(?:LSCP|BS|KD|WY)_Applicant\.OrderNum$/.test(job.bpmOrder.field)||job.bpmOrder.value!==raw))throw fail('BPM 订单号字段与归档订单号不一致,请重新导出。');
// The full BPM order field determines whether the order is six or seven digits.
// Never truncate a longer identifier or infer an order from a PDF filename.
if(!/^\d{6,7}$/.test(order))throw fail('BPM 订单号不是明确的 6 位或 7 位,请核实主订单号,暂不自动截取或归档。');
return {orderNumber:order,orderFolder:order,orderNumberLength:order.length};
}
module.exports={orderFolder};
+9 -1
View File
@@ -1,6 +1,7 @@
'use strict';
const fs=require('node:fs/promises');const path=require('node:path');
const {SmbClient,segments,inside}=require('./storage-files.cjs');
const {orderFolder}=require('./storage-order.cjs');
const fail=(message,status=400)=>Object.assign(new Error(message),{status});
const safe=e=>e.status?e:fail(({ENOENT:'找不到文件或归档目录,请检查路径。',EACCES:'没有共享目录访问权限。',EPERM:'没有共享目录操作权限。',ENOSPC:'存储服务器空间不足。',ENOTDIR:'指定路径不是目录。'})[e.code]||'存储操作未完成,请检查服务器与网络。',502);
class StorageService{
@@ -58,7 +59,14 @@ class StorageService{
segments(directory);if(typeof jobId!=='string'||!jobId||jobId.length>200)throw fail('请选择已完成的 PDF 导出任务。');
const job=this.jobs.jobs().find(j=>j.jobId===jobId);if(!job||job.status!=='success'||typeof job.path!=='string')throw fail('请选择已完成且存在的 PDF。');
const record=this.store.beginArchive(id,jobId,actor,directory);
try{const [root,file]=await Promise.all([fs.realpath(this.outputDir),fs.realpath(job.path)]);if(!inside(root,file)||file===root||path.extname(file).toLowerCase()!=='.pdf')throw fail('PDF 不在本工具输出目录内。');const area=await this.client.area(server),result=await area.archive(file,path.basename(file),directory);return this.store.finishArchive(record.id,'success',{...result,serverName:server.name});}
try{
const [root,file]=await Promise.all([fs.realpath(this.outputDir),fs.realpath(job.path)]);
if(!inside(root,file)||file===root||path.extname(file).toLowerCase()!=='.pdf')throw fail('PDF 不在本工具输出目录内。');
const grouping=orderFolder(job),base=segments(directory),current=base.at(-1)||segments(server.directory).at(-1);
const target=(current===grouping.orderFolder?base:[...base,grouping.orderFolder]).join('/');segments(target);
const area=await this.client.area(server),result=await area.archive(file,path.basename(file),target);
return this.store.finishArchive(record.id,'success',{...result,...grouping,baseDirectory:directory,directory:target,serverName:server.name});
}
catch(e){const error=safe(e);this.store.finishArchive(record.id,'error',{error:error.message,serverName:server.name});throw error;}
});
}