The next-generation file converter. Open source, fully local* and free forever.
import { formatError } from "#lib/util/error";
import { error, log } from "#lib/util/logger.svelte";
import { downloadZip } from "client-zip";
import { unzip } from "fflate";
export interface ZipEntry {
filename: string;
data: Uint8Array;
}
export async function extractZip(file: File): Promise<ZipEntry[]> {
log(["zip"], `extracting zip: ${file.name}`);
const arrayBuffer = await file.arrayBuffer();
const uint8Array = new Uint8Array(arrayBuffer);
return new Promise((resolve, reject) => {
unzip(uint8Array, (err, unzipped) => {
if (err) {
error(["zip"], `failed to extract zip: ${formatError(err)}`);
reject(new Error(`Failed to extract zip: ${formatError(err)}`));
return;
}
const entries = Object.entries(unzipped)
.filter(([filename]) => !ignoreEntry(filename))
.map(([filename, data]) => ({
filename,
data,
}));
log(
["zip"],
`extracted ${entries.length} entries from ${file.name}`,
);
resolve(entries);
});
});
}
export async function createZip(files: File[]): Promise<Uint8Array> {
log(["zip"], `creating zip with ${files.length} files`);
const zipBlob = await downloadZip(files).blob();
return new Uint8Array(await zipBlob.arrayBuffer());
}
export function ignoreEntry(filename: string): boolean {
const parts = filename.replace(/\\/g, "/").split("/");
return (
parts.some(
(part) => part.startsWith(".") || part.toUpperCase() === "__MACOSX",
) || filename.endsWith("/")
);
}
export function formatFilename(format: string, file: File | string) {
const now = new Date();
const iso = now.toISOString();
const date = iso.split("T")[0];
const time = iso.split("T")[1].split(".")[0].replace(/:/g, "-");
const unix = now.getTime().toString();
const baseName =
typeof file === "string"
? file.replace(/\.[^/.]+$/, "")
: file.name.replace(/\.[^/.]+$/, "");
const originalExtension =
typeof file === "string"
? file.split(".").pop()!
: file.name.split(".").pop()!;
return format
.replace(/%datetime%/g, iso)
.replace(/%date%/g, date)
.replace(/%time%/g, time)
.replace(/%unix%/g, unix)
.replace(/%name%/g, baseName)
.replace(/%extension%/g, originalExtension);
}
export const formatBytes = (bytes: number): string => {
if (!Number.isFinite(bytes) || bytes <= 0) return "0 B";
if (bytes < 1024) return `${bytes} B`;
const units = ["KB", "MB", "GB", "TB"];
let value = bytes;
let unitIndex = -1;
while (value >= 1024 && unitIndex < units.length - 1) {
value /= 1024;
unitIndex++;
}
return `${value.toFixed(value >= 100 ? 0 : value >= 10 ? 1 : 2)} ${units[unitIndex]}`;
};