Skip to content

Commit 0a7b07b

Browse files
authored
fix: strip trailing slash from car file name (#290)
Otherwise filenames end up being Qmfoo_.car instead of Qmfoo.car
1 parent 24cb15f commit 0a7b07b

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

packages/verified-fetch/src/plugins/plugin-handle-car.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ function getFilename ({ cid, ipfsPath, query }: Pick<PluginContext, 'query' | 'c
1717
const filename = ipfsPath
1818
.replace(/\/ipfs\//, '')
1919
.replace(/\/ipns\//, '')
20+
.replace(/\/+$/g, '')
2021
.replace(/\//g, '_')
2122

2223
return `${filename}.car`

packages/verified-fetch/test/content-disposition.spec.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,46 @@ describe('content-disposition', () => {
5656
expect(resp.status).to.equal(200)
5757
expect(resp.headers.get('Content-Disposition')).to.include('attachment')
5858
})
59+
60+
it('should default to the CID for the car file name', async () => {
61+
const obj = {
62+
hello: 'world'
63+
}
64+
const c = dagCbor(helia)
65+
const cid = await c.add(obj)
66+
67+
const resp = await fetch(`ipfs://${cid}/?format=car`)
68+
expect(resp).to.be.ok()
69+
expect(resp.status).to.equal(200)
70+
expect(resp.headers.get('Content-Disposition')).to.include('attachment')
71+
expect(resp.headers.get('Content-Disposition')).to.include(`filename="${cid}.car"`)
72+
})
73+
74+
it('should respect a filename for the car file name', async () => {
75+
const obj = {
76+
hello: 'world'
77+
}
78+
const c = dagCbor(helia)
79+
const cid = await c.add(obj)
80+
81+
const resp = await fetch(`ipfs://${cid}/?filename=my-car.car&format=car`)
82+
expect(resp).to.be.ok()
83+
expect(resp.status).to.equal(200)
84+
expect(resp.headers.get('Content-Disposition')).to.include('attachment')
85+
expect(resp.headers.get('Content-Disposition')).to.include('filename="my-car.car"')
86+
})
87+
88+
it('should respect a filename for the car file name with many trailing slashes', async () => {
89+
const obj = {
90+
hello: 'world'
91+
}
92+
const c = dagCbor(helia)
93+
const cid = await c.add(obj)
94+
95+
const resp = await fetch(`ipfs://${cid}///////?filename=my-car.car&format=car`)
96+
expect(resp).to.be.ok()
97+
expect(resp.status).to.equal(200)
98+
expect(resp.headers.get('Content-Disposition')).to.include('attachment')
99+
expect(resp.headers.get('Content-Disposition')).to.include('filename="my-car.car"')
100+
})
59101
})

0 commit comments

Comments
 (0)