getAttachmentList() contains

I want to check if a certain attachment exists on a page.

I would like to use the “contains” method of a list, but I can’t get it to work with attachments. Whatever I put into that method, the result is always false.

So I have created a foreach loop instead, which gets slower and slower when there are more and more attachments on a page.

Does anyone have a working example of $attachments.contains() ???

#set($attachmentFound = false)
#set($attachments = $mydoc.getAttachmentList())
#foreach($attachment in $attachments)
 #if($attachment.filename == "file_name_with_extension")
  #set($attachmentFound = true)

The best is usually to test if $mydoc.getAttachment(‘file_name_with_extension’) return something.

#if ($mydoc.getAttachment('file_name_with_extension'))
  The attachment exist

Still the fact that contains does not work looks like a bug to me, would be great if you could report it to Loading....

Thanks, the test with the #if ($mydoc.getAttachment(“file_name_with_extension”)) works like a charm!

Regarding the contains methode: I have some code that works…but now I have another question :slight_smile:

#set($attachments = $doc.getAttachmentList())

This is display:


The problem now, is that the first line is different everytime I hit refresh (F5).

I do know the filename of the attachment (which does not change at all), but how do I translate that to the “encrypted” name?

Nothing wrong here, that’s because you get a new Java object every time but it’s the same thing behind the scene. com.xpn.xwiki.api.Attachment is the class which is exposed to script and which contains protections and helpers and the “code” is the reference of the object (which is always different because it’s a different Java object every time).

If you want get the name of the attachment use $attachments.get(0).filename.

And that’s were the real problem lies :slight_smile: I know the name of the attachment.

The “contains” method requires that newly generated name.

So, in order the use the List.contains method, I need to know how I can get from my filename to the name of that newly generated Java object…

It’s not really clear to me why you absolutely want to use List#contains, what’s wrong with $doc.getAttachment('I_know_the_name') ?

$doc.getAttachment works, that’s why have accepted that as the solution.

But I am just zooming in on the problem with the contains method with the $doc.getAttachmentList()

Not being able to figure out what name to use (because a new Java object is generated everytime you run the code) renders the contains method useless. That’s all :slight_smile:

Maybe XWiki could offer a $doc.getAttachmentFileNameList()???

It’s not a name, contains takes an object or type Attachment. You are just mixing the result of object.toString() (what happen when you ask Velocity to print the object) and the object itself.

Sure why not but never really been a need so far, people get the attachments and call getName() on each one. Also it does not have much to do with contains.