How can I retrieve all attachments for a space?
I want to do something similar to the following though for an individual space:
Iterator<Attachment> i = attachmentManager.getAttachmentDao(). findLatestVersionsIterator(); while (i.hasNext()) { Attachment a = i.next(); // Process attachment. } }
Works a treat, though instead of going through every attachment in the system I only want to go over certain spaces but get the current version only.
Community moderators have prevented the ability to post new answers.
There's no methods on the AttachmentManager or the AttachmentDAO to get all the attachments in a particular space.
You would need to use the SpaceManager to get all the pages in the space, and then iterate through them one-by-one and use the AttachmentManager to get all the attachments on each page.
Alternatively, you could use the Lucene index to retrieve this information. This will perform much better, but the downside is it may be up to a minute or so out-of-date. Have a look at how the {space-attachments} macro built-in to Confluence does it - https://studio.plugins.atlassian.com/source/changelog/CONFATT?cs=167076
While this would work for me it turned out to be slow. Instead I identified all space keys I wanted to process, then use the iterator pattern in my original post and check the attachments space key:
Set<String> keys = getSpaceKeys(); Iterator<Attachment> i = attachmentManager.getAttachmentDao(). findLatestVersionsIterator(); while (i.hasNext()) { Attachment a = i.next(); if (a.getSpace() != null && keys.contains(a.getSpace().getKey())) { // Process attachment } }
This turns out quite effective.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.