@@ -13,6 +13,8 @@ import { HTTPMethod } from './types.ts'
13
13
import { isNodeError , Logger } from './util.ts'
14
14
15
15
const API_URL_PATH = / \/ a p i \/ v 1 \/ b l o b s \/ (?< site_id > [ ^ / ] + ) \/ (?< store_name > [ ^ / ] + ) \/ ? (?< key > [ ^ ? ] * ) /
16
+ const LEGACY_API_URL_PATH = / \/ a p i \/ v 1 \/ s i t e s \/ (?< site_id > [ ^ / ] + ) \/ b l o b s \/ ? (?< key > [ ^ ? ] * ) /
17
+ const LEGACY_DEFAULT_STORE = 'production'
16
18
17
19
export enum Operation {
18
20
DELETE = 'delete' ,
@@ -99,11 +101,11 @@ export class BlobsServer {
99
101
async delete ( req : http . IncomingMessage , res : http . ServerResponse ) {
100
102
const apiMatch = this . parseAPIRequest ( req )
101
103
102
- if ( apiMatch ) {
104
+ if ( apiMatch ?. useSignedURL ) {
103
105
return this . sendResponse ( req , res , 200 , JSON . stringify ( { url : apiMatch . url . toString ( ) } ) )
104
106
}
105
107
106
- const url = new URL ( req . url ?? '' , this . address )
108
+ const url = new URL ( apiMatch ?. url ?? req . url ?? '' , this . address )
107
109
const { dataPath, key, metadataPath } = this . getLocalPaths ( url )
108
110
109
111
if ( ! dataPath || ! key ) {
@@ -390,22 +392,42 @@ export class BlobsServer {
390
392
391
393
const apiURLMatch = req . url . match ( API_URL_PATH )
392
394
393
- if ( ! apiURLMatch ) {
394
- return null
395
+ if ( apiURLMatch ) {
396
+ const key = apiURLMatch . groups ?. key
397
+ const siteID = apiURLMatch . groups ?. site_id as string
398
+ const storeName = apiURLMatch . groups ?. store_name as string
399
+ const urlPath = [ siteID , storeName , key ] . filter ( Boolean ) as string [ ]
400
+ const url = new URL ( `/${ urlPath . join ( '/' ) } ?signature=${ this . tokenHash } ` , this . address )
401
+
402
+ return {
403
+ key,
404
+ siteID,
405
+ storeName,
406
+ url,
407
+ useSignedURL : req . headers . accept === 'application/json;type=signed-url' ,
408
+ }
395
409
}
396
410
397
- const key = apiURLMatch . groups ?. key
398
- const siteID = apiURLMatch . groups ?. site_id as string
399
- const storeName = apiURLMatch . groups ?. store_name as string
400
- const urlPath = [ siteID , storeName , key ] . filter ( Boolean ) as string [ ]
401
- const url = new URL ( `/${ urlPath . join ( '/' ) } ?signature=${ this . tokenHash } ` , this . address )
411
+ const legacyAPIURLMatch = req . url . match ( LEGACY_API_URL_PATH )
402
412
403
- return {
404
- key,
405
- siteID,
406
- storeName,
407
- url,
413
+ if ( legacyAPIURLMatch ) {
414
+ const fullURL = new URL ( req . url , this . address )
415
+ const storeName = fullURL . searchParams . get ( 'context' ) ?? LEGACY_DEFAULT_STORE
416
+ const key = legacyAPIURLMatch . groups ?. key
417
+ const siteID = legacyAPIURLMatch . groups ?. site_id as string
418
+ const urlPath = [ siteID , storeName , key ] . filter ( Boolean ) as string [ ]
419
+ const url = new URL ( `/${ urlPath . join ( '/' ) } ?signature=${ this . tokenHash } ` , this . address )
420
+
421
+ return {
422
+ key,
423
+ siteID,
424
+ storeName,
425
+ url,
426
+ useSignedURL : true ,
427
+ }
408
428
}
429
+
430
+ return null
409
431
}
410
432
411
433
sendResponse ( req : http . IncomingMessage , res : http . ServerResponse , status : number , body ?: string ) {
0 commit comments