How to use Velocity to pull an image into a confluence user macro?

Steve Goldberg
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
October 7, 2012

Hi,

I'm writing a macro and I want to pull in an image attached to a page that follows a regular pattern. I.e., I'm trying to pull in an image called 'clientlogo.gif' that is always attached to a page following the pattern of https://[baseurl]/display/[client name]/[client name]/clientlogo.gif.

I figure I have to use $attachmentManager but I'm not sure how to use it. Can anybody help?

Thanks

1 answer

0 votes
Matthew J. Horn
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
October 8, 2012

Looks like AttachmentManager is the class to use. Have you peeked at the API? http://docs.atlassian.com/atlassian-confluence/latest/com/atlassian/confluence/pages/AttachmentManager.html

The first thing I would try is the getAttachmentDownloadPath() method, which should be what the IMG tag wants...

Steve Goldberg
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
October 8, 2012

Thanks, Matt, but I'm terrible at reading their API docs so I don't know how I would use it. Am I write in thinking it would be something like $attachmentManager.getAttachmentDownloadPath(clientlogo.gif,$paramclientname/$paramclientname) ?

Matthew J. Horn
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
October 8, 2012

Now that I think about it, since you know the location of the page, and the name of the file, you don't need to use the attachment manager. You just need the client name. Let's assume the client name is set as $clientName. Then you just do:

<img src="https://$baseUrl/display/$clientName/$clientName/clientlogo.gif"/>

Does that work?

Steve Goldberg
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
October 9, 2012

No, it doesn't. The problem is that the way attachments are handled the URLs are structured like this: https://$baseUrl/download/attachments/$pageId/attachment.ext. So that's fine if you're going to use the same resource each time but when you need to reference another page, you're screwed.

Suggest an answer

Log in or Sign up to answer