TryGhost / TryGhost/SDK

Image upload via admin api

Open
#477 1 comment 0 reactions 0 assignees View on GitHub

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
  1. 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;
	                    });
                    }
  1. 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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.