Skip to content

Commit 3682644

Browse files
Modify to actually support GCS format
1 parent ec63394 commit 3682644

File tree

2 files changed

+33
-9
lines changed

2 files changed

+33
-9
lines changed

packages/storage/src/implementation/location.ts

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,23 +75,41 @@ export class Location {
7575
}
7676
}
7777
const gsPath = '(/(.*))?$';
78-
const path = '(/([^?#]*).*)?$';
7978
const gsRegex = new RegExp('^gs://' + bucketDomain + gsPath, 'i');
8079
const gsIndices = { bucket: 1, path: 3 };
8180

8281
function httpModify(loc: Location): void {
8382
loc.path_ = decodeURIComponent(loc.path);
8483
}
8584
const version = 'v[A-Za-z0-9_]+';
86-
const hostRegex = "(?:firebase)?storage.googleapis.com";
87-
const httpRegex = new RegExp(
88-
`^https?://${hostRegex}/${version}/b/${bucketDomain}/o${path}`,
85+
const firebaseStorageHost = 'firebasestorage.googleapis.com';
86+
const firebaseStoragePath = '(/([^?#]*).*)?$';
87+
const firebaseStorageRegExp = new RegExp(
88+
`^https?://${firebaseStorageHost}/${version}/b/${bucketDomain}/o${firebaseStoragePath}`,
8989
'i'
9090
);
91-
const httpIndices = { bucket: 1, path: 3 };
91+
const firebaseStorageIndices = { bucket: 1, path: 3 };
92+
93+
const cloudStorageHost = 'storage.googleapis.com';
94+
const cloudStoragePath = '([^?]*)';
95+
const cloudStorageRegExp = new RegExp(
96+
`^https?://${cloudStorageHost}/${bucketDomain}/${cloudStoragePath}`,
97+
'i'
98+
);
99+
const cloudStorageIndices = { bucket: 1, path: 2 };
100+
92101
const groups = [
93102
{ regex: gsRegex, indices: gsIndices, postModify: gsModify },
94-
{ regex: httpRegex, indices: httpIndices, postModify: httpModify }
103+
{
104+
regex: firebaseStorageRegExp,
105+
indices: firebaseStorageIndices,
106+
postModify: httpModify
107+
},
108+
{
109+
regex: cloudStorageRegExp,
110+
indices: cloudStorageIndices,
111+
postModify: httpModify
112+
}
95113
];
96114
for (let i = 0; i < groups.length; i++) {
97115
const group = groups[i];

packages/storage/test/service.test.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -184,10 +184,16 @@ describe('Firebase Storage > Service', () => {
184184
});
185185
it('Works with storage.googleapis.com URLs', () => {
186186
const ref = service.refFromURL(
187-
`http://storage.googleapis.com/v0/b/` +
188-
'mybucket/o/child%2Fpath%2Fimage.png?downloadToken=hello'
187+
`https://storage.googleapis.com/mybucket/path%20with%20space/image.png`
189188
);
190-
assert.equal(ref.toString(), 'gs://mybucket/child/path/image.png');
189+
assert.equal(ref.toString(), 'gs://mybucket/path with space/image.png');
190+
});
191+
it('Works with storage.googleapis.com URLs with query params', () => {
192+
const ref = service.refFromURL(
193+
`https://storage.googleapis.com/mybucket/path%20with%20space/image.png?X-Goog-Algorithm=
194+
GOOG4-RSA-SHA256`
195+
);
196+
assert.equal(ref.toString(), 'gs://mybucket/path with space/image.png');
191197
});
192198
});
193199
describe('Argument verification', () => {

0 commit comments

Comments
 (0)