@Hailst0rm Here’s code I wrote that works for me. I confirmed it returns a successful response and that the file is indeed on the file system after the upload.
Here I don’t send the Content-Type
as I advised above.
import * as fs from 'node:fs/promises';
// Construct the token from the key exactly as Ghost would.
import keyToToken from '@tryghost/admin-api/lib/token.js'
const token = keyToToken(key, '/admin');
const key = 'ADMIN-KEY-FROM-GHOST';
const imagePath = '/home/mark/Downloads/face.png';
const filename = 'face.png';
const settings = {
url: 'http://127.0.0.1:2368',
};
const fileContent = await fs.readFile(imagePath);
const contentType = "image/png"
const fileBlob = new Blob([fileContent], { type: contentType });
const formData = new FormData();
formData.append("file", fileBlob, filename);
formData.append("purpose", "image");
formData.append("ref", imagePath);
let response;
try {
response = await fetch(`${settings.url}/ghost/api/admin/images/upload/`, {
method: "POST",
headers: {
// "Content-Type": "multipart/form-data", // Don't include!
Authorization: `Ghost ${token}`,
},
body: formData
});
}
catch (err) {
console.error("ERROR",err);
};
console.log(await response?.json());
That produces this output for me:
{
images: [
{
url: 'http://localhost:2368/content/images/2023/09/face.png',
ref: '/home/mark/Downloads/face.png'
}
]
}