Skip to content

Commit

Permalink
Fixes #3385
Browse files Browse the repository at this point in the history
  • Loading branch information
abeisgoat committed May 25, 2021
1 parent 6714788 commit 774e9ad
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 15 deletions.
52 changes: 40 additions & 12 deletions scripts/storage-emulator-integration/tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1019,22 +1019,50 @@ describe("Storage emulator", () => {
});
});

it("#setMetadata()", async () => {
const metadata = await page.evaluate((filename) => {
return firebase
.storage()
.ref(filename)
.updateMetadata({
describe("#setMetadata()", () => {
it("should allow for custom metadata to be set", async () => {
const metadata = await page.evaluate((filename) => {
return firebase
.storage()
.ref(filename)
.updateMetadata({
customMetadata: {
is_over: "9000",
},
})
.then(() => {
return firebase.storage().ref(filename).getMetadata();
});
}, filename);

expect(metadata.customMetadata.is_over).to.equal("9000");
});

it("should allow deletion of custom metadata by setting to null", async () => {
const setMetadata = await page.evaluate((filename) => {
const storageReference = firebase.storage().ref(filename);
return storageReference.updateMetadata({
contentType: "text/plain",
customMetadata: {
is_over: "9000",
removeMe: "please",
},
})
.then(() => {
return firebase.storage().ref(filename).getMetadata();
});
}, filename);
}, filename);

expect(setMetadata.customMetadata.removeMe).to.equal("please");

expect(metadata.customMetadata.is_over).to.equal("9000");
const nulledMetadata = await page.evaluate((filename) => {
const storageReference = firebase.storage().ref(filename);
return storageReference.updateMetadata({
contentType: "text/plain",
customMetadata: {
removeMe: null as any,
},
});
}, filename);

expect(nulledMetadata.customMetadata.removeMe).to.equal(undefined);
});
});

it("#delete()", async () => {
Expand Down
35 changes: 32 additions & 3 deletions src/emulator/storage/metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,9 @@ export class StoredFileMetadata {
this.update(incomingMetadata);
}

this.applyDownloadTokensFromMetadata();

this.deleteFieldsSetAsNull();
this.setDownloadTokensFromCustomMetadata();
}

asRulesResource(proposedChanges?: RulesResourceMetadataOverrides): RulesResourceMetadata {
Expand Down Expand Up @@ -130,7 +132,7 @@ export class StoredFileMetadata {
return rulesResource;
}

private applyDownloadTokensFromMetadata() {
private setDownloadTokensFromCustomMetadata() {
if (!this.customMetadata) return;

if (this.customMetadata.firebaseStorageDownloadTokens) {
Expand All @@ -139,6 +141,31 @@ export class StoredFileMetadata {
}
}

private deleteFieldsSetAsNull() {
const deletableFields: (keyof this)[] = [
"contentDisposition",
"contentType",
"contentLanguage",
"contentEncoding",
"cacheControl",
];

deletableFields.map((field: keyof this) => {
if (this[field] === null) {
delete this[field];
}
});

if (this.customMetadata) {
Object.keys(this.customMetadata).map((key: string) => {
if (!this.customMetadata) return;
if (this.customMetadata[key] === null) {
delete this.customMetadata[key];
}
});
}
}

update(incoming: IncomingMetadata): void {
if (incoming.contentDisposition) {
this.contentDisposition = incoming.contentDisposition;
Expand All @@ -150,7 +177,6 @@ export class StoredFileMetadata {

if (incoming.metadata) {
this.customMetadata = incoming.metadata;
this.applyDownloadTokensFromMetadata();
}

if (incoming.contentLanguage) {
Expand All @@ -171,6 +197,9 @@ export class StoredFileMetadata {
this.cacheControl = incoming.cacheControl;
}

this.setDownloadTokensFromCustomMetadata();
this.deleteFieldsSetAsNull();

this._cloudFunctions.dispatch("metadataUpdate", new CloudStorageObjectMetadata(this));
}

Expand Down

0 comments on commit 774e9ad

Please sign in to comment.