This repository has been archived on 2025-07-31. You can view files and clone it, but cannot push or open issues or pull requests.
Files
darktable-ghost-cms-publish/src/files.ts
Nikolaos Karaolidis 4c36307b01 Add source code
Signed-off-by: Nikolaos Karaolidis <nick@karaolidis.com>
2025-02-23 13:24:50 +00:00

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;
};