Skip to content

Support storage.googleapis.com in refFromUrl #2758

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Mar 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 26 additions & 7 deletions packages/storage/src/implementation/location.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* @license
* Copyright 2017 Google Inc.
* Copyright 2017 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -76,23 +76,42 @@ export class Location {
}
}
const gsPath = '(/(.*))?$';
const path = '(/([^?#]*).*)?$';
const gsRegex = new RegExp('^gs://' + bucketDomain + gsPath, 'i');
const gsIndices = { bucket: 1, path: 3 };

function httpModify(loc: Location): void {
loc.path_ = decodeURIComponent(loc.path);
}
const version = 'v[A-Za-z0-9_]+';
const hostRegex = DEFAULT_HOST.replace(/[.]/g, '\\.');
const httpRegex = new RegExp(
`^https?://${hostRegex}/${version}/b/${bucketDomain}/o${path}`,
const firebaseStorageHost = DEFAULT_HOST.replace(/[.]/g, '\\.');
const firebaseStoragePath = '(/([^?#]*).*)?$';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's special about #? Trying to understand the difference between firebaseStoragePath and cloudStoragePath.

Copy link
Contributor Author

@schmidt-sebastian schmidt-sebastian Mar 26, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably exclude # in both cases, even though I don't see GCS creating URLs that provide anchors. I updated the cloudStoragePath.

I am not sure why we are using an extra .* in firebaseStoragePath, but I'd rather leave it the way it was as I can see neither harm nor benefit.

const firebaseStorageRegExp = new RegExp(
`^https?://${firebaseStorageHost}/${version}/b/${bucketDomain}/o${firebaseStoragePath}`,
'i'
);
const httpIndices = { bucket: 1, path: 3 };
const firebaseStorageIndices = { bucket: 1, path: 3 };

const cloudStorageHost =
'(?:storage.googleapis.com|storage.cloud.google.com)';
const cloudStoragePath = '([^?#]*)';
const cloudStorageRegExp = new RegExp(
`^https?://${cloudStorageHost}/${bucketDomain}/${cloudStoragePath}`,
'i'
);
const cloudStorageIndices = { bucket: 1, path: 2 };

const groups = [
{ regex: gsRegex, indices: gsIndices, postModify: gsModify },
{ regex: httpRegex, indices: httpIndices, postModify: httpModify }
{
regex: firebaseStorageRegExp,
indices: firebaseStorageIndices,
postModify: httpModify
},
{
regex: cloudStorageRegExp,
indices: cloudStorageIndices,
postModify: httpModify
}
];
for (let i = 0; i < groups.length; i++) {
const group = groups[i];
Expand Down
27 changes: 26 additions & 1 deletion packages/storage/test/service.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* @license
* Copyright 2017 Google Inc.
* Copyright 2017 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -182,6 +182,31 @@ describe('Firebase Storage > Service', () => {
);
assert.equal(ref.toString(), 'gs://mybucket/child/path/image.png');
});
it('Works with storage.googleapis.com URLs', () => {
const ref = service.refFromURL(
`https://storage.googleapis.com/mybucket/path%20with%20space/image.png`
);
assert.equal(ref.toString(), 'gs://mybucket/path with space/image.png');
});
it('Works with storage.googleapis.com URLs with query params', () => {
const ref = service.refFromURL(
`https://storage.googleapis.com/mybucket/path%20with%20space/image.png?X-Goog-Algorithm=
GOOG4-RSA-SHA256`
);
assert.equal(ref.toString(), 'gs://mybucket/path with space/image.png');
});
it('Works with storage.cloud.google.com URLs', () => {
const ref = service.refFromURL(
`https://storage.cloud.google.com/mybucket/path%20with%20space/image.png`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OOC, will this generate the same result?
https://storage.cloud.google.com/mybucket/path%20with%20space%2Fimage.png

Maybe add a test.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a test.

);
assert.equal(ref.toString(), 'gs://mybucket/path with space/image.png');
});
it('Works with storage.cloud.google.com URLs and escaped slash', () => {
const ref = service.refFromURL(
`https://storage.cloud.google.com/mybucket/path%20with%20space%2Fimage.png`
);
assert.equal(ref.toString(), 'gs://mybucket/path with space/image.png');
});
});
describe('Argument verification', () => {
const service = new Service(
Expand Down