Skip to content

Commit 978a87d

Browse files
fix(storage): do not set metadata property unless it has a value (#12805)
1 parent 7d4c200 commit 978a87d

File tree

5 files changed

+107
-55
lines changed

5 files changed

+107
-55
lines changed

packages/firebase_storage/firebase_storage/android/src/main/java/io/flutter/plugins/firebase/storage/FlutterFirebaseStoragePlugin.java

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -391,13 +391,23 @@ public void referenceListAll(
391391

392392
StorageMetadata getMetaDataFromPigeon(
393393
GeneratedAndroidFirebaseStorage.PigeonSettableMetadata pigeonSettableMetatdata) {
394-
StorageMetadata.Builder androidMetaDataBuilder =
395-
new StorageMetadata.Builder()
396-
.setCacheControl(pigeonSettableMetatdata.getCacheControl())
397-
.setContentDisposition(pigeonSettableMetatdata.getContentDisposition())
398-
.setContentEncoding(pigeonSettableMetatdata.getContentEncoding())
399-
.setContentLanguage(pigeonSettableMetatdata.getContentLanguage())
400-
.setContentType(pigeonSettableMetatdata.getContentType());
394+
StorageMetadata.Builder androidMetaDataBuilder = new StorageMetadata.Builder();
395+
396+
if (pigeonSettableMetatdata.getContentType() != null) {
397+
androidMetaDataBuilder.setContentType(pigeonSettableMetatdata.getContentType());
398+
}
399+
if (pigeonSettableMetatdata.getCacheControl() != null) {
400+
androidMetaDataBuilder.setCacheControl(pigeonSettableMetatdata.getCacheControl());
401+
}
402+
if (pigeonSettableMetatdata.getContentDisposition() != null) {
403+
androidMetaDataBuilder.setContentDisposition(pigeonSettableMetatdata.getContentDisposition());
404+
}
405+
if (pigeonSettableMetatdata.getContentEncoding() != null) {
406+
androidMetaDataBuilder.setContentEncoding(pigeonSettableMetatdata.getContentEncoding());
407+
}
408+
if (pigeonSettableMetatdata.getContentLanguage() != null) {
409+
androidMetaDataBuilder.setContentLanguage(pigeonSettableMetatdata.getContentLanguage());
410+
}
401411

402412
Map<String, String> pigeonCustomMetadata = pigeonSettableMetatdata.getCustomMetadata();
403413
if (pigeonCustomMetadata != null) {

packages/firebase_storage/firebase_storage/ios/Classes/FLTFirebaseStoragePlugin.m

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -175,13 +175,28 @@ - (FIRStorage *_Nullable)getFIRStorageFromAppNameFromPigeon:(PigeonStorageFireba
175175

176176
- (FIRStorageMetadata *)getFIRStorageMetadataFromPigeon:(PigeonSettableMetadata *)pigeonMetadata {
177177
FIRStorageMetadata *metadata = [[FIRStorageMetadata alloc] init];
178-
metadata.cacheControl = pigeonMetadata.cacheControl;
179-
metadata.contentDisposition = pigeonMetadata.contentDisposition;
180-
metadata.contentEncoding = pigeonMetadata.contentEncoding;
181-
metadata.contentLanguage = pigeonMetadata.contentLanguage;
182-
metadata.contentType = pigeonMetadata.contentType;
183178

184-
metadata.customMetadata = pigeonMetadata.customMetadata;
179+
if (pigeonMetadata.cacheControl != nil) {
180+
metadata.cacheControl = pigeonMetadata.cacheControl;
181+
}
182+
if (pigeonMetadata.contentType != nil) {
183+
metadata.contentType = pigeonMetadata.contentType;
184+
}
185+
if (pigeonMetadata.contentDisposition != nil) {
186+
metadata.contentDisposition = pigeonMetadata.contentDisposition;
187+
}
188+
189+
if (pigeonMetadata.contentEncoding != nil) {
190+
metadata.contentEncoding = pigeonMetadata.contentEncoding;
191+
}
192+
193+
if (pigeonMetadata.contentLanguage != nil) {
194+
metadata.contentLanguage = pigeonMetadata.contentLanguage;
195+
}
196+
197+
if (pigeonMetadata.customMetadata != nil) {
198+
metadata.customMetadata = pigeonMetadata.customMetadata;
199+
}
185200

186201
return metadata;
187202
}

packages/firebase_storage/firebase_storage_web/lib/src/interop/storage.dart

Lines changed: 60 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -251,25 +251,38 @@ class UploadMetadata
251251
extends _UploadMetadataBase<storage_interop.UploadMetadataJsImpl> {
252252
/// Creates a new UploadMetadata with optional metadata parameters.
253253
factory UploadMetadata(
254-
{String? md5Hash,
255-
String? cacheControl,
256-
String? contentDisposition,
257-
String? contentEncoding,
258-
String? contentLanguage,
259-
String? contentType,
260-
Map<String, String>? customMetadata}) =>
261-
UploadMetadata.fromJsObject(
262-
storage_interop.UploadMetadataJsImpl(
263-
md5Hash: md5Hash?.toJS,
264-
cacheControl: cacheControl?.toJS,
265-
contentDisposition: contentDisposition?.toJS,
266-
contentEncoding: contentEncoding?.toJS,
267-
contentLanguage: contentLanguage?.toJS,
268-
contentType: contentType?.toJS,
269-
customMetadata:
270-
(customMetadata != null) ? customMetadata.jsify() : null,
271-
),
272-
);
254+
{String? md5Hash,
255+
String? cacheControl,
256+
String? contentDisposition,
257+
String? contentEncoding,
258+
String? contentLanguage,
259+
String? contentType,
260+
Map<String, String>? customMetadata}) {
261+
final metadata = storage_interop.UploadMetadataJsImpl();
262+
263+
if (md5Hash != null) {
264+
metadata.md5Hash = md5Hash.toJS;
265+
}
266+
if (cacheControl != null) {
267+
metadata.cacheControl = cacheControl.toJS;
268+
}
269+
if (contentDisposition != null) {
270+
metadata.contentDisposition = contentDisposition.toJS;
271+
}
272+
if (contentEncoding != null) {
273+
metadata.contentEncoding = contentEncoding.toJS;
274+
}
275+
if (contentLanguage != null) {
276+
metadata.contentLanguage = contentLanguage.toJS;
277+
}
278+
if (contentType != null) {
279+
metadata.contentType = contentType.toJS;
280+
}
281+
if (customMetadata != null) {
282+
metadata.customMetadata = customMetadata.jsify();
283+
}
284+
return UploadMetadata.fromJsObject(metadata);
285+
}
273286

274287
/// Creates a new UploadMetadata from a [jsObject].
275288
UploadMetadata.fromJsObject(storage_interop.UploadMetadataJsImpl jsObject)
@@ -437,20 +450,34 @@ class SettableMetadata
437450
extends _SettableMetadataBase<storage_interop.SettableMetadataJsImpl> {
438451
/// Creates a new SettableMetadata with optional metadata parameters.
439452
factory SettableMetadata(
440-
{String? cacheControl,
441-
String? contentDisposition,
442-
String? contentEncoding,
443-
String? contentLanguage,
444-
String? contentType,
445-
Map? customMetadata}) =>
446-
SettableMetadata.fromJsObject(storage_interop.SettableMetadataJsImpl(
447-
cacheControl: cacheControl?.toJS,
448-
contentDisposition: contentDisposition?.toJS,
449-
contentEncoding: contentEncoding?.toJS,
450-
contentLanguage: contentLanguage?.toJS,
451-
contentType: contentType?.toJS,
452-
customMetadata:
453-
(customMetadata != null) ? customMetadata.jsify() : null));
453+
{String? cacheControl,
454+
String? contentDisposition,
455+
String? contentEncoding,
456+
String? contentLanguage,
457+
String? contentType,
458+
Map? customMetadata}) {
459+
final metadata = storage_interop.SettableMetadataJsImpl();
460+
461+
if (cacheControl != null) {
462+
metadata.cacheControl = cacheControl.toJS;
463+
}
464+
if (contentDisposition != null) {
465+
metadata.contentDisposition = contentDisposition.toJS;
466+
}
467+
if (contentEncoding != null) {
468+
metadata.contentEncoding = contentEncoding.toJS;
469+
}
470+
if (contentLanguage != null) {
471+
metadata.contentLanguage = contentLanguage.toJS;
472+
}
473+
if (contentType != null) {
474+
metadata.contentType = contentType.toJS;
475+
}
476+
if (customMetadata != null) {
477+
metadata.customMetadata = customMetadata.jsify();
478+
}
479+
return SettableMetadata.fromJsObject(metadata);
480+
}
454481

455482
/// Creates a new SettableMetadata from a [jsObject].
456483
SettableMetadata.fromJsObject(storage_interop.SettableMetadataJsImpl jsObject)

tests/android/app/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ apply plugin: 'kotlin-android'
2929
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"
3030

3131
android {
32-
compileSdkVersion 33
32+
compileSdkVersion 34
3333

3434
compileOptions {
3535
sourceCompatibility JavaVersion.VERSION_1_8
@@ -47,7 +47,7 @@ android {
4747
defaultConfig {
4848
// TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
4949
applicationId "io.flutter.plugins.firebase.tests"
50-
minSdkVersion 21
50+
minSdkVersion 24
5151
targetSdkVersion 33
5252
versionCode flutterVersionCode.toInteger()
5353
versionName flutterVersionName

tests/integration_test/firebase_storage/reference_e2e.dart

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -258,8 +258,7 @@ void setupReferenceTests() {
258258
);
259259

260260
expect(complete.metadata?.size, text.length);
261-
// Metadata isn't saved on objects when using the emulator which fails test
262-
// expect(complete.metadata?.contentLanguage, 'en');
261+
expect(complete.metadata?.contentLanguage, 'en');
263262

264263
// Download the file from Firebase Storage
265264
final downloadedData = await ref.getData();
@@ -332,22 +331,23 @@ void setupReferenceTests() {
332331
file,
333332
SettableMetadata(
334333
contentLanguage: 'en',
334+
contentType: 'text/plain',
335335
customMetadata: <String, String>{'activity': 'test'},
336336
),
337337
);
338-
338+
// metadata.contentType appears as application/octet-stream if not set. contentType is not inferred on emulator
339339
expect(complete.metadata?.size, kTestString.length);
340-
// Metadata isn't saved on objects when using the emulator which fails test
341-
// expect(complete.metadata?.contentLanguage, 'en');
342-
// expect(complete.metadata?.customMetadata!['activity'], 'test');
343-
340+
expect(complete.metadata?.contentLanguage, 'en');
341+
expect(complete.metadata?.customMetadata!['activity'], 'test');
342+
expect(complete.metadata?.contentType, 'text/plain');
344343
// Check without SettableMetadata
345344
final Reference ref2 =
346345
storage.ref('flutter-tests').child('flt-ok-2.txt');
347346
final TaskSnapshot complete2 = await ref2.putFile(
348347
file,
349348
);
350349
expect(complete2.metadata?.size, kTestString.length);
350+
expect(complete2.metadata?.customMetadata, isA<Map>());
351351
},
352352
// putFile is not supported on the web platform.
353353
skip: kIsWeb,

0 commit comments

Comments
 (0)