Skip to content

Commit

Permalink
feat(storage): allow user to delete custom metadata to match underlyi…
Browse files Browse the repository at this point in the history
…ng SDKs behavior
  • Loading branch information
Lyokone committed Mar 19, 2024
1 parent 74b9a05 commit 1f1fb83
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class SettableMetadata {
final String? contentType;

/// Additional user-defined custom metadata.
final Map<String, String>? customMetadata;
final Map<String, String?>? customMetadata;

/// Returns the settable metadata as a [Map].
Map<String, dynamic> asMap() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,26 @@ storage_interop.SettableMetadata settableMetadataToFbSettableMetadata(
storage_interop.UploadMetadata settableMetadataToFbUploadMetadata(
SettableMetadata metadata,
{String? md5Hash}) {
// We are removing null values from the customMetadata map.
// since we are setting the whole map, whereas in Android and iOS we can
// set individual keys.
final listOfCustomMetadata = metadata.customMetadata?.entries
.where((entry) => entry.value != null)
.map((entry) => MapEntry(entry.key, entry.value))
.cast<MapEntry<String, String>>();

final Map<String, String>? filteredCustomMetadata =
listOfCustomMetadata != null
? Map.fromEntries(listOfCustomMetadata)
: null;

return storage_interop.UploadMetadata(
cacheControl: metadata.cacheControl,
contentDisposition: metadata.contentDisposition,
contentEncoding: metadata.contentEncoding,
contentLanguage: metadata.contentLanguage,
contentType: metadata.contentType,
customMetadata: metadata.customMetadata,
customMetadata: filteredCustomMetadata,
md5Hash: md5Hash,
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class SettableMetadataCache {
return _cache;
}

final newMetadata = <String, String>{
final newMetadata = <String, String?>{
...?incoming.customMetadata,
...?_cache.customMetadata,
};
Expand Down
21 changes: 21 additions & 0 deletions tests/integration_test/firebase_storage/reference_e2e.dart
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,27 @@ void setupReferenceTests() {
expect(fullMetadata.customMetadata!['foo'], 'bar');
});

test('delete metadata', () async {
Reference ref = storage.ref('flutter-tests').child('flt-ok.txt');
FullMetadata fullMetadata = await ref
.updateMetadata(SettableMetadata(customMetadata: {'foo': 'bar'}));
expect(fullMetadata.customMetadata!['foo'], 'bar');

fullMetadata = await ref.updateMetadata(
SettableMetadata(
customMetadata: {
'foo': null,
},
),
);
expect(fullMetadata.customMetadata!['foo'], isNull);

// Setting it again
fullMetadata = await ref
.updateMetadata(SettableMetadata(customMetadata: {'foo': 'bar2'}));
expect(fullMetadata.customMetadata!['foo'], 'bar2');
});

test('errors if property does not exist', () async {
Reference ref = storage.ref('flutter-tests/iDoNotExist.jpeg');

Expand Down

0 comments on commit 1f1fb83

Please sign in to comment.