Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Custom macro - Display generated link to attachment of another page only if target exists

mark
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
October 26, 2019

Hi everybody

I am stuck at a custom macro I hope someone can help me with.

It takes these parameters:
$parampage = another Confluence page
$paramextension = filetype (e.g. pdf, doc...)
$paramcaption = link text that is shown as a result

The macro does the following:

  1. It takes $parampage and determines $pageid of that page.
  2. Then it creates a link to the path where the attachments of $parampage are stored and in there specifically to this file:
    TITLEOFCURRENTPAGE.$paramextension

The link is created just fine and it also points to the correct file in the correct space and I can for example open the pdf.

The problem is, that I only want to display the link, if the file (e.g. titleofpage.pdf) exists. I tried all kinds of stuff I can't even remember anymore but nothing worked so far.

Here's the code with the commented line "NOT WORKING YET !!!!", where the check that I don't get to work could take place.

## @param page:title=Page|type=confluence-content|required=true|desc=Seite auswählen, die den Anhang beinhaltet
## @param extension:title=Dateierweiterung|type=string|required=true|desc=Dateierweiterung des Anhangs (z.B. pdf oder xlsx)
## @param caption:title=Link Text|type=string|required=true|desc=Text, der für den Link auf der Seite angezeigt werden soll

#############################################################################
## create strings for "space key" and "page name" from parameter page
## source: https://community.atlassian.com/t5/Answers-Developer-Questions/Autocomplete-and-link-to-page-in-user-macro/qaq-p/522841
#############################################################################
#set ( $colonIndex = $parampage.indexOf(":") )
#if ( $colonIndex == -1 )
#set ( $spaceKey = $space.key )
#set ( $pageName = $parampage )
#else
#set ( $spaceKey = $parampage.substring(0, $colonIndex) )
#set ( $pageNameIndex = $colonIndex + 1 )
#set ( $pageName = $parampage.substring($pageNameIndex) )
#end
#set ( $page = $pageManager.getPage($spaceKey, $pageName) )

#############################################################################
## determine page id of that page because attachments are placed in a subfolder named after the id of the page
#############################################################################
#set($pageid=$pageManager.getPage($spaceKey, $pageName).getIdAsString())
#set($linktoatt="https://SOMEWIKIPAGE/download/attachments/$pageid/$content.title.$paramextension")
##$linktoatt #uncomment to see string

#############################################################################
## check if page and attachment exists and create a link to the attachment if both positive
#############################################################################
#if($page) ## only proceed if page exists, works fine
#if(!$linktoatt.getBody()) ## only proceed if attachment exists !!! NOT WORKING YET !!!!
<a href=$linktoatt> $paramcaption </a>
#else
Es existiert auf der Seite <i>$page</i> kein Anhang mit Dateinamen <i>$content.title.$paramextension</i>
#end
#else
Seite $parampage existiert nicht
#end

Do you have any clue?

Thanks a lot,
Mark

0 answers

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events