Register images via plugin


(Shiv Kumar) #1

@zogstrip @sam I noticed that you were involved with creating the emoji plugin, which registers a ton of images. I’m trying to register some images via a plugin, but don’t really understand what you had to do in order to get those images to register properly. Can you pass along some tips/best practices for how to register images via plugins?


(Sam Saffron) #2

@shivermetimbers can you update this with what you did to get it work?


(Shiv Kumar) #3

Sure! In order to register images via a plugin, simply create a new public folder in your plugin’s directory, then create an images folder within that folder, and then just drop all your images in that folder. There is some code that runs every time your app is started that looks for a public/images folder and automatically registers any images within that folder.

(note: the reason this wasn’t working for me at first was because there was a minor bug that was preventing images from being registered if the name of your plugin contained spaces. I submitted a PR to fix this, so hopefully no one else has this problem going forward)


(etewiah) #4

I am having trouble getting to an image file in my plugin directory. On my local dev server, I can get to the image via:

http://localhost:3000/plugins/klavado/images/klavado_132.jpg

After deploying to the server though, this does not work:

http://klavado.com/plugins/klavado/images/klavado_132.jpg

I have sshed into the docker container and verified that the image file has been correctly cloned into the plugin directory (its in the ‘public/images’ folder)

The emoji plugin uses the same folder structure and I’m able to access files from it like so:

http://klavado.com/plugins/emoji/images/smile.png

I rebuilt the docker app twice just to see if there might be an issue there.

Any advice will be much appreciated ( and please don’t judge me by my site - its a proof of concept and not quite ready for use at the moment :wink:


(etewiah) #5

Okay, got to the bottom of this.

It turns out the url is case-sensitive. From sshing into the docker container, I could see my plugin images had been moved here:

/var/www/discourse/public/plugins/Klavado/images/

Not entirely sure why ‘klavado’ in that directory name is capitalized but it isn’t for the emoji plugin. Anyway, turns out the url has to be capitalized exactly the same way so this does work:

http://klavado.com/plugins/Klavado/images/klavado_132.jpg


(Alex Okafor) #6

Think I found out why. I had a similar issue but the top of my plugin.rb file I had:

# name:  a completely different name from name of plugin

And that was the name it used to generate the folder under /var/www/discourse/public/plugins/…
Updated it to current name of the plugin and it started working.


#7

I had exactly the same problem; thanks for posting your solution. Who knows how long it would have taken me to figure that out otherwise!

I thought the comments at the top of plugin.rb were purely for the reader’s benefit. I didn’t realize there was some sort of metaprogramming looking at those during plugin registration, or whatever.