Skip to content
Merged
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
27 changes: 27 additions & 0 deletions src/io/__tests__/amazonS3.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,33 @@ describe('amazonS3', () => {
expect(init).toBeUndefined();
});

it('percent-encodes reserved chars in keys but preserves slashes', async () => {
fetchSpy.mockResolvedValueOnce(
okResponse(
xmlListResponse([
'scan?1.dcm',
'seg#1.dcm',
'my scan.dcm',
'a+b.dcm',
'sub/dir/file.dcm',
])
)
);

const urls: string[] = [];
await getObjectsFromS3('s3://bucket/prefix', (_name, url) =>
urls.push(url)
);

expect(urls).toEqual([
'https://bucket.s3.amazonaws.com/scan%3F1.dcm',
'https://bucket.s3.amazonaws.com/seg%231.dcm',
'https://bucket.s3.amazonaws.com/my%20scan.dcm',
'https://bucket.s3.amazonaws.com/a%2Bb.dcm',
'https://bucket.s3.amazonaws.com/sub/dir/file.dcm',
]);
});

it('follows pagination via continuation token', async () => {
fetchSpy
.mockResolvedValueOnce(
Expand Down
7 changes: 6 additions & 1 deletion src/io/amazonS3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,13 @@ export const isAmazonS3Uri = (uri: string) =>

export type ObjectAvailableCallback = (name: string, url: string) => void;

// Percent-encode each path segment so keys containing reserved URI chars
// (`?`, `#`, space, `+`, …) round-trip correctly. Slashes are preserved.
const encodeS3Key = (key: string) =>
key.split('/').map(encodeURIComponent).join('/');

const getObjectPublicUrl = (bucket: string, key: string) =>
`https://${bucket}.s3.amazonaws.com/${key}`;
`https://${bucket}.s3.amazonaws.com/${encodeS3Key(key)}`;

const buildListUrl = (
bucket: string,
Expand Down
Loading