Skip to content

Image upload via admin api #477

@Hailst0rm1

Description

@Hailst0rm1

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions