Files
NianAIGC/lib/client/zip-download.ts
2026-09-23 13:33:47 +08:00

158 lines
5.7 KiB
TypeScript

type DownloadableFile = {
name: string;
url: string;
};
type ZipEntry = {
name: Uint8Array;
data: Uint8Array;
crc: number;
offset: number;
};
export async function downloadFilesAsZip(files: DownloadableFile[], archiveName: string) {
if (!files.length) return;
const usedNames = new Set<string>();
const entries: ZipEntry[] = [];
for (let index = 0; index < files.length; index += 1) {
const file = files[index];
const response = await fetch(file.url, { credentials: "same-origin" });
if (!response.ok) throw new Error(`下载第 ${index + 1} 个图层失败`);
const data = new Uint8Array(await response.arrayBuffer());
const safeName = uniqueFileName(imageFileName(sanitizeFileName(file.name) || `layer-${index}`, data), usedNames);
entries.push({ name: new TextEncoder().encode(safeName), data, crc: crc32(data), offset: 0 });
}
const blob = zipBlob(entries);
const href = URL.createObjectURL(blob);
const anchor = document.createElement("a");
anchor.href = href;
anchor.download = sanitizeFileName(archiveName) || "seedream-layers.zip";
document.body.appendChild(anchor);
anchor.click();
anchor.remove();
window.setTimeout(() => URL.revokeObjectURL(href), 1000);
}
function zipBlob(entries: ZipEntry[]) {
const localParts: Uint8Array[] = [];
let offset = 0;
for (const entry of entries) {
entry.offset = offset;
const header = new Uint8Array(30 + entry.name.length);
const view = new DataView(header.buffer);
view.setUint32(0, 0x04034b50, true);
view.setUint16(4, 20, true);
view.setUint16(6, 0x0800, true);
view.setUint16(8, 0, true);
view.setUint32(14, entry.crc, true);
view.setUint32(18, entry.data.length, true);
view.setUint32(22, entry.data.length, true);
view.setUint16(26, entry.name.length, true);
header.set(entry.name, 30);
localParts.push(header, entry.data);
offset += header.length + entry.data.length;
}
const centralOffset = offset;
const centralParts: Uint8Array[] = [];
for (const entry of entries) {
const header = new Uint8Array(46 + entry.name.length);
const view = new DataView(header.buffer);
view.setUint32(0, 0x02014b50, true);
view.setUint16(4, 20, true);
view.setUint16(6, 20, true);
view.setUint16(8, 0x0800, true);
view.setUint16(10, 0, true);
view.setUint32(16, entry.crc, true);
view.setUint32(20, entry.data.length, true);
view.setUint32(24, entry.data.length, true);
view.setUint16(28, entry.name.length, true);
view.setUint32(42, entry.offset, true);
header.set(entry.name, 46);
centralParts.push(header);
offset += header.length;
}
const end = new Uint8Array(22);
const endView = new DataView(end.buffer);
endView.setUint32(0, 0x06054b50, true);
endView.setUint16(8, entries.length, true);
endView.setUint16(10, entries.length, true);
endView.setUint32(12, offset - centralOffset, true);
endView.setUint32(16, centralOffset, true);
return new Blob([...localParts, ...centralParts, end].map(arrayBufferCopy), { type: "application/zip" });
}
function arrayBufferCopy(value: Uint8Array) {
const copy = new Uint8Array(value.byteLength);
copy.set(value);
return copy.buffer;
}
function sanitizeFileName(value: string) {
return value.trim().replace(/[\\/:*?"<>|\u0000-\u001f]/g, "-").replace(/^\.+/, "").slice(0, 120);
}
function imageFileName(name: string, data: Uint8Array) {
let extensions: string[] | undefined;
let preferred: string | undefined;
if (data.length >= 8 && data[0] === 0x89 && data[1] === 0x50 && data[2] === 0x4e && data[3] === 0x47 && data[4] === 0x0d && data[5] === 0x0a && data[6] === 0x1a && data[7] === 0x0a) {
extensions = ["png"];
preferred = "png";
} else if (data.length >= 3 && data[0] === 0xff && data[1] === 0xd8 && data[2] === 0xff) {
extensions = ["jpg", "jpeg", "jpe"];
preferred = "jpg";
} else if (data.length >= 14 && asciiAt(data, 0, "RIFF") && asciiAt(data, 8, "WEBPVP")) {
extensions = ["webp"];
preferred = "webp";
} else if (data.length >= 6 && (asciiAt(data, 0, "GIF87a") || asciiAt(data, 0, "GIF89a"))) {
extensions = ["gif"];
preferred = "gif";
} else if (data.length >= 2 && asciiAt(data, 0, "BM")) {
extensions = ["bmp"];
preferred = "bmp";
} else if (data.length >= 4 && data[0] === 0 && data[1] === 0 && (data[2] === 1 || data[2] === 2) && data[3] === 0) {
extensions = ["ico"];
preferred = "ico";
}
if (!preferred || !extensions) return name;
const dot = name.lastIndexOf(".");
const current = dot > 0 ? name.slice(dot + 1) : "";
if (extensions.includes(current.toLowerCase())) return name;
return `${dot > 0 ? name.slice(0, dot) : name}.${preferred}`;
}
function asciiAt(data: Uint8Array, offset: number, value: string) {
for (let index = 0; index < value.length; index += 1) {
if (data[offset + index] !== value.charCodeAt(index)) return false;
}
return true;
}
function uniqueFileName(name: string, used: Set<string>) {
if (!used.has(name)) {
used.add(name);
return name;
}
const dot = name.lastIndexOf(".");
const base = dot > 0 ? name.slice(0, dot) : name;
const extension = dot > 0 ? name.slice(dot) : "";
let index = 2;
while (used.has(`${base}-${index}${extension}`)) index += 1;
const result = `${base}-${index}${extension}`;
used.add(result);
return result;
}
const crcTable = Array.from({ length: 256 }, (_, index) => {
let value = index;
for (let bit = 0; bit < 8; bit += 1) value = (value & 1) ? 0xedb88320 ^ (value >>> 1) : value >>> 1;
return value >>> 0;
});
function crc32(data: Uint8Array) {
let crc = 0xffffffff;
for (const value of data) crc = crcTable[(crc ^ value) & 0xff] ^ (crc >>> 8);
return (crc ^ 0xffffffff) >>> 0;
}