Skip to content

Commit 774e9ad

Browse files
committed
Fixes #3385
1 parent 6714788 commit 774e9ad

File tree

2 files changed

+72
-15
lines changed

2 files changed

+72
-15
lines changed

scripts/storage-emulator-integration/tests.ts

+40-12
Original file line numberDiff line numberDiff line change
@@ -1019,22 +1019,50 @@ describe("Storage emulator", () => {
10191019
});
10201020
});
10211021

1022-
it("#setMetadata()", async () => {
1023-
const metadata = await page.evaluate((filename) => {
1024-
return firebase
1025-
.storage()
1026-
.ref(filename)
1027-
.updateMetadata({
1022+
describe("#setMetadata()", () => {
1023+
it("should allow for custom metadata to be set", async () => {
1024+
const metadata = await page.evaluate((filename) => {
1025+
return firebase
1026+
.storage()
1027+
.ref(filename)
1028+
.updateMetadata({
1029+
customMetadata: {
1030+
is_over: "9000",
1031+
},
1032+
})
1033+
.then(() => {
1034+
return firebase.storage().ref(filename).getMetadata();
1035+
});
1036+
}, filename);
1037+
1038+
expect(metadata.customMetadata.is_over).to.equal("9000");
1039+
});
1040+
1041+
it("should allow deletion of custom metadata by setting to null", async () => {
1042+
const setMetadata = await page.evaluate((filename) => {
1043+
const storageReference = firebase.storage().ref(filename);
1044+
return storageReference.updateMetadata({
1045+
contentType: "text/plain",
10281046
customMetadata: {
1029-
is_over: "9000",
1047+
removeMe: "please",
10301048
},
1031-
})
1032-
.then(() => {
1033-
return firebase.storage().ref(filename).getMetadata();
10341049
});
1035-
}, filename);
1050+
}, filename);
1051+
1052+
expect(setMetadata.customMetadata.removeMe).to.equal("please");
10361053

1037-
expect(metadata.customMetadata.is_over).to.equal("9000");
1054+
const nulledMetadata = await page.evaluate((filename) => {
1055+
const storageReference = firebase.storage().ref(filename);
1056+
return storageReference.updateMetadata({
1057+
contentType: "text/plain",
1058+
customMetadata: {
1059+
removeMe: null as any,
1060+
},
1061+
});
1062+
}, filename);
1063+
1064+
expect(nulledMetadata.customMetadata.removeMe).to.equal(undefined);
1065+
});
10381066
});
10391067

10401068
it("#delete()", async () => {

src/emulator/storage/metadata.ts

+32-3
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,9 @@ export class StoredFileMetadata {
8888
this.update(incomingMetadata);
8989
}
9090

91-
this.applyDownloadTokensFromMetadata();
91+
92+
this.deleteFieldsSetAsNull();
93+
this.setDownloadTokensFromCustomMetadata();
9294
}
9395

9496
asRulesResource(proposedChanges?: RulesResourceMetadataOverrides): RulesResourceMetadata {
@@ -130,7 +132,7 @@ export class StoredFileMetadata {
130132
return rulesResource;
131133
}
132134

133-
private applyDownloadTokensFromMetadata() {
135+
private setDownloadTokensFromCustomMetadata() {
134136
if (!this.customMetadata) return;
135137

136138
if (this.customMetadata.firebaseStorageDownloadTokens) {
@@ -139,6 +141,31 @@ export class StoredFileMetadata {
139141
}
140142
}
141143

144+
private deleteFieldsSetAsNull() {
145+
const deletableFields: (keyof this)[] = [
146+
"contentDisposition",
147+
"contentType",
148+
"contentLanguage",
149+
"contentEncoding",
150+
"cacheControl",
151+
];
152+
153+
deletableFields.map((field: keyof this) => {
154+
if (this[field] === null) {
155+
delete this[field];
156+
}
157+
});
158+
159+
if (this.customMetadata) {
160+
Object.keys(this.customMetadata).map((key: string) => {
161+
if (!this.customMetadata) return;
162+
if (this.customMetadata[key] === null) {
163+
delete this.customMetadata[key];
164+
}
165+
});
166+
}
167+
}
168+
142169
update(incoming: IncomingMetadata): void {
143170
if (incoming.contentDisposition) {
144171
this.contentDisposition = incoming.contentDisposition;
@@ -150,7 +177,6 @@ export class StoredFileMetadata {
150177

151178
if (incoming.metadata) {
152179
this.customMetadata = incoming.metadata;
153-
this.applyDownloadTokensFromMetadata();
154180
}
155181

156182
if (incoming.contentLanguage) {
@@ -171,6 +197,9 @@ export class StoredFileMetadata {
171197
this.cacheControl = incoming.cacheControl;
172198
}
173199

200+
this.setDownloadTokensFromCustomMetadata();
201+
this.deleteFieldsSetAsNull();
202+
174203
this._cloudFunctions.dispatch("metadataUpdate", new CloudStorageObjectMetadata(this));
175204
}
176205

0 commit comments

Comments
 (0)