Created
February 22, 2016 21:04
-
-
Save dhrrgn/42bafbb984c3e6e4941e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// RNS3Uploader.h | |
// Squad | |
// | |
// Created by Dan Horrigan on 10/6/15. | |
// Copyright © 2015 Tackk. All rights reserved. | |
// | |
#import <Foundation/Foundation.h> | |
#import "RCTBridgeModule.h" | |
@interface RNS3Uploader : NSObject <RCTBridgeModule> | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// RNS3Uploader.m | |
// Squad | |
// | |
// Created by Dan Horrigan on 10/6/15. | |
// Copyright © 2015 Tackk. All rights reserved. | |
// | |
#import "RNS3Uploader.h" | |
#import <MobileCoreServices/MobileCoreServices.h> | |
#import <CommonCrypto/CommonCrypto.h> | |
#import <AWSS3/AWSS3.h> | |
@implementation RNS3Uploader | |
RCT_EXPORT_MODULE(); | |
RCT_EXPORT_METHOD(upload:(NSString *)filename | |
bucket:(NSString *)bucket | |
key:(NSString *)photoKey | |
resolver:(RCTPromiseResolveBlock)resolve | |
rejecter:(RCTPromiseRejectBlock)reject) | |
{ | |
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ | |
AWSS3TransferManagerUploadRequest *uploadRequest = [AWSS3TransferManagerUploadRequest new]; | |
uploadRequest.body = [NSURL fileURLWithPath:filename]; | |
uploadRequest.key = photoKey; | |
uploadRequest.contentType = [self mimeTypeForFileAtPath:filename]; | |
uploadRequest.bucket = bucket; | |
uploadRequest.ACL = AWSS3ObjectCannedACLPublicRead; | |
[self doUpload:uploadRequest key:photoKey resolver:resolve rejecter:reject]; | |
}); | |
} | |
- (void)doUpload:(AWSS3TransferManagerUploadRequest *)uploadRequest | |
key:(NSString *)photoKey | |
resolver:(RCTPromiseResolveBlock)resolve | |
rejecter:(RCTPromiseRejectBlock)reject | |
{ | |
AWSS3TransferManager *transferManager = [AWSS3TransferManager defaultS3TransferManager]; | |
[[transferManager upload:uploadRequest] continueWithBlock:^id(AWSTask *task) { | |
if (task.error) { | |
reject([@(task.error.code) stringValue], task.error.localizedDescription, task.error); | |
} else if (task.exception) { | |
NSMutableDictionary *errorInfo = [NSMutableDictionary dictionary]; | |
errorInfo[NSLocalizedDescriptionKey] = [NSString stringWithFormat:@"An exception occurred while uploading the photo: %@", task.exception.name]; | |
if (task.exception.reason) { | |
errorInfo[NSLocalizedFailureReasonErrorKey] = task.exception.reason; | |
} | |
NSError *error = [NSError errorWithDomain:@"com.test" code:100 userInfo:errorInfo]; | |
reject([@(error.code) stringValue], error.localizedDescription, error); | |
} else if (task.cancelled) { | |
NSDictionary *uploadInfo = @{NSLocalizedDescriptionKey: NSLocalizedString(@"Operation cancelled.", nil)}; | |
NSError *error = [NSError errorWithDomain:@"com.test" code:100 userInfo:uploadInfo]; | |
reject([@(error.code) stringValue], error.localizedDescription, error); | |
} | |
if (task.result) { | |
resolve(photoKey); | |
} | |
return nil; | |
}]; | |
} | |
- (NSString*) mimeTypeForFileAtPath: (NSString *)path | |
{ | |
if (![[NSFileManager defaultManager] fileExistsAtPath:path]) { | |
return nil; | |
} | |
CFStringRef UTI = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, (__bridge CFStringRef)[path pathExtension], NULL); | |
CFStringRef mimeType = UTTypeCopyPreferredTagWithClass (UTI, kUTTagClassMIMEType); | |
CFRelease(UTI); | |
if (!mimeType) { | |
return @"application/octet-stream"; | |
} | |
return (__bridge NSString *)mimeType; | |
} | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react-native'; | |
const { NativeModules: { RNS3Uploader } } = React; | |
// In some component | |
RNS3Uploader.upload(filePathToUpload, s3Bucket, s3Key); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment