Skip to content

Commit 5181cef

Browse files
authored
fix(blob): encode pathname (#330)
1 parent 223f2f6 commit 5181cef

File tree

1 file changed

+19
-18
lines changed
  • src/runtime/blob/server/utils

1 file changed

+19
-18
lines changed

src/runtime/blob/server/utils/blob.ts

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,8 @@ export function hubBlob(): HubBlob {
191191
}
192192
},
193193
async serve(event: H3Event, pathname: string) {
194-
const object = await bucket.get(decodeURI(pathname))
194+
pathname = decodeURIComponent(pathname)
195+
const object = await bucket.get(pathname)
195196

196197
if (!object) {
197198
throw createError({ message: 'File not found', statusCode: 404 })
@@ -204,7 +205,7 @@ export function hubBlob(): HubBlob {
204205
return object.body
205206
},
206207
async get(pathname: string): Promise<Blob | null> {
207-
const object = await bucket.get(decodeURI(pathname))
208+
const object = await bucket.get(decodeURIComponent(pathname))
208209

209210
if (!object) {
210211
return null
@@ -213,7 +214,7 @@ export function hubBlob(): HubBlob {
213214
return object.blob() as Promise<Blob>
214215
},
215216
async put(pathname: string, body: string | ReadableStream<any> | ArrayBuffer | ArrayBufferView | Blob, options: BlobPutOptions = {}) {
216-
pathname = decodeURI(pathname)
217+
pathname = decodeURIComponent(pathname)
217218
const { contentType: optionsContentType, contentLength, addRandomSuffix, prefix, customMetadata } = options
218219
const contentType = optionsContentType || (body as Blob).type || getContentType(pathname)
219220

@@ -238,7 +239,7 @@ export function hubBlob(): HubBlob {
238239
return mapR2ObjectToBlob(object)
239240
},
240241
async head(pathname: string) {
241-
const object = await bucket.head(decodeURI(pathname))
242+
const object = await bucket.head(decodeURIComponent(pathname))
242243

243244
if (!object) {
244245
throw createError({ message: 'Blob not found', statusCode: 404 })
@@ -248,13 +249,13 @@ export function hubBlob(): HubBlob {
248249
},
249250
async del(pathnames: string | string[]) {
250251
if (Array.isArray(pathnames)) {
251-
return await bucket.delete(pathnames.map(p => decodeURI(p)))
252+
return await bucket.delete(pathnames.map(p => decodeURIComponent(p)))
252253
} else {
253-
return await bucket.delete(decodeURI(pathnames))
254+
return await bucket.delete(decodeURIComponent(pathnames))
254255
}
255256
},
256257
async createMultipartUpload(pathname: string, options: BlobMultipartOptions = {}): Promise<BlobMultipartUpload> {
257-
pathname = decodeURI(pathname)
258+
pathname = decodeURIComponent(pathname)
258259
const { contentType: optionsContentType, contentLength, addRandomSuffix, prefix, customMetadata } = options
259260
const contentType = optionsContentType || getContentType(pathname)
260261

@@ -278,7 +279,7 @@ export function hubBlob(): HubBlob {
278279
return mapR2MpuToBlobMpu(mpu)
279280
},
280281
resumeMultipartUpload(pathname: string, uploadId: string) {
281-
const mpu = bucket.resumeMultipartUpload(pathname, uploadId)
282+
const mpu = bucket.resumeMultipartUpload(decodeURIComponent(pathname), uploadId)
282283

283284
return mapR2MpuToBlobMpu(mpu)
284285
},
@@ -375,7 +376,7 @@ export function proxyHubBlob(projectUrl: string, secretKey?: string): HubBlob {
375376
})
376377
},
377378
async serve(_event: H3Event, pathname: string) {
378-
return blobAPI<ReadableStream<any>>(decodeURI(pathname), {
379+
return blobAPI<ReadableStream<any>>(encodeURIComponent(pathname), {
379380
method: 'GET'
380381
})
381382
},
@@ -391,20 +392,20 @@ export function proxyHubBlob(projectUrl: string, secretKey?: string): HubBlob {
391392
if (body instanceof Uint8Array) {
392393
body = new Blob([body])
393394
}
394-
return await blobAPI<BlobObject>(decodeURI(pathname), {
395+
return await blobAPI<BlobObject>(encodeURIComponent(pathname), {
395396
method: 'PUT',
396397
headers,
397398
body,
398399
query
399400
})
400401
},
401402
async head(pathname: string): Promise<BlobObject> {
402-
return await blobAPI(`/head/${decodeURI(pathname)}`, {
403+
return await blobAPI(`/head/${encodeURIComponent(pathname)}`, {
403404
method: 'GET'
404405
})
405406
},
406407
async get(pathname: string): Promise<Blob> {
407-
return await blobAPI(`/${decodeURI(pathname)}`, {
408+
return await blobAPI(`/${encodeURIComponent(pathname)}`, {
408409
method: 'GET',
409410
responseType: 'blob'
410411
})
@@ -414,18 +415,18 @@ export function proxyHubBlob(projectUrl: string, secretKey?: string): HubBlob {
414415
await blobAPI('/delete', {
415416
method: 'POST',
416417
body: {
417-
pathnames: pathnames.map(p => decodeURI(p))
418+
pathnames: pathnames.map(p => encodeURIComponent(p))
418419
}
419420
})
420421
} else {
421-
await blobAPI(decodeURI(pathnames), {
422+
await blobAPI(encodeURIComponent(pathnames), {
422423
method: 'DELETE'
423424
})
424425
}
425426
return
426427
},
427428
async createMultipartUpload(pathname: string, options: BlobMultipartOptions = {}) {
428-
return await blobAPI<BlobMultipartUpload>(`/multipart/create/${decodeURI(pathname)}`, {
429+
return await blobAPI<BlobMultipartUpload>(`/multipart/create/${encodeURIComponent(pathname)}`, {
429430
method: 'POST',
430431
query: options
431432
})
@@ -435,7 +436,7 @@ export function proxyHubBlob(projectUrl: string, secretKey?: string): HubBlob {
435436
pathname,
436437
uploadId,
437438
async uploadPart(partNumber: number, body: string | ReadableStream<any> | ArrayBuffer | ArrayBufferView | Blob): Promise<BlobUploadedPart> {
438-
return await blobAPI<BlobUploadedPart>(`/multipart/upload/${decodeURI(pathname)}`, {
439+
return await blobAPI<BlobUploadedPart>(`/multipart/upload/${encodeURIComponent(pathname)}`, {
439440
method: 'PUT',
440441
query: {
441442
uploadId,
@@ -445,15 +446,15 @@ export function proxyHubBlob(projectUrl: string, secretKey?: string): HubBlob {
445446
})
446447
},
447448
async abort(): Promise<void> {
448-
await blobAPI(`/multipart/abort/${decodeURI(pathname)}`, {
449+
await blobAPI(`/multipart/abort/${encodeURIComponent(pathname)}`, {
449450
method: 'DELETE',
450451
query: {
451452
uploadId
452453
}
453454
})
454455
},
455456
async complete(parts: BlobUploadedPart[]): Promise<BlobObject> {
456-
return await blobAPI<BlobObject>(`/multipart/complete/${decodeURI(pathname)}`, {
457+
return await blobAPI<BlobObject>(`/multipart/complete/${encodeURIComponent(pathname)}`, {
457458
method: 'POST',
458459
query: {
459460
uploadId

0 commit comments

Comments
 (0)