Add darktable ghost publish plugin
Signed-off-by: Nikolaos Karaolidis <nick@karaolidis.com>
This commit is contained in:
110
hosts/common/configs/user/gui/darktable/publish/src/api.ts
Normal file
110
hosts/common/configs/user/gui/darktable/publish/src/api.ts
Normal file
@@ -0,0 +1,110 @@
|
||||
import { sign } from "jsonwebtoken";
|
||||
import { file } from "bun";
|
||||
|
||||
const getAdminApiKey = async () => {
|
||||
const keyPath = process.env.GHOST_ADMIN_API_KEY_PATH;
|
||||
if (!keyPath) {
|
||||
throw new Error(
|
||||
"Environment variable GHOST_ADMIN_API_KEY_PATH is not set.",
|
||||
);
|
||||
}
|
||||
|
||||
const keyFile = file(keyPath);
|
||||
if (!(await keyFile.exists())) {
|
||||
throw new Error(`Key file not found at path: ${keyPath}`);
|
||||
}
|
||||
|
||||
return await keyFile.text();
|
||||
};
|
||||
|
||||
const getEndpoint = () => {
|
||||
const endpoint = process.env.GHOST_URL;
|
||||
if (!endpoint) {
|
||||
throw new Error("Environment variable GHOST_URL is not set.");
|
||||
}
|
||||
|
||||
return endpoint;
|
||||
};
|
||||
|
||||
const createJwt = (key: string) => {
|
||||
const [id, secret] = key.split(":");
|
||||
if (!id || !secret) {
|
||||
throw new Error("Invalid API key format. Expected format: {id}:{secret}");
|
||||
}
|
||||
|
||||
return sign({}, Buffer.from(secret, "hex"), {
|
||||
keyid: id,
|
||||
algorithm: "HS256",
|
||||
expiresIn: "5m",
|
||||
audience: `/admin/`,
|
||||
});
|
||||
};
|
||||
|
||||
const upload = async (
|
||||
slug: string,
|
||||
path: string,
|
||||
type: string | undefined,
|
||||
): Promise<any> => {
|
||||
const endpoint = getEndpoint();
|
||||
const fullEndpoint = `${endpoint}${slug}`;
|
||||
|
||||
const key = await getAdminApiKey();
|
||||
const token = createJwt(key);
|
||||
|
||||
const f = Bun.file(path, { type });
|
||||
const formData = new FormData();
|
||||
formData.append("file", f);
|
||||
|
||||
const response = await fetch(fullEndpoint, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
Authorization: `Ghost ${token}`,
|
||||
},
|
||||
body: formData,
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(
|
||||
`Failed to upload to ${fullEndpoint}: ${response.status} ${response.statusText}`,
|
||||
);
|
||||
}
|
||||
|
||||
return await response.json();
|
||||
};
|
||||
|
||||
export const uploadImage = async (imagePath: string): Promise<string> => {
|
||||
const slug = `/ghost/api/admin/images/upload`;
|
||||
return (await upload(slug, imagePath, "image/jpeg")).images[0].url;
|
||||
};
|
||||
|
||||
export const uploadFile = async (filePath: string): Promise<string> => {
|
||||
const slug = `/ghost/api/admin/files/upload`;
|
||||
return (await upload(slug, filePath, undefined)).files[0].url;
|
||||
};
|
||||
|
||||
export const uploadPost = async (post: any): Promise<string> => {
|
||||
const endpoint = getEndpoint();
|
||||
const fullEndpoint = `${endpoint}/ghost/api/admin/posts`;
|
||||
|
||||
const key = await getAdminApiKey();
|
||||
const token = createJwt(key);
|
||||
|
||||
const response = await fetch(fullEndpoint, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
Authorization: `Ghost ${token}`,
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: JSON.stringify({
|
||||
posts: [post],
|
||||
}),
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(
|
||||
`Failed to upload to ${fullEndpoint}: ${response.status} ${response.statusText}`,
|
||||
);
|
||||
}
|
||||
|
||||
return (await response.json()).posts[0].url;
|
||||
};
|
48
hosts/common/configs/user/gui/darktable/publish/src/exif.ts
Normal file
48
hosts/common/configs/user/gui/darktable/publish/src/exif.ts
Normal file
@@ -0,0 +1,48 @@
|
||||
import { exiftool } from "exiftool-vendored";
|
||||
import type { FileInfo } from "./files";
|
||||
|
||||
export interface ShootingConditions {
|
||||
make: string;
|
||||
model: string;
|
||||
lensMake: string;
|
||||
lensModel: string;
|
||||
focalLength: string;
|
||||
focalLength35: string;
|
||||
shutterSpeed: string;
|
||||
fStop: string;
|
||||
iso: string;
|
||||
timestamp: string;
|
||||
}
|
||||
|
||||
export const extractShootingConditions = async (
|
||||
fileInfo: FileInfo,
|
||||
): Promise<ShootingConditions> => {
|
||||
const path = fileInfo.rawPath ?? fileInfo.jpegPath;
|
||||
|
||||
try {
|
||||
const exifData = await exiftool.read(path);
|
||||
|
||||
return {
|
||||
make: exifData.Make ?? "Unknown",
|
||||
model: exifData.Model ?? "Unknown",
|
||||
lensMake: exifData.LensMake ?? "Unknown",
|
||||
lensModel: exifData.LensModel ?? "Unknown",
|
||||
focalLength: exifData.FocalLength ?? "Unknown",
|
||||
focalLength35: exifData.FocalLengthIn35mmFormat ?? "Unknown",
|
||||
shutterSpeed: exifData.ExposureTime ?? "Unknown",
|
||||
fStop: exifData.FNumber?.toString() ?? "Unknown",
|
||||
iso: exifData.ISO?.toString() ?? "Unknown",
|
||||
timestamp: new Date(
|
||||
(exifData.CreateDate?.toString() as string).replace(/\./g, ":"),
|
||||
).toISOString(),
|
||||
};
|
||||
} catch (error: any) {
|
||||
throw new Error(
|
||||
`Failed to extract EXIF data from ${path}: ${error.message}`,
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
export const createImageCaption = (exif: ShootingConditions) => {
|
||||
return `${exif.make} ${exif.model}, ${exif.lensMake} ${exif.lensModel} @ ${exif.focalLength} (${exif.focalLength35}), ${exif.shutterSpeed} s, f/${exif.fStop}, ISO ${exif.iso}`;
|
||||
};
|
66
hosts/common/configs/user/gui/darktable/publish/src/files.ts
Normal file
66
hosts/common/configs/user/gui/darktable/publish/src/files.ts
Normal file
@@ -0,0 +1,66 @@
|
||||
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;
|
||||
};
|
115
hosts/common/configs/user/gui/darktable/publish/src/index.ts
Normal file
115
hosts/common/configs/user/gui/darktable/publish/src/index.ts
Normal file
@@ -0,0 +1,115 @@
|
||||
import { Command } from "commander";
|
||||
import { createFileNode, createImageNode, createHeadingNode } from "./lexical";
|
||||
import {
|
||||
extractShootingConditions,
|
||||
createImageCaption,
|
||||
} from "./exif";
|
||||
import { uploadFile, uploadImage, uploadPost } from "./api";
|
||||
import { getBasenameWithExtension, prepareFiles } from "./files";
|
||||
|
||||
new Command()
|
||||
.name("darktable-publish")
|
||||
.description("Publish files to GHOST CMS with optional metadata.")
|
||||
.option("-t, --title [string]", "Specify the title")
|
||||
.option("-s, --slug [string]", "Specify the slug")
|
||||
.option("-k, --keywords [string...]", "Specify blog post keywords (tags)")
|
||||
.argument("<files...>", "Files to process")
|
||||
.action(async (files, options) => {
|
||||
if (!options.title) {
|
||||
throw new Error("Please specify a title.");
|
||||
}
|
||||
|
||||
if (!options.slug) {
|
||||
throw new Error("Please specify a slug.");
|
||||
}
|
||||
|
||||
const parsedFiles = await prepareFiles(files);
|
||||
|
||||
const [
|
||||
shootingConditions,
|
||||
uploadedJpegImages,
|
||||
uploadedJpegFiles,
|
||||
uploadedRawFiles,
|
||||
] = await Promise.all([
|
||||
Promise.all(parsedFiles.map(extractShootingConditions)),
|
||||
Promise.all(parsedFiles.map((f) => uploadImage(f.jpegPath))),
|
||||
Promise.all(parsedFiles.map((f) => uploadFile(f.jpegPath))),
|
||||
Promise.all(
|
||||
parsedFiles.map((f) =>
|
||||
f.rawPath ? uploadFile(f.rawPath) : Promise.resolve(undefined),
|
||||
),
|
||||
),
|
||||
]);
|
||||
|
||||
const aggregatedFiles = parsedFiles.map((file, index) => ({
|
||||
...file,
|
||||
shootingConditions: shootingConditions[index],
|
||||
uploadedJpegImage: uploadedJpegImages[index],
|
||||
uploadedJpegFile: uploadedJpegFiles[index],
|
||||
uploadedRawFile: uploadedRawFiles[index],
|
||||
}));
|
||||
|
||||
const result: any = {
|
||||
root: {
|
||||
children: [],
|
||||
direction: "ltr",
|
||||
format: "",
|
||||
indent: 0,
|
||||
type: "root",
|
||||
version: 1,
|
||||
},
|
||||
};
|
||||
|
||||
if (aggregatedFiles.length > 1) {
|
||||
aggregatedFiles.slice(1).forEach((file) =>
|
||||
result.root.children.push(
|
||||
createImageNode({
|
||||
src: file.uploadedJpegImage,
|
||||
caption: createImageCaption(file.shootingConditions),
|
||||
}),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
result.root.children.push(createHeadingNode("Downloads", "h2"));
|
||||
|
||||
aggregatedFiles.forEach((file) => {
|
||||
result.root.children.push(
|
||||
createFileNode({
|
||||
src: file.uploadedJpegFile,
|
||||
name: getBasenameWithExtension(file.jpegPath),
|
||||
size: file.jpegSize,
|
||||
}),
|
||||
);
|
||||
|
||||
if (file.uploadedRawFile && file.rawPath && file.rawSize) {
|
||||
result.root.children.push(
|
||||
createFileNode({
|
||||
src: file.uploadedRawFile,
|
||||
name: getBasenameWithExtension(file.rawPath),
|
||||
size: file.rawSize,
|
||||
}),
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
const post = {
|
||||
title: options.title,
|
||||
slug: options.slug,
|
||||
lexical: JSON.stringify(result),
|
||||
feature_image: aggregatedFiles[0].uploadedJpegImage,
|
||||
feature_image_caption: createImageCaption(
|
||||
aggregatedFiles[0].shootingConditions,
|
||||
),
|
||||
status: "published",
|
||||
visibility: "public",
|
||||
tags: options.keywords,
|
||||
published_at: aggregatedFiles[0].shootingConditions.timestamp,
|
||||
};
|
||||
|
||||
const url = await uploadPost(post);
|
||||
console.log(url);
|
||||
|
||||
process.exit(0);
|
||||
})
|
||||
.parse();
|
@@ -0,0 +1,50 @@
|
||||
export const createTextNode = (text: string) => ({
|
||||
detail: 0,
|
||||
format: 0,
|
||||
mode: "normal",
|
||||
style: "",
|
||||
text,
|
||||
type: "extended-text",
|
||||
version: 1,
|
||||
});
|
||||
|
||||
export const createHeadingNode = (text: string, level: string) => ({
|
||||
children: [createTextNode(text)],
|
||||
direction: "ltr",
|
||||
format: "",
|
||||
indent: 0,
|
||||
type: "extended-heading",
|
||||
version: 1,
|
||||
tag: level,
|
||||
});
|
||||
|
||||
export interface ImageInput {
|
||||
src: string;
|
||||
caption: string;
|
||||
}
|
||||
|
||||
export const createImageNode = (image: ImageInput) => {
|
||||
return {
|
||||
type: "image",
|
||||
version: 1,
|
||||
cardWidth: "regular",
|
||||
...image,
|
||||
};
|
||||
};
|
||||
|
||||
export interface FileInput {
|
||||
src: string;
|
||||
name: string;
|
||||
size: number;
|
||||
}
|
||||
|
||||
export const createFileNode = (file: FileInput) => {
|
||||
return {
|
||||
type: "file",
|
||||
src: file.src,
|
||||
fileTitle: file.name,
|
||||
fileName: file.name,
|
||||
fileCaption: "",
|
||||
fileSize: file.size,
|
||||
};
|
||||
};
|
Reference in New Issue
Block a user