feat: 更新静态出库模板与大包上传
This commit is contained in:
@@ -1,40 +1,129 @@
|
||||
import { mkdir, open } 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_UNCOMPRESSED_SIZE = 120 * 1024 * 1024;
|
||||
export const OUTBOUND_IMPORT_MAX_XLSX_SIZE = 10 * 1024 * 1024;
|
||||
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 = 50 * 1024 * 1024;
|
||||
export const OUTBOUND_IMPORT_MAX_IMAGE_SIZE = 10 * 1024 * 1024;
|
||||
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_MAX_STORED_IMAGE_SIZE = 200 * 1024 * 1024;
|
||||
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-';
|
||||
|
||||
const ARCHIVE_PROGRESS_INTERVAL_BYTES = 8 * 1024 * 1024;
|
||||
const ZIP_END_RECORD_MAX_SIZE = 65_557;
|
||||
const ZIP_END_RECORD_MIN_SIZE = 22;
|
||||
const ZIP64_LOCATOR_SIZE = 20;
|
||||
const ZIP64_END_RECORD_MIN_SIZE = 56;
|
||||
const ZIP_END_SIGNATURE = 0x06054b50;
|
||||
const ZIP64_LOCATOR_SIGNATURE = 0x07064b50;
|
||||
const ZIP64_END_SIGNATURE = 0x06064b50;
|
||||
|
||||
export async function ensureOutboundImportTempDir(): Promise<void> {
|
||||
await mkdir(OUTBOUND_IMPORT_TEMP_DIR, { recursive: true });
|
||||
}
|
||||
|
||||
interface ArchiveEntryLike {
|
||||
isDirectory: boolean;
|
||||
header: { size: number };
|
||||
}
|
||||
|
||||
export function assertZipEntryCount(buffer: Buffer, maxEntries: number, label: string): void {
|
||||
const minEndOffset = Math.max(0, buffer.length - 65_557);
|
||||
for (let offset = buffer.length - 22; offset >= minEndOffset; offset--) {
|
||||
if (buffer.readUInt32LE(offset) !== 0x06054b50) continue;
|
||||
interface ArchiveStreamEntryLike extends ArchiveEntryLike {
|
||||
stream: () => Readable;
|
||||
}
|
||||
|
||||
function findZipEndRecordOffset(buffer: Buffer, label: string): number {
|
||||
const minEndOffset = Math.max(0, buffer.length - ZIP_END_RECORD_MAX_SIZE);
|
||||
for (let offset = buffer.length - ZIP_END_RECORD_MIN_SIZE; offset >= minEndOffset; offset--) {
|
||||
if (buffer.readUInt32LE(offset) !== ZIP_END_SIGNATURE) continue;
|
||||
const commentLength = buffer.readUInt16LE(offset + 20);
|
||||
if (offset + 22 + commentLength !== buffer.length) continue;
|
||||
const entries = buffer.readUInt16LE(offset + 10);
|
||||
if (entries === 0xffff || entries > maxEntries) {
|
||||
throw new Error(`${label}文件数量超过限制(最多 ${maxEntries} 个)`);
|
||||
}
|
||||
return;
|
||||
if (offset + ZIP_END_RECORD_MIN_SIZE + commentLength !== buffer.length) continue;
|
||||
return offset;
|
||||
}
|
||||
throw new Error(`${label}文件结构无效`);
|
||||
}
|
||||
|
||||
function assertZipEntryCountValue(entries: bigint, maxEntries: number, label: string): void {
|
||||
if (entries > BigInt(maxEntries)) {
|
||||
throw new Error(`${label}文件数量超过限制(最多 ${maxEntries} 个)`);
|
||||
}
|
||||
}
|
||||
|
||||
function usesZip64(buffer: Buffer, endOffset: number): boolean {
|
||||
return buffer.readUInt16LE(endOffset + 10) === 0xffff
|
||||
|| buffer.readUInt32LE(endOffset + 12) === 0xffffffff
|
||||
|| buffer.readUInt32LE(endOffset + 16) === 0xffffffff;
|
||||
}
|
||||
|
||||
function readZip64EntryCount(buffer: Buffer, endOffset: number, label: string): bigint {
|
||||
const locatorOffset = endOffset - ZIP64_LOCATOR_SIZE;
|
||||
if (locatorOffset < 0 || buffer.readUInt32LE(locatorOffset) !== ZIP64_LOCATOR_SIGNATURE) {
|
||||
throw new Error(`${label}文件结构无效`);
|
||||
}
|
||||
const recordOffset = buffer.readBigUInt64LE(locatorOffset + 8);
|
||||
if (recordOffset > BigInt(Number.MAX_SAFE_INTEGER)) throw new Error(`${label}文件结构无效`);
|
||||
const offset = Number(recordOffset);
|
||||
if (offset < 0 || offset + ZIP64_END_RECORD_MIN_SIZE > buffer.length) throw new Error(`${label}文件结构无效`);
|
||||
if (buffer.readUInt32LE(offset) !== ZIP64_END_SIGNATURE) throw new Error(`${label}文件结构无效`);
|
||||
return buffer.readBigUInt64LE(offset + 32);
|
||||
}
|
||||
|
||||
export function assertZipEntryCount(buffer: Buffer, maxEntries: number, label: string): number {
|
||||
const endOffset = findZipEndRecordOffset(buffer, label);
|
||||
const entries = usesZip64(buffer, endOffset)
|
||||
? readZip64EntryCount(buffer, endOffset, label)
|
||||
: BigInt(buffer.readUInt16LE(endOffset + 10));
|
||||
assertZipEntryCountValue(entries, maxEntries, label);
|
||||
return buffer.length - endOffset;
|
||||
}
|
||||
|
||||
export async function inspectZipFile(filePath: string, maxEntries: number, label: string): Promise<number> {
|
||||
const file = await open(filePath, 'r');
|
||||
try {
|
||||
const { size } = await file.stat();
|
||||
if (size < ZIP_END_RECORD_MIN_SIZE) throw new Error(`${label}文件结构无效`);
|
||||
const tailLength = Math.min(size, ZIP_END_RECORD_MAX_SIZE);
|
||||
const tail = Buffer.allocUnsafe(tailLength);
|
||||
const tailRead = await file.read(tail, 0, tailLength, size - tailLength);
|
||||
if (tailRead.bytesRead !== tailLength) throw new Error(`${label}文件结构无效`);
|
||||
const relativeEndOffset = findZipEndRecordOffset(tail, label);
|
||||
const absoluteEndOffset = size - tailLength + relativeEndOffset;
|
||||
let entries: bigint;
|
||||
if (usesZip64(tail, relativeEndOffset)) {
|
||||
const locator = Buffer.allocUnsafe(ZIP64_LOCATOR_SIZE);
|
||||
if (absoluteEndOffset < ZIP64_LOCATOR_SIZE) throw new Error(`${label}文件结构无效`);
|
||||
const locatorRead = await file.read(locator, 0, locator.length, absoluteEndOffset - ZIP64_LOCATOR_SIZE);
|
||||
if (locatorRead.bytesRead !== locator.length) throw new Error(`${label}文件结构无效`);
|
||||
if (locator.readUInt32LE(0) !== ZIP64_LOCATOR_SIGNATURE) throw new Error(`${label}文件结构无效`);
|
||||
const recordOffset = locator.readBigUInt64LE(8);
|
||||
if (recordOffset > BigInt(Number.MAX_SAFE_INTEGER)) throw new Error(`${label}文件结构无效`);
|
||||
const record = Buffer.allocUnsafe(ZIP64_END_RECORD_MIN_SIZE);
|
||||
const recordRead = await file.read(record, 0, record.length, Number(recordOffset));
|
||||
if (recordRead.bytesRead !== record.length) throw new Error(`${label}文件结构无效`);
|
||||
if (record.readUInt32LE(0) !== ZIP64_END_SIGNATURE) throw new Error(`${label}文件结构无效`);
|
||||
entries = record.readBigUInt64LE(32);
|
||||
} else {
|
||||
entries = BigInt(tail.readUInt16LE(relativeEndOffset + 10));
|
||||
}
|
||||
assertZipEntryCountValue(entries, maxEntries, label);
|
||||
return size - absoluteEndOffset;
|
||||
} finally {
|
||||
await file.close();
|
||||
}
|
||||
}
|
||||
|
||||
export function assertArchiveLimits(
|
||||
entries: ArchiveEntryLike[],
|
||||
maxEntries: number,
|
||||
maxUncompressedSize: number,
|
||||
maxUncompressedSize: number | undefined,
|
||||
label: string,
|
||||
): void {
|
||||
if (entries.length > maxEntries) {
|
||||
@@ -46,18 +135,72 @@ export function assertArchiveLimits(
|
||||
const size = Number(entry.header?.size ?? 0);
|
||||
if (!Number.isSafeInteger(size) || size < 0) throw new Error(`${label}包含无效文件大小`);
|
||||
total += size;
|
||||
if (total > maxUncompressedSize) {
|
||||
if (maxUncompressedSize !== undefined && total > maxUncompressedSize) {
|
||||
throw new Error(`${label}解压后总大小超过限制`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export function assertStoredImageBudget(referenceCount: number, totalBytes: number): void {
|
||||
export function assertStoredImageBudget(referenceCount: number): void {
|
||||
if (referenceCount > OUTBOUND_IMPORT_MAX_IMAGE_REFERENCES) {
|
||||
throw new Error(`出库图片引用总数超过限制(最多 ${OUTBOUND_IMPORT_MAX_IMAGE_REFERENCES} 张)`);
|
||||
}
|
||||
if (totalBytes > OUTBOUND_IMPORT_MAX_STORED_IMAGE_SIZE) {
|
||||
throw new Error(`出库图片按引用累计大小超过限制(最多 ${OUTBOUND_IMPORT_MAX_STORED_IMAGE_SIZE / 1024 / 1024}MB)`);
|
||||
}
|
||||
|
||||
export async function readArchiveEntryBuffer(
|
||||
entry: Pick<ArchiveStreamEntryLike, 'stream'>,
|
||||
maxBytes: number,
|
||||
overflowMessage: string,
|
||||
onProgress?: () => Promise<void>,
|
||||
): Promise<Buffer> {
|
||||
const stream = entry.stream();
|
||||
const chunks: Buffer[] = [];
|
||||
let total = 0;
|
||||
let progressBytes = 0;
|
||||
try {
|
||||
for await (const chunk of stream) {
|
||||
const data = Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk);
|
||||
total += data.length;
|
||||
if (total > maxBytes) throw new Error(overflowMessage);
|
||||
chunks.push(data);
|
||||
progressBytes += data.length;
|
||||
if (onProgress && progressBytes >= ARCHIVE_PROGRESS_INTERVAL_BYTES) {
|
||||
progressBytes = 0;
|
||||
await onProgress();
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
stream.destroy();
|
||||
throw error;
|
||||
}
|
||||
return Buffer.concat(chunks, total);
|
||||
}
|
||||
|
||||
export async function assertArchiveActualSize(
|
||||
entries: ArchiveStreamEntryLike[],
|
||||
maxUncompressedSize: number,
|
||||
label: string,
|
||||
onProgress?: () => Promise<void>,
|
||||
): Promise<void> {
|
||||
let total = 0;
|
||||
let progressBytes = 0;
|
||||
for (const entry of entries) {
|
||||
const stream = entry.stream();
|
||||
try {
|
||||
for await (const chunk of stream) {
|
||||
const size = Buffer.isBuffer(chunk) ? chunk.length : Buffer.byteLength(chunk);
|
||||
total += size;
|
||||
if (total > maxUncompressedSize) throw new Error(`${label}解压后总大小超过限制`);
|
||||
progressBytes += size;
|
||||
if (onProgress && progressBytes >= ARCHIVE_PROGRESS_INTERVAL_BYTES) {
|
||||
progressBytes = 0;
|
||||
await onProgress();
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
stream.destroy();
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user