Alex
Alex

Reputation: 1668

Confluence plugin import external attachments

I'm creating a plugin for confluence to import pages (title, body) and file assets (pdf and images).

I'm using PageManager to save the page and saving/creating a page is pretty straight forward but adding a attachment I'm finding dificult to understand how to do it as there is almost no information online that I can find how to do it.

Does anyone know how to set an attachment to a Page object form an inputStream or a byte[]? The page.setAttachments() takes a list of Attachment object but if I try to create a attachment object it doesnt take either an inputStream or a byte array.

PageManager pageManager = ComponentLocator.getComponent(PageManager) as PageManager
Page page = pageManager.getPage(8290525l)
URL url = new URL("https://i.ytimg.com/vi/tntOCGkgt98/maxresdefault.jpg");
InputStream bufferIn = new BufferedInputStream(url.openStream());

Any help would be appreciated

Thanks


Edit:

PageManager pageManager = ComponentLocator.getComponent(PageManager) as PageManager
ArrayList<AttachmentResource> attachResources = new ArrayList<AttachmentResource>()

def page = pageManager.getPage(8290525l)
URL url = new URL("https://i.ytimg.com/vi/tntOCGkgt98/maxresdefault.jpg");
InputStream bufferIn = new BufferedInputStream(url.openStream());

DefaultSaveContext context = new DefaultSaveContext()
context.setUpdateLastModifier(true)


AttachmentManager attachmentManager = ComponentLocator.getComponent(AttachmentManager) as AttachmentManager
Attachment attachment = new Attachment("2454.png", "image/png", bufferIn.getBytes().length, "", false)
attachmentManager.saveAttachment(attachment, null, bufferIn)

page.addAttachment(attachment)
pageManager.saveContentEntity(page, context)

Upvotes: 1

Views: 640

Answers (1)

rmlan
rmlan

Reputation: 4657

Use the FileUploadManager to upload the data and add the attachment (it looks like you are actually using Groovy, so I will too):

PageManager pageManager = ComponentLocator.getComponent(PageManager) as PageManager
Page page = pageManager.getPage(8290525l)

URL url = new URL("https://i.ytimg.com/vi/tntOCGkgt98/maxresdefault.jpg");
InputStream bufferIn = new BufferedInputStream(url.openStream());

FileUploadManager uploadManager = ComponentLocator.getComponent(FileUploadManager) as FileUploadManager
AttachmentResource attachment = new InputStreamAttachmentResource (bufferIn, "2454.png", "image/png", bufferIn.getBytes().length)
uploadManager.storeResource(attachment, page)

Upvotes: 1

Related Questions