// Self-contained ZIP and XLSX generation for offline exports.
const zlib = require('node:zlib');
const crcTable = Array.from({ length: 256 }, (_, start) => {
let value = start;
for (let bit = 0; bit < 8; bit++) value = value & 1 ? (value >>> 1) ^ 0xedb88320 : value >>> 1;
return value >>> 0;
});
function crc32(data) {
let value = 0xffffffff;
for (const byte of data) value = crcTable[(value ^ byte) & 0xff] ^ (value >>> 8);
return (value ^ 0xffffffff) >>> 0;
}
function zip(entries) {
const localParts = [];
const centralParts = [];
let offset = 0;
const now = new Date();
const dosTime = (now.getHours() << 11) | (now.getMinutes() << 5) | (now.getSeconds() >> 1);
const dosDate = (Math.max(1980, now.getFullYear()) - 1980) << 9 | ((now.getMonth() + 1) << 5) | now.getDate();
for (const entry of entries) {
const name = Buffer.from(entry.name.replaceAll('\\', '/'), 'utf8');
if (!name.length || name.length > 65535 || entry.name.includes('..')) throw new Error('导出文件名无效');
const data = Buffer.isBuffer(entry.data) ? entry.data : Buffer.from(entry.data, 'utf8');
const deflated = zlib.deflateRawSync(data);
const compressed = deflated.length < data.length ? deflated : data;
const method = compressed === data ? 0 : 8;
const checksum = crc32(data);
const local = Buffer.alloc(30);
local.writeUInt32LE(0x04034b50, 0);
local.writeUInt16LE(20, 4);
local.writeUInt16LE(0x0800, 6); // UTF-8 filenames
local.writeUInt16LE(method, 8);
local.writeUInt16LE(dosTime, 10);
local.writeUInt16LE(dosDate, 12);
local.writeUInt32LE(checksum, 14);
local.writeUInt32LE(compressed.length, 18);
local.writeUInt32LE(data.length, 22);
local.writeUInt16LE(name.length, 26);
localParts.push(local, name, compressed);
const central = Buffer.alloc(46);
central.writeUInt32LE(0x02014b50, 0);
central.writeUInt16LE(20, 4);
central.writeUInt16LE(20, 6);
central.writeUInt16LE(0x0800, 8);
central.writeUInt16LE(method, 10);
central.writeUInt16LE(dosTime, 12);
central.writeUInt16LE(dosDate, 14);
central.writeUInt32LE(checksum, 16);
central.writeUInt32LE(compressed.length, 20);
central.writeUInt32LE(data.length, 24);
central.writeUInt16LE(name.length, 28);
central.writeUInt32LE(offset, 42);
centralParts.push(central, name);
offset += local.length + name.length + compressed.length;
}
if (entries.length > 65535) throw new Error('单次导出记录过多');
const centralLength = centralParts.reduce((sum, part) => sum + part.length, 0);
const end = Buffer.alloc(22);
end.writeUInt32LE(0x06054b50, 0);
end.writeUInt16LE(entries.length, 8);
end.writeUInt16LE(entries.length, 10);
end.writeUInt32LE(centralLength, 12);
end.writeUInt32LE(offset, 16);
return Buffer.concat([...localParts, ...centralParts, end]);
}
const xml = value => String(value ?? '').replace(/[&<>"']/g, char => ({ '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' })[char]);
function excel(records) {
const headings = ['模块 SN', 'FPV 编号', '机型', '登记人 / 备注', '登记时间', '照片 1', '照片 2', '档案 ID'];
const rows = [headings, ...records.map(record => [
record.serialNo, record.uasCode, record.modelName, record.ownerName,
record.createdAt, record.photo1 ? '有' : '无', record.photo2 ? '有' : '无', record.id,
])];
const rowXml = rows.map((row, rowIndex) => `${row.map((value, columnIndex) => {
const cell = `${String.fromCharCode(65 + columnIndex)}${rowIndex + 1}`;
return `${xml(value)}`;
}).join('')}
`).join('');
const lastRow = rows.length;
const worksheet = `${[25, 19, 20, 24, 25, 10, 10, 40].map((width, i) => ``).join('')}${rowXml}`;
return zip([
{ name: '[Content_Types].xml', data: '' },
{ name: '_rels/.rels', data: '' },
{ name: 'xl/workbook.xml', data: '' },
{ name: 'xl/_rels/workbook.xml.rels', data: '' },
{ name: 'xl/styles.xml', data: '' },
{ name: 'xl/worksheets/sheet1.xml', data: worksheet },
]);
}
module.exports = { zip, excel };