Image upload via admin api
Open
Nobody has claimed this yet.
- Dominant language
- JavaScript
- Stars
- 134
- Forks
- 87
- Avg merge
- 12m
- Merged PRs (30d)
- 25
Description
Issue Summary
I'm writing a plugin uploading images from obsidian to ghost. When running the code bellow it triggers CORS due to the use of axios. See also https://forum.ghost.org/t/cors-using-admin-api-from-electron-app-obsidian/37743/6
To Reproduce
- Code bellow and run the plugin. (Ignore if there's a parenthesis missing or something, I went back to try and reconstruct it).
async function uploadImages(html: string) {
// Find images that Ghost Upload supports
let imageRegex = /!*\[\[(.*?)\]\]/g;
let imagePromises = [];
// Get full-path to images
let imageDirectory: string;
let adapter = app.vault.adapter;
if (adapter instanceof FileSystemAdapter) {
imageDirectory = adapter.getBasePath(); // Vault directory
if (settings.screenshotsFolder) {
imageDirectory = `${imageDirectory}${settings.screenshotsFolder}`;
}
if (frontmatter.imageDirectory) { // Extends the image directory
imageDirectory = `${imageDirectory}${frontmatter.imageDirectory}`;
}
}
console.log("Image Directory", imageDirectory);
let result: RegExpExecArray | null; // Declare the 'result' variable
while((result = imageRegex.exec(html)) !== null) {
let file = `${imageDirectory}/${result[1]}`;
// Upload the image, using the original matched filename as a reference
imagePromises.push(api.images.upload({
ref: file,
file: file
}));
}
return Promise
.all(imagePromises)
.then(images => {
images.forEach(image => html = html.replace(image.ref, image.url));
return html;
});
}
- Execute the plugin in obsidian and get the following:
Access to XMLHttpRequest at 'http://localhost:2368/ghost/api/v4/admin/images/upload/' from origin 'app://obsidian.md' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
Maybe this is more of a feature request than a bug - nevertheless something annoying.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the Admin API image-upload endpoint and its preflight response, using the Obsidian/Electron origin shown in the report. The issue names no repository file or test; done means the documented upload flow can be exercised from that origin without the reported CORS failure.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- electron, javascript
- Domain
- api
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100