Getting attachment version from AttachmentService instead of AttachmentManager, how?


I’m trying to replace the deprecated AttachmentManager.getAttachment(…)-calls with the recommended AttachmentService.find(…)-calls, but having some trouble understanding how to do it and haven’t really found any help from any Atlassian documentation.

So for example, I have this code now:

// Attachment version 3 in deprecated way with AttachmentManager
Attachment attv3 = am.getAttachment(pageCEO, "file.txt", 3);

// I can get InputStream from content, which is what I need
InputStream isv3 = am.getAttachmentData(attv3);

// All good now, have Attachment and can read content from it.
// But we did it in deprecated way..... :(

I’m trying to achieve the same now with AttachmentService like this:

        Content conv3 = as.find()
                .fetchMany(new SimplePageRequest(0, Integer.MAX_VALUE))
                .filter(f -> f.getVersion().getNumber() == 3)

That looks very ugly but I think it should achieve the same goal, except now I’m getting instance of Content instead of Attachment which is a problem for me, I would need to have the corresponding Attachment-instance.

So, how do I now read the contents from the Content-instance? Is there a way to get an InputStream from it?

I also noticed in the documentation that ContentSelector should be used if specific version is to be retrieved, which sounds like better way to get the desired version instead of the fetching all and filtering the results, but I can’t figure out how exactly ContentSelector is supposed to be used, i.e. I can’t see any place where it can be used as parameter for the fetching.

Any tutorial or example code of above usage of getting specific attachment version contents would be very much appreciated.