@@ -95,6 +95,36 @@ - (NSString *)guessMIMETypeFromFileName: (NSString *)fileName {
9595 return (__bridge NSString *)(MIMEType);
9696}
9797
98+ /*
99+ Utility method to copy a PHAsset file into a local temp file, which can then be uploaded.
100+ */
101+ - (void )copyAssetToFile : (NSString *)assetUrl completionHandler : (void (^)(NSString *__nullable tempFileUrl, NSError *__nullable error))completionHandler {
102+ NSURL *url = [NSURL URLWithString: assetUrl];
103+ PHAsset *asset = [PHAsset fetchAssetsWithALAssetURLs: @[url] options: nil ].lastObject ;
104+ if (!asset) {
105+ NSMutableDictionary * details = [NSMutableDictionary dictionary ];
106+ [details setValue: @" Asset could not be fetched. Are you missing permissions?" forKey: NSLocalizedDescriptionKey ];
107+ completionHandler (nil , [NSError errorWithDomain: @" RNUploader" code: 5 userInfo: details]);
108+ return ;
109+ }
110+ PHAssetResource *assetResource = [[PHAssetResource assetResourcesForAsset: asset] firstObject ];
111+ NSString *pathToWrite = [NSTemporaryDirectory () stringByAppendingPathComponent: [[NSUUID UUID ] UUIDString ]];
112+ NSURL *pathUrl = [NSURL fileURLWithPath: pathToWrite];
113+ NSString *fileURI = pathUrl.absoluteString ;
114+
115+ PHAssetResourceRequestOptions *options = [PHAssetResourceRequestOptions new ];
116+ options.networkAccessAllowed = YES ;
117+
118+ [[PHAssetResourceManager defaultManager ] writeDataForAssetResource: assetResource toFile: pathUrl options: options completionHandler: ^(NSError * _Nullable e) {
119+ if (e == nil ) {
120+ completionHandler (fileURI, nil );
121+ }
122+ else {
123+ completionHandler (nil , e);
124+ }
125+ }];
126+ }
127+
98128/*
99129 * Starts a file upload.
100130 * Options are passed in as the first argument as a js hash:
@@ -115,7 +145,7 @@ - (NSString *)guessMIMETypeFromFileName: (NSString *)fileName {
115145 }
116146
117147 NSString *uploadUrl = options[@" url" ];
118- NSString *fileURI = options[@" path" ];
148+ __block NSString *fileURI = options[@" path" ];
119149 NSString *method = options[@" method" ] ?: @" POST" ;
120150 NSString *uploadType = options[@" type" ] ?: @" raw" ;
121151 NSString *fieldName = options[@" field" ];
@@ -135,35 +165,21 @@ - (NSString *)guessMIMETypeFromFileName: (NSString *)fileName {
135165 }
136166 }];
137167
138- // asset library files have to be copied over to a temp file. they cannot be uploaded directly
139- if ([fileURI hasPrefix: @" assets-library" ]) {
140- NSURL *url = [NSURL URLWithString: fileURI];
141- PHAsset *asset = [PHAsset fetchAssetsWithALAssetURLs: @[url] options: nil ].lastObject ;
142- if (!asset) {
143- reject (@" RN Uploader" , @" Asset could not be fetched. Are you missing permissions?" , nil );
144- return ;
145- }
146- PHAssetResource *assetResource = [[PHAssetResource assetResourcesForAsset: asset] firstObject ];
147- NSString *pathToWrite = [NSTemporaryDirectory () stringByAppendingPathComponent: [[NSUUID UUID ] UUIDString ]];
148- NSURL *pathUrl = [NSURL fileURLWithPath: pathToWrite];
149- fileURI = pathUrl.absoluteString ;
150-
151- PHAssetResourceRequestOptions *options = [PHAssetResourceRequestOptions new ];
152- options.networkAccessAllowed = YES ;
153168
169+ // asset library files have to be copied over to a temp file. they can't be uploaded directly
170+ if ([fileURI hasPrefix: @" assets-library" ]) {
154171 dispatch_group_t group = dispatch_group_create ();
155172 dispatch_group_enter (group);
156-
157- __block NSError *error;
158- [[PHAssetResourceManager defaultManager ] writeDataForAssetResource: assetResource toFile: pathUrl options: options completionHandler: ^(NSError * _Nullable e) {
159- error = e;
173+ [self copyAssetToFile: fileURI completionHandler: ^(NSString * _Nullable tempFileUrl, NSError * _Nullable error) {
174+ if (error) {
175+ dispatch_group_leave (group);
176+ reject (@" RN Uploader" , @" Asset could not be copied to temp file." , nil );
177+ return ;
178+ }
179+ fileURI = tempFileUrl;
160180 dispatch_group_leave (group);
161181 }];
162182 dispatch_group_wait (group, DISPATCH_TIME_FOREVER);
163- if (error) {
164- reject (@" RN Uploader" , @" Asset could not be copied." , nil );
165- return ;
166- }
167183 }
168184
169185 NSURLSessionDataTask *uploadTask;
0 commit comments