Best way to upload Images to a plugin/theme?

I would like to upload image icons as a part of my theme or plugin (I use both). I will then be referring to them from a glimmer component in the plugin. I need to be able to get the string URL of the image location, because I’m sending it to another server in an ajax request. (The remote server embeds icons into QR codes, if you want to know.)

If I put my images in about.json in the theme, they don’t get placed in a deterministic location - instead there’s a hash in the URL. So I don’t know how I would refer to them outside of CSS, much less in the plugin.

If I put my images in an admin setting for the plugin as an uploaded_image_list, the locations are also indeterministic, and what’s more they end up in an array with no human readable name/tag/alias to refer to them by.

// console.log(this.siteSettings.county_fence_available_icons):
/uploads/default/original/1X/1764ca1c70c7f4f7f01da26e702639fd8cfe2bf4.jpeg|/uploads/default/original/1X/b16cecd70e4d25abdf06a8b6135ec126d2c6ce2d.jpeg|/uploads/default/original/1X/b4c049256c1068e531e43f1622ada78c5b2070b3.jpeg|/uploads/default/original/1X/4aa457c9cd939e178eb251682a4438cc027b96e9.jpeg

So is there a way I can upload images, either to the theme or plugin, in a way that they end up at a deterministic URL?

This question also pertains to any other assets that a person would want to upload. Both the assets section of about.json as well as the register_asset function to be used in plugin.rb look like they should be able to handle other file types than fonts, but if those files are getting uploaded I have no idea where they are.

No. You refer to them by the variable name as it looks like you did? All uploads are saved as a hash of their contents.

1 Like

Even though you can’t set the URL, it is deterministic, since the URL is made up of the SHA1 hash of the file.

As long as you don’t change the file contents, it should end up at either

/uploads/<instance name>/original/1X/1764ca1c70c7f4f7f01da26e702639fd8cfe2bf4.jpeg

or

/uploads/<instance name>/original/2X/1/1764ca1c70c7f4f7f01da26e702639fd8cfe2bf4.jpeg

or

/uploads/<instance name>/original/3X/1/7/1764ca1c70c7f4f7f01da26e702639fd8cfe2bf4.jpeg

or

/uploads/<instance name>/original/4X/1/7/6/1764ca1c70c7f4f7f01da26e702639fd8cfe2bf4.jpeg

(and which one depends on the number of uploads you already have).

1 Like

As for plugins, hard assets (ones not modifiable by admin config) will have a clear URL.

You can reference the Discourse Math plugin initializers:

Assets in /public directory will be available under the plugin name. These assets are called directly without needing to be registered in plugin.rb.

2 Likes