Skip to content

Commit

Permalink
feat(storage, md5hash): allow md5hash to be set on upload
Browse files Browse the repository at this point in the history
metadata property md5hash is allowed for upload, just not for updateMetadata
allow it to go through in the putNNN scenarios but screen for it on update
  • Loading branch information
mikehardy committed May 19, 2021
1 parent 1d3e946 commit be1bed8
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 7 deletions.
47 changes: 47 additions & 0 deletions packages/storage/e2e/StorageReference.e2e.js
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,10 @@ describe('storage() -> StorageReference', function () {
const storageReference = firebase.storage().ref(WRITE_ONLY_NAME);
const metadata = await storageReference.updateMetadata({
contentType: 'image/jpeg',
cacheControl: 'true',
contentDisposition: 'disposed',
contentEncoding: 'encoded',
contentLanguage: 'martian',
customMetadata: {
hello: 'world',
},
Expand Down Expand Up @@ -406,6 +410,19 @@ describe('storage() -> StorageReference', function () {
// FIXME this is failing the part that fails
should.equal(metadataAfterRemove.customMetadata.removeMe, undefined);
});

it('should error if updateMetadata includes md5hash', async function () {
const storageReference = firebase.storage().ref(WRITE_ONLY_NAME);
try {
await storageReference.updateMetadata({
md5hash: '0xDEADBEEF',
});
return Promise.reject(new Error('Did not throw on invalid updateMetadata'));
} catch (e) {
e.message.should.containEql('md5hash may only be set on upload, not on updateMetadata');
return Promise.resolve();
}
});
});

describe('putFile', function () {
Expand Down Expand Up @@ -515,6 +532,21 @@ describe('storage() -> StorageReference', function () {
return Promise.resolve();
}
});

it('allows valid metadata properties for upload', async function () {
const storageReference = firebase.storage().ref(`${PATH}/metadataTest.txt`);
await storageReference.putString('foo', 'raw', {
contentType: 'text/plain',
md5hash: '123412341234',
cacheControl: 'true',
contentDisposition: 'disposed',
contentEncoding: 'encoded',
contentLanguage: 'martian',
customMetadata: {
customMetadata1: 'metadata1value',
},
});
});
});

describe('put', function () {
Expand Down Expand Up @@ -563,5 +595,20 @@ describe('storage() -> StorageReference', function () {
return Promise.resolve();
}
});

it('allows valid metadata properties for upload', async function () {
const storageReference = firebase.storage().ref(`${PATH}/metadataTest.jpeg`);
await storageReference.put(new jet.context.window.ArrayBuffer(), {
contentType: 'image/jpg',
md5hash: '123412341234',
cacheControl: 'true',
contentDisposition: 'disposed',
contentEncoding: 'encoded',
contentLanguage: 'martian',
customMetadata: {
customMetadata1: 'metadata1value',
},
});
});
});
});
12 changes: 6 additions & 6 deletions packages/storage/lib/StorageReference.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,12 +179,12 @@ export default class StorageReference extends ReferenceBase {
*/
put(data, metadata) {
if (!isUndefined(metadata)) {
validateMetadata(metadata);
validateMetadata(metadata, false);
}

return new StorageUploadTask(this, task =>
Base64.fromData(data).then(({ string, format }) => {
const { _string, _format, _metadata } = this._updateString(string, format, metadata);
const { _string, _format, _metadata } = this._updateString(string, format, metadata, false);
return this._storage.native.putString(
this.toString(),
_string,
Expand All @@ -200,7 +200,7 @@ export default class StorageReference extends ReferenceBase {
* @url https://firebase.google.com/docs/reference/js/firebase.storage.Reference#putString
*/
putString(string, format = StorageStatics.StringFormat.RAW, metadata) {
const { _string, _format, _metadata } = this._updateString(string, format, metadata);
const { _string, _format, _metadata } = this._updateString(string, format, metadata, false);

return new StorageUploadTask(this, task =>
this._storage.native.putString(this.toString(), _string, _format, _metadata, task._id),
Expand Down Expand Up @@ -250,7 +250,7 @@ export default class StorageReference extends ReferenceBase {
*/
putFile(filePath, metadata) {
if (!isUndefined(metadata)) {
validateMetadata(metadata);
validateMetadata(metadata, false);
}

if (!isString(filePath)) {
Expand All @@ -264,7 +264,7 @@ export default class StorageReference extends ReferenceBase {
);
}

_updateString(string, format, metadata) {
_updateString(string, format, metadata, update = false) {
if (!isString(string)) {
throw new Error(
"firebase.storage.StorageReference.putString(*, _, _) 'string' expects a string value.",
Expand All @@ -280,7 +280,7 @@ export default class StorageReference extends ReferenceBase {
}

if (!isUndefined(metadata)) {
validateMetadata(metadata);
validateMetadata(metadata, update);
}

let _string = string;
Expand Down
5 changes: 5 additions & 0 deletions packages/storage/lib/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,11 @@ export namespace FirebaseStorageTypes {
*/
contentType?: string | null;

/**
* You may specify the md5hash of the file in metadata on upload only. It may not be updated via updateMetadata
*/
md5hash?: string | null;

/**
* Additional user-defined custom metadata for this storage object.
*
Expand Down
10 changes: 9 additions & 1 deletion packages/storage/lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const SETTABLE_FIELDS = [
'contentLanguage',
'contentType',
'customMetadata',
'md5hash',
];

export async function handleStorageEvent(storageInstance, event) {
Expand Down Expand Up @@ -57,7 +58,7 @@ export function getGsUrlParts(url) {
return { bucket, path };
}

export function validateMetadata(metadata) {
export function validateMetadata(metadata, update = true) {
if (!isObject(metadata)) {
throw new Error('firebase.storage.SettableMetadata must be an object value if provided.');
}
Expand All @@ -73,6 +74,13 @@ export function validateMetadata(metadata) {
);
}

// md5 is only allowed on put, not on update
if (key === 'md5hash' && update === true) {
throw new Error(
`firebase.storage.SettableMetadata md5hash may only be set on upload, not on updateMetadata`,
);
}

// validate values
if (key !== 'customMetadata') {
if (!isString(value) && !isNull(value)) {
Expand Down

1 comment on commit be1bed8

@vercel
Copy link

@vercel vercel bot commented on be1bed8 May 19, 2021

Choose a reason for hiding this comment

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

Please sign in to comment.