feat: 重构追溯码及上链出库流程

This commit is contained in:
Nixevol
2026-07-16 11:49:50 +08:00
parent 5f5c158c01
commit ebc2603815
92 changed files with 7089 additions and 1615 deletions
+36 -11
View File
@@ -1,22 +1,24 @@
import { mkdir, open } from 'fs/promises';
import { createHash } from 'crypto';
import { mkdir, open, statfs } from 'fs/promises';
import { tmpdir } from 'os';
import { resolve } from 'path';
import { Readable } from 'stream';
export const OUTBOUND_IMPORT_MAX_ROWS = 10_000;
export const OUTBOUND_IMPORT_MAX_COLUMNS = 64;
export const OUTBOUND_IMPORT_MAX_ZIP_ENTRIES = 20_002;
// 仅用于防止恶意中央目录耗尽内存,不作为业务图片数量限制。
export const OUTBOUND_IMPORT_MAX_ZIP_ENTRIES = 100_002;
export const OUTBOUND_IMPORT_MAX_XLSX_SIZE = 100 * 1024 * 1024;
export const OUTBOUND_IMPORT_MAX_XLSX_ENTRIES = 256;
export const OUTBOUND_IMPORT_MAX_XLSX_UNCOMPRESSED_SIZE = 500 * 1024 * 1024;
export const OUTBOUND_IMPORT_MAX_IMAGE_SIZE = 100 * 1024 * 1024;
export const OUTBOUND_IMPORT_MAX_IMAGES_PER_ROW = 20;
export const OUTBOUND_IMPORT_MAX_IMAGE_PIXELS = 40_000_000;
export const OUTBOUND_IMPORT_MAX_IMAGE_REFERENCES = 20_000;
export const OUTBOUND_IMPORT_STORAGE_TIMEOUT_MS = 60 * 60 * 1000;
export const OUTBOUND_IMPORT_TEMP_DIR = resolve(process.env.ARTVERIS_OUTBOUND_TEMP_DIR || tmpdir());
export const OUTBOUND_IMPORT_UPLOAD_TEMP_PREFIX = 'artveris-outbound-upload-';
export const OUTBOUND_IMPORT_PREVIEW_TEMP_PREFIX = 'artveris-outbound-preview-';
export const OUTBOUND_IMPORT_PREVIEW_STORAGE_ROOT = 'outbound-preview';
const OUTBOUND_IMPORT_MIN_FREE_BYTES = 1024 * 1024 * 1024;
const ARCHIVE_PROGRESS_INTERVAL_BYTES = 8 * 1024 * 1024;
const ZIP_END_RECORD_MAX_SIZE = 65_557;
@@ -27,10 +29,39 @@ const ZIP_END_SIGNATURE = 0x06054b50;
const ZIP64_LOCATOR_SIGNATURE = 0x07064b50;
const ZIP64_END_SIGNATURE = 0x06064b50;
export function outboundImportPreviewStoragePrefix(sessionId: string): string {
if (!/^[A-Za-z0-9_-]+$/.test(sessionId)) throw new Error('Invalid outbound import session id');
return `${OUTBOUND_IMPORT_PREVIEW_STORAGE_ROOT}/${sessionId}/`;
}
export function outboundImportPreviewImageKey(sessionId: string, imagePath: string): string {
const canonicalPath = canonicalImagePath(imagePath);
const pathHash = createHash('sha256').update(canonicalPath, 'utf8').digest('hex');
const extension = mimeOfImagePath(canonicalPath) === 'image/jpeg'
? '.jpg'
: canonicalPath.slice(canonicalPath.lastIndexOf('.')).toLowerCase();
return `${outboundImportPreviewStoragePrefix(sessionId)}${pathHash}${extension}`;
}
export async function ensureOutboundImportTempDir(): Promise<void> {
await mkdir(OUTBOUND_IMPORT_TEMP_DIR, { recursive: true });
}
export async function assertOutboundImportDiskCapacity(requiredBytes: number, copies = 1): Promise<void> {
await ensureOutboundImportTempDir();
const fs = await statfs(OUTBOUND_IMPORT_TEMP_DIR);
const available = Number(fs.bavail) * Number(fs.bsize);
const required = Math.max(
OUTBOUND_IMPORT_MIN_FREE_BYTES,
Math.max(0, Math.ceil(requiredBytes)) * Math.max(1, copies),
);
if (!Number.isFinite(available) || available < required) {
throw new Error(
`出库导入临时磁盘空间不足:至少需要 ${Math.ceil(required / 1024 / 1024)}MB 可用空间`,
);
}
}
interface ArchiveEntryLike {
isDirectory: boolean;
header: { size: number };
@@ -141,12 +172,6 @@ export function assertArchiveLimits(
}
}
export function assertStoredImageBudget(referenceCount: number): void {
if (referenceCount > OUTBOUND_IMPORT_MAX_IMAGE_REFERENCES) {
throw new Error(`出库图片引用总数超过限制(最多 ${OUTBOUND_IMPORT_MAX_IMAGE_REFERENCES} 张)`);
}
}
export async function readArchiveEntryBuffer(
entry: Pick<ArchiveStreamEntryLike, 'stream'>,
maxBytes: number,
@@ -259,7 +284,7 @@ export function parseOutboundDate(value?: string): Date | undefined | null {
}
export function normalizeImagePath(raw: string): string {
const path = raw.trim().replace(/^image\//i, 'images/');
const path = raw.trim();
if (!path) return '';
if (path.includes('\\') || path.includes('..') || path.startsWith('/') || /^[a-z]:/i.test(path)) {
throw new Error(`图片路径不安全:${raw}`);