Skip to content

Commit e7b230c

Browse files
Support storage.googleapis.com in refFromUrl (#2758)
1 parent 17e3e61 commit e7b230c

File tree

2 files changed

+52
-8
lines changed

2 files changed

+52
-8
lines changed

packages/storage/src/implementation/location.ts

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* @license
3-
* Copyright 2017 Google Inc.
3+
* Copyright 2017 Google LLC
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
66
* you may not use this file except in compliance with the License.
@@ -76,23 +76,42 @@ export class Location {
7676
}
7777
}
7878
const gsPath = '(/(.*))?$';
79-
const path = '(/([^?#]*).*)?$';
8079
const gsRegex = new RegExp('^gs://' + bucketDomain + gsPath, 'i');
8180
const gsIndices = { bucket: 1, path: 3 };
8281

8382
function httpModify(loc: Location): void {
8483
loc.path_ = decodeURIComponent(loc.path);
8584
}
8685
const version = 'v[A-Za-z0-9_]+';
87-
const hostRegex = DEFAULT_HOST.replace(/[.]/g, '\\.');
88-
const httpRegex = new RegExp(
89-
`^https?://${hostRegex}/${version}/b/${bucketDomain}/o${path}`,
86+
const firebaseStorageHost = DEFAULT_HOST.replace(/[.]/g, '\\.');
87+
const firebaseStoragePath = '(/([^?#]*).*)?$';
88+
const firebaseStorageRegExp = new RegExp(
89+
`^https?://${firebaseStorageHost}/${version}/b/${bucketDomain}/o${firebaseStoragePath}`,
9090
'i'
9191
);
92-
const httpIndices = { bucket: 1, path: 3 };
92+
const firebaseStorageIndices = { bucket: 1, path: 3 };
93+
94+
const cloudStorageHost =
95+
'(?:storage.googleapis.com|storage.cloud.google.com)';
96+
const cloudStoragePath = '([^?#]*)';
97+
const cloudStorageRegExp = new RegExp(
98+
`^https?://${cloudStorageHost}/${bucketDomain}/${cloudStoragePath}`,
99+
'i'
100+
);
101+
const cloudStorageIndices = { bucket: 1, path: 2 };
102+
93103
const groups = [
94104
{ regex: gsRegex, indices: gsIndices, postModify: gsModify },
95-
{ regex: httpRegex, indices: httpIndices, postModify: httpModify }
105+
{
106+
regex: firebaseStorageRegExp,
107+
indices: firebaseStorageIndices,
108+
postModify: httpModify
109+
},
110+
{
111+
regex: cloudStorageRegExp,
112+
indices: cloudStorageIndices,
113+
postModify: httpModify
114+
}
96115
];
97116
for (let i = 0; i < groups.length; i++) {
98117
const group = groups[i];

packages/storage/test/service.test.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* @license
3-
* Copyright 2017 Google Inc.
3+
* Copyright 2017 Google LLC
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
66
* you may not use this file except in compliance with the License.
@@ -182,6 +182,31 @@ describe('Firebase Storage > Service', () => {
182182
);
183183
assert.equal(ref.toString(), 'gs://mybucket/child/path/image.png');
184184
});
185+
it('Works with storage.googleapis.com URLs', () => {
186+
const ref = service.refFromURL(
187+
`https://storage.googleapis.com/mybucket/path%20with%20space/image.png`
188+
);
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');
197+
});
198+
it('Works with storage.cloud.google.com URLs', () => {
199+
const ref = service.refFromURL(
200+
`https://storage.cloud.google.com/mybucket/path%20with%20space/image.png`
201+
);
202+
assert.equal(ref.toString(), 'gs://mybucket/path with space/image.png');
203+
});
204+
it('Works with storage.cloud.google.com URLs and escaped slash', () => {
205+
const ref = service.refFromURL(
206+
`https://storage.cloud.google.com/mybucket/path%20with%20space%2Fimage.png`
207+
);
208+
assert.equal(ref.toString(), 'gs://mybucket/path with space/image.png');
209+
});
185210
});
186211
describe('Argument verification', () => {
187212
const service = new Service(

0 commit comments

Comments
 (0)