Hey @BPB ,
There has to be a connection between the attachment ID you get in the Forge event and the media ID somewhere in the darkness of the underlying Atlassian infrastructure.
This relation is used by the endpoint: /rest/api/3/attachment/content/{id} – it returns the actual (content of the) attachment.
As luck would have it I know that this endpoint answers with a status code 3xx which forwards to the media server.
To illustrate what I mean I have dug out a publicly available attachment from an Atlassian Jira:
https://ecosystem.atlassian.net/rest/api/3/attachment/content/217145
Let’s make a HEAD request to this URL (HEAD returns the headers that a GET would return).
> HEAD /rest/api/3/attachment/content/217145 HTTP/2
> Host: ecosystem.atlassian.net
> cookie: atlassian.xsrf.token=8_redacted
> accept: */*
* TLSv1.2 (IN), TLS header, Supplemental data (23):
< HTTP/2 303
< date: Wed, 01 Nov 2023 17:34:15 GMT
< content-type: application/json;charset=UTF-8
< server: AtlassianEdge
< timing-allow-origin: *
< x-arequestid: c8b4502dfa6c00146bb553adf468284e
< cache-control: no-cache, no-store, no-transform
< location: https://api.media.atlassian.com/file/ff4a5337-ab17-4572-ac66-04e3d869d92a/binary?token=ey_redacted&client=0a67ae6f-ef7d-4e7e-9d37-ff56929fa074&dl=true&name=image-20231027-152009.png
< server-timing: filter-workcontext;dur=68, filter-frontend-router;dur=54, mcache-client;dur=6, filter-request-papi;dur=68, sql;dur=5
< vary: Accept-Encoding
< x-content-type-options: nosniff
< x-xss-protection: 1; mode=block
[redacted]
The interesting part is the Location header (so the URL Atlassian redirects us too). It is the actual download URL. It contains an ID that looks like the media ID you are looking for. I verified this with the example attachment from above.
So, this is the path I would pursue.
Next challenge: how to do that in your code? The problem you’ll probably face is that the Forge backend doesn’t give you a real implementation of the Node.js fetch method. But I fear that could be needed to properly implement a “convertAttachmentIdToMediaId” function in Javascript.
A Javascript implementation should probably look like this:
const mediaId = (await fetch("https://ecosystem.atlassian.net/rest/api/3/attachment/content/217145")).url
I’m not sure about the Forge runtime (this could be a good chance to use the new Node runtime which should give you a real fetch) but at least in the browser this works:
Well, after that you’ll have to parse the URL and extract the id from the path. You can use the URL constructor for that. If you came to this point, this shouldn’t be a problem
Hope this helps, happy hacking!
Cheers
Julian