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) {
StorageMetadata.Builder metadataBuilder = new StorageMetadata.Builder();
Map<String, Object> m = Utils.recursivelyDeconstructReadableMap(metadata);
for (Map.Entry<String, Object> entry : m.entrySet()) {
metadataBuilder.setCustomMetadata(entry.getKey(), entry.getValue().toString());
try {
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();

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) {
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) {
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 {
FIRStorageReference *fileRef = [self getReference:path];
FIRStorageMetadata *firmetadata = [[FIRStorageMetadata alloc] initWithDictionary:metadata];
FIRStorageMetadata *firmetadata = [self buildMetadataFromMap:metadata];
FIRStorageUploadTask *uploadTask = [fileRef putFile:url metadata:firmetadata];
[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{
FIRStorageReference *fileRef = [self getReference:path];
FIRStorageMetadata *firmetadata = [[FIRStorageMetadata alloc] initWithDictionary:metadata];
FIRStorageMetadata *firmetadata = [self buildMetadataFromMap:metadata];
FIRStorageUploadTask *uploadTask = [fileRef putData:data metadata:firmetadata];
[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 {
if (status == FIRStorageTaskStatusResume || status == FIRStorageTaskStatusProgress) {
return @"running";