Files
bhursey cd52a2f82f Initial commit: Luminary photo catalog and RAW editor
Browser-based photo catalog with an optional team server (SQLite + LibRaw).
Local database, uploaded photos, build output and dependencies are gitignored.

🤖 Generated with Codebuff
Co-Authored-By: Codebuff <noreply@codebuff.com>
2026-11-23 15:19:25 +00:00

124 lines
3.9 KiB
JavaScript

/**
* Minimal stored-ZIP writer for server-side gallery downloads. JPEGs are
* already compressed, so entries are stored rather than deflated.
*/
const MAX_ENTRIES = 0xffff;
const MAX_SIZE = 0xffffffff;
const CRC_TABLE = (() => {
const table = new Uint32Array(256);
for (let i = 0; i < 256; i++) {
let c = i;
for (let k = 0; k < 8; k++) c = c & 1 ? 0xedb88320 ^ (c >>> 1) : c >>> 1;
table[i] = c >>> 0;
}
return table;
})();
function crc32(bytes) {
let c = 0xffffffff;
for (let i = 0; i < bytes.length; i++) c = CRC_TABLE[(c ^ bytes[i]) & 0xff] ^ (c >>> 8);
return (c ^ 0xffffffff) >>> 0;
}
function dosStamp(date) {
const year = Math.min(2107, Math.max(1980, date.getFullYear()));
const time = (date.getHours() << 11) | (date.getMinutes() << 5) | (date.getSeconds() >> 1);
const day = ((year - 1980) << 9) | ((date.getMonth() + 1) << 5) | date.getDate();
return { time, day };
}
function uniqueName(name, used) {
const clean = String(name || 'photo.jpg')
.replace(/[\\/]+/g, '-')
.replace(/[\u0000-\u001f]/g, '')
.trim() || 'photo.jpg';
if (!used.has(clean.toLowerCase())) {
used.add(clean.toLowerCase());
return clean;
}
const dot = clean.lastIndexOf('.');
const stem = dot > 0 ? clean.slice(0, dot) : clean;
const ext = dot > 0 ? clean.slice(dot) : '';
let n = 2;
while (used.has(`${stem} (${n})${ext}`.toLowerCase())) n += 1;
const result = `${stem} (${n})${ext}`;
used.add(result.toLowerCase());
return result;
}
export function createZip(entries, date = new Date()) {
if (!entries.length) throw new Error('Nothing to archive');
if (entries.length > MAX_ENTRIES) throw new Error('Too many files for one ZIP archive');
const { time, day } = dosStamp(date);
const locals = [];
const central = [];
const used = new Set();
let offset = 0;
for (const entry of entries) {
if (!Buffer.isBuffer(entry.data)) throw new Error('ZIP entries must contain buffers');
const name = Buffer.from(uniqueName(entry.name, used), 'utf8');
const data = entry.data;
if (data.length > MAX_SIZE) throw new Error(`${entry.name} is too large for a ZIP archive`);
const crc = crc32(data);
const local = Buffer.alloc(30);
local.writeUInt32LE(0x04034b50, 0);
local.writeUInt16LE(20, 4);
local.writeUInt16LE(0x0800, 6);
local.writeUInt16LE(0, 8);
local.writeUInt16LE(time, 10);
local.writeUInt16LE(day, 12);
local.writeUInt32LE(crc, 14);
local.writeUInt32LE(data.length, 18);
local.writeUInt32LE(data.length, 22);
local.writeUInt16LE(name.length, 26);
local.writeUInt16LE(0, 28);
locals.push(local, name, data);
central.push({ name, crc, size: data.length, offset });
offset += local.length + name.length + data.length;
}
const centralStart = offset;
let centralSize = 0;
for (const file of central) {
const record = Buffer.alloc(46);
record.writeUInt32LE(0x02014b50, 0);
record.writeUInt16LE(20, 4);
record.writeUInt16LE(20, 6);
record.writeUInt16LE(0x0800, 8);
record.writeUInt16LE(0, 10);
record.writeUInt16LE(time, 12);
record.writeUInt16LE(day, 14);
record.writeUInt32LE(file.crc, 16);
record.writeUInt32LE(file.size, 20);
record.writeUInt32LE(file.size, 24);
record.writeUInt16LE(file.name.length, 28);
record.writeUInt16LE(0, 30);
record.writeUInt16LE(0, 32);
record.writeUInt16LE(0, 34);
record.writeUInt16LE(0, 36);
record.writeUInt32LE(0, 38);
record.writeUInt32LE(file.offset, 42);
locals.push(record, file.name);
centralSize += record.length + file.name.length;
}
const end = Buffer.alloc(22);
end.writeUInt32LE(0x06054b50, 0);
end.writeUInt16LE(0, 4);
end.writeUInt16LE(0, 6);
end.writeUInt16LE(central.length, 8);
end.writeUInt16LE(central.length, 10);
end.writeUInt32LE(centralSize, 12);
end.writeUInt32LE(centralStart, 16);
end.writeUInt16LE(0, 20);
locals.push(end);
return Buffer.concat(locals);
}