Skip to content

Commit

Permalink
version bump
Browse files Browse the repository at this point in the history
  • Loading branch information
StevePotter committed Feb 8, 2018
1 parent 30c1326 commit 709228c
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 25 deletions.
64 changes: 40 additions & 24 deletions ios/VydiaRNFileUploader.m
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,36 @@ - (NSString *)guessMIMETypeFromFileName: (NSString *)fileName {
return (__bridge NSString *)(MIMEType);
}

/*
Utility method to copy a PHAsset file into a local temp file, which can then be uploaded.
*/
- (void)copyAssetToFile: (NSString *)assetUrl completionHandler: (void(^)(NSString *__nullable tempFileUrl, NSError *__nullable error))completionHandler {
NSURL *url = [NSURL URLWithString:assetUrl];
PHAsset *asset = [PHAsset fetchAssetsWithALAssetURLs:@[url] options:nil].lastObject;
if (!asset) {
NSMutableDictionary* details = [NSMutableDictionary dictionary];
[details setValue:@"Asset could not be fetched. Are you missing permissions?" forKey:NSLocalizedDescriptionKey];
completionHandler(nil, [NSError errorWithDomain:@"RNUploader" code:5 userInfo:details]);
return;
}
PHAssetResource *assetResource = [[PHAssetResource assetResourcesForAsset:asset] firstObject];
NSString *pathToWrite = [NSTemporaryDirectory() stringByAppendingPathComponent:[[NSUUID UUID] UUIDString]];
NSURL *pathUrl = [NSURL fileURLWithPath:pathToWrite];
NSString *fileURI = pathUrl.absoluteString;

PHAssetResourceRequestOptions *options = [PHAssetResourceRequestOptions new];
options.networkAccessAllowed = YES;

[[PHAssetResourceManager defaultManager] writeDataForAssetResource:assetResource toFile:pathUrl options:options completionHandler:^(NSError * _Nullable e) {
if (e == nil) {
completionHandler(fileURI, nil);
}
else {
completionHandler(nil, e);
}
}];
}

/*
* Starts a file upload.
* Options are passed in as the first argument as a js hash:
Expand All @@ -115,7 +145,7 @@ - (NSString *)guessMIMETypeFromFileName: (NSString *)fileName {
}

NSString *uploadUrl = options[@"url"];
NSString *fileURI = options[@"path"];
__block NSString *fileURI = options[@"path"];
NSString *method = options[@"method"] ?: @"POST";
NSString *uploadType = options[@"type"] ?: @"raw";
NSString *fieldName = options[@"field"];
Expand All @@ -135,35 +165,21 @@ - (NSString *)guessMIMETypeFromFileName: (NSString *)fileName {
}
}];

// asset library files have to be copied over to a temp file. they cannot be uploaded directly
if ([fileURI hasPrefix:@"assets-library"]) {
NSURL *url = [NSURL URLWithString:fileURI];
PHAsset *asset = [PHAsset fetchAssetsWithALAssetURLs:@[url] options:nil].lastObject;
if (!asset) {
reject(@"RN Uploader", @"Asset could not be fetched. Are you missing permissions?", nil);
return;
}
PHAssetResource *assetResource = [[PHAssetResource assetResourcesForAsset:asset] firstObject];
NSString *pathToWrite = [NSTemporaryDirectory() stringByAppendingPathComponent:[[NSUUID UUID] UUIDString]];
NSURL *pathUrl = [NSURL fileURLWithPath:pathToWrite];
fileURI = pathUrl.absoluteString;

PHAssetResourceRequestOptions *options = [PHAssetResourceRequestOptions new];
options.networkAccessAllowed = YES;

// asset library files have to be copied over to a temp file. they can't be uploaded directly
if ([fileURI hasPrefix:@"assets-library"]) {
dispatch_group_t group = dispatch_group_create();
dispatch_group_enter(group);

__block NSError *error;
[[PHAssetResourceManager defaultManager] writeDataForAssetResource:assetResource toFile:pathUrl options:options completionHandler:^(NSError * _Nullable e) {
error = e;
[self copyAssetToFile:fileURI completionHandler:^(NSString * _Nullable tempFileUrl, NSError * _Nullable error) {
if (error) {
dispatch_group_leave(group);
reject(@"RN Uploader", @"Asset could not be copied to temp file.", nil);
return;
}
fileURI = tempFileUrl;
dispatch_group_leave(group);
}];
dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
if (error) {
reject(@"RN Uploader", @"Asset could not be copied.", nil);
return;
}
}

NSURLSessionDataTask *uploadTask;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-native-background-upload",
"version": "4.2.0",
"version": "4.3.0",
"description": "Cross platform http post file uploader with android and iOS background support",
"main": "index.js",
"scripts": {
Expand Down

0 comments on commit 709228c

Please sign in to comment.