67 lines
1.5 KiB
TypeScript
67 lines
1.5 KiB
TypeScript
import { basename, extname } from "path";
|
|
|
|
export interface FileInfo {
|
|
jpegPath: string;
|
|
jpegSize: number;
|
|
rawPath?: string;
|
|
rawSize?: number;
|
|
}
|
|
|
|
export const getBasenameWithoutExtension = (path: string): string => {
|
|
const base = basename(path);
|
|
const extension = extname(path);
|
|
return base.slice(0, -extension.length);
|
|
};
|
|
|
|
export const getBasenameWithExtension = (path: string): string => {
|
|
return basename(path);
|
|
};
|
|
|
|
export const prepareFiles = async (files: string[]): Promise<FileInfo[]> => {
|
|
if (files.length > 10) {
|
|
throw new Error("Up to 10 files are allowed at a time.");
|
|
}
|
|
|
|
const parsedFiles: FileInfo[] = [];
|
|
|
|
for (const pair of files) {
|
|
const parts = pair.split(/(?<!\\):/);
|
|
const jpegPath = parts[0].replace(/\\:/g, ":");
|
|
const rawPath = parts[1]?.replace(/\\:/g, ":");
|
|
|
|
const jpegFile = Bun.file(jpegPath);
|
|
if (!(await jpegFile.exists())) {
|
|
throw new Error(`JPEG file not found: ${jpegPath}`);
|
|
}
|
|
|
|
const jpegSize = jpegFile.size;
|
|
|
|
if (!rawPath) {
|
|
parsedFiles.push({
|
|
jpegPath,
|
|
jpegSize,
|
|
rawPath: undefined,
|
|
rawSize: undefined,
|
|
});
|
|
|
|
continue;
|
|
}
|
|
|
|
const rawFile = Bun.file(rawPath);
|
|
if (!(await rawFile.exists())) {
|
|
throw new Error(`RAW file not found: ${rawPath}`);
|
|
}
|
|
|
|
const rawSize = rawFile.size;
|
|
|
|
parsedFiles.push({
|
|
jpegPath,
|
|
jpegSize,
|
|
rawPath: rawPath,
|
|
rawSize: rawSize,
|
|
});
|
|
}
|
|
|
|
return parsedFiles;
|
|
};
|