Merge pull request #217 from nico1510/fix-customMetadata

Fix custom metadata
This commit is contained in:
Michael Diarmid 2017-06-30 14:25:19 +01:00 committed by GitHub
commit b79a07c85b
2 changed files with 29 additions and 6 deletions

View File

@ -428,10 +428,26 @@ public class RNFirebaseStorage extends ReactContextBaseJavaModule {
*/ */
private StorageMetadata buildMetadataFromMap(ReadableMap metadata) { private StorageMetadata buildMetadataFromMap(ReadableMap metadata) {
StorageMetadata.Builder metadataBuilder = new StorageMetadata.Builder(); StorageMetadata.Builder metadataBuilder = new StorageMetadata.Builder();
Map<String, Object> m = Utils.recursivelyDeconstructReadableMap(metadata);
for (Map.Entry<String, Object> entry : m.entrySet()) { try {
metadataBuilder.setCustomMetadata(entry.getKey(), entry.getValue().toString());
Map<String, Object> m = Utils.recursivelyDeconstructReadableMap(metadata);
Map<String, Object> customMetadata = (Map<String, Object>) m.get("customMetadata");
if (customMetadata != null) {
for (Map.Entry<String, Object> entry : customMetadata.entrySet()) {
metadataBuilder.setCustomMetadata(entry.getKey(), String.valueOf(entry.getValue()));
}
}
metadataBuilder.setCacheControl((String) m.get("cacheControl"));
metadataBuilder.setContentDisposition((String) m.get("contentDisposition"));
metadataBuilder.setContentEncoding((String) m.get("contentEncoding"));
metadataBuilder.setContentLanguage((String) m.get("contentLanguage"));
metadataBuilder.setContentType((String) m.get("contentType"));
} catch (Exception e) {
Log.e(TAG, "error while building meta data " + e.getMessage());
} }
return metadataBuilder.build(); return metadataBuilder.build();

View File

@ -154,7 +154,7 @@ RCT_EXPORT_METHOD(getMetadata: (NSString *) path resolver:(RCTPromiseResolveBloc
*/ */
RCT_EXPORT_METHOD(updateMetadata: (NSString *) path metadata:(NSDictionary *) metadata resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) { RCT_EXPORT_METHOD(updateMetadata: (NSString *) path metadata:(NSDictionary *) metadata resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) {
FIRStorageReference *fileRef = [self getReference:path]; FIRStorageReference *fileRef = [self getReference:path];
FIRStorageMetadata *firmetadata = [[FIRStorageMetadata alloc] initWithDictionary:metadata]; FIRStorageMetadata *firmetadata = [self buildMetadataFromMap:metadata];
[fileRef updateMetadata:firmetadata completion:^(FIRStorageMetadata * _Nullable metadata, NSError * _Nullable error) { [fileRef updateMetadata:firmetadata completion:^(FIRStorageMetadata * _Nullable metadata, NSError * _Nullable error) {
if (error != nil) { if (error != nil) {
@ -317,14 +317,14 @@ RCT_EXPORT_METHOD(putFile:(NSString *) path localPath:(NSString *)localPath meta
- (void) uploadFile:(NSURL *) url metadata:(NSDictionary *) metadata path:(NSString *) path resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject { - (void) uploadFile:(NSURL *) url metadata:(NSDictionary *) metadata path:(NSString *) path resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject {
FIRStorageReference *fileRef = [self getReference:path]; FIRStorageReference *fileRef = [self getReference:path];
FIRStorageMetadata *firmetadata = [[FIRStorageMetadata alloc] initWithDictionary:metadata]; FIRStorageMetadata *firmetadata = [self buildMetadataFromMap:metadata];
FIRStorageUploadTask *uploadTask = [fileRef putFile:url metadata:firmetadata]; FIRStorageUploadTask *uploadTask = [fileRef putFile:url metadata:firmetadata];
[self addUploadObservers:uploadTask path:path resolver:resolve rejecter:reject]; [self addUploadObservers:uploadTask path:path resolver:resolve rejecter:reject];
} }
- (void) uploadData:(NSData *) data metadata:(NSDictionary *) metadata path:(NSString *) path resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject{ - (void) uploadData:(NSData *) data metadata:(NSDictionary *) metadata path:(NSString *) path resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject{
FIRStorageReference *fileRef = [self getReference:path]; FIRStorageReference *fileRef = [self getReference:path];
FIRStorageMetadata *firmetadata = [[FIRStorageMetadata alloc] initWithDictionary:metadata]; FIRStorageMetadata *firmetadata = [self buildMetadataFromMap:metadata];
FIRStorageUploadTask *uploadTask = [fileRef putData:data metadata:firmetadata]; FIRStorageUploadTask *uploadTask = [fileRef putData:data metadata:firmetadata];
[self addUploadObservers:uploadTask path:path resolver:resolve rejecter:reject]; [self addUploadObservers:uploadTask path:path resolver:resolve rejecter:reject];
} }
@ -394,6 +394,13 @@ RCT_EXPORT_METHOD(putFile:(NSString *) path localPath:(NSString *)localPath meta
}; };
} }
- (FIRStorageMetadata *)buildMetadataFromMap:(NSDictionary *)metadata {
NSMutableDictionary *result = [metadata mutableCopy];
result[@"metadata"] = metadata[@"customMetadata"];
[result removeObjectForKey:@"customMetadata"];
return [[FIRStorageMetadata alloc] initWithDictionary:result];
}
- (NSString *)getTaskStatus:(FIRStorageTaskStatus)status { - (NSString *)getTaskStatus:(FIRStorageTaskStatus)status {
if (status == FIRStorageTaskStatusResume || status == FIRStorageTaskStatusProgress) { if (status == FIRStorageTaskStatusResume || status == FIRStorageTaskStatusProgress) {
return @"running"; return @"running";