Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion lib/confluence-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -2078,7 +2078,16 @@ class ConfluenceClient {
return pathOrUrl;
}

return this.buildUrl(pathOrUrl);
// Prepend context path (e.g. /wiki on Atlassian Cloud) when the API returns
// a path relative to the Confluence context root. Without this, URLs such as
// attachment download links (_links.download → "/download/attachments/...")
// get built as https://<domain>/download/... instead of
// https://<domain>/wiki/download/..., which returns 404 on Cloud.
const prefix = this.webUrlPrefix || '';
const needsPrefix = prefix && !pathOrUrl.startsWith(`${prefix}/`);
const withPrefix = needsPrefix ? `${prefix}${pathOrUrl}` : pathOrUrl;

return this.buildUrl(withPrefix);
}

parseNextStart(nextLink) {
Expand Down
33 changes: 33 additions & 0 deletions tests/confluence-client.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,39 @@ describe('ConfluenceClient', () => {
});
expect(httpClient.toAbsoluteUrl('https://cdn.example.com/file.pdf')).toBe('https://cdn.example.com/file.pdf');
});

test('toAbsoluteUrl prepends /wiki context path on Atlassian Cloud', () => {
const cloudClient = new ConfluenceClient({
domain: 'test.atlassian.net',
token: 'token',
apiPath: '/wiki/rest/api'
});
// _links.download from the API is relative to the Confluence context root,
// so on Cloud (apiPath starts with /wiki/) we must prepend /wiki. Otherwise
// the resulting URL hits https://<domain>/download/... and returns 404.
expect(cloudClient.toAbsoluteUrl('/download/attachments/123/file.png?version=1&modificationDate=1700000000000&cacheVersion=1&api=v2'))
.toBe('https://test.atlassian.net/wiki/download/attachments/123/file.png?version=1&modificationDate=1700000000000&cacheVersion=1&api=v2');
});

test('toAbsoluteUrl does not double-prepend /wiki when path already starts with it', () => {
const cloudClient = new ConfluenceClient({
domain: 'test.atlassian.net',
token: 'token',
apiPath: '/wiki/rest/api'
});
expect(cloudClient.toAbsoluteUrl('/wiki/download/attachments/123/file.png'))
.toBe('https://test.atlassian.net/wiki/download/attachments/123/file.png');
});

test('toAbsoluteUrl leaves Server/DC paths untouched (no webUrlPrefix)', () => {
const serverClient = new ConfluenceClient({
domain: 'confluence.example.com',
token: 'token',
apiPath: '/rest/api'
});
expect(serverClient.toAbsoluteUrl('/download/attachments/123/file.png'))
.toBe('https://confluence.example.com/download/attachments/123/file.png');
});
});

describe('api path handling', () => {
Expand Down