Skip to content

Commit

Permalink
fix(ios): file copy error handling (#653)
Browse files Browse the repository at this point in the history
  • Loading branch information
vonovak authored May 16, 2023
1 parent 2c04d3e commit 991368a
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions ios/RNDocumentPicker.mm
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,10 @@ - (NSMutableDictionary *)getMetadataForUrl:(NSURL *)url error:(NSError **)error
result[FIELD_URI] = ((mode == UIDocumentPickerModeOpen) ? url : newURL).absoluteString;

NSError *copyError;
NSString *maybeFileCopyPath = copyDestination ? [RNDocumentPicker copyToUniqueDestinationFrom:newURL usingDestinationPreset:copyDestination error:copyError].absoluteString : nil;
NSURL *maybeFileCopyPath = copyDestination ? [RNDocumentPicker copyToUniqueDestinationFrom:newURL usingDestinationPreset:copyDestination error:&copyError] : nil;

if (!copyError) {
result[FIELD_FILE_COPY_URI] = RCTNullIfNil(maybeFileCopyPath);
result[FIELD_FILE_COPY_URI] = RCTNullIfNil(maybeFileCopyPath.absoluteString);
} else {
result[FIELD_COPY_ERR] = copyError.localizedDescription;
result[FIELD_FILE_COPY_URI] = [NSNull null];
Expand Down Expand Up @@ -193,21 +193,21 @@ - (void)pickDirectory:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBl
reject(@"RNDocumentPicker:pickDirectory", @"pickDirectory is not supported on iOS", nil);
}

+ (NSURL *)copyToUniqueDestinationFrom:(NSURL *)url usingDestinationPreset:(NSString *)copyToDirectory error:(NSError *)error
+ (NSURL *)copyToUniqueDestinationFrom:(NSURL *)url usingDestinationPreset:(NSString *)copyToDirectory error:(NSError **)error
{
NSURL *destinationRootDir = [self getDirectoryForFileCopy:copyToDirectory];
// we don't want to rename the file so we put it into a unique location
NSString *uniqueSubDirName = [[NSUUID UUID] UUIDString];
NSURL *destinationDir = [destinationRootDir URLByAppendingPathComponent:[NSString stringWithFormat:@"%@/", uniqueSubDirName]];
NSURL *destinationUrl = [destinationDir URLByAppendingPathComponent:[NSString stringWithFormat:@"%@", url.lastPathComponent]];

[NSFileManager.defaultManager createDirectoryAtURL:destinationDir withIntermediateDirectories:YES attributes:nil error:&error];
if (error) {
return url;
[NSFileManager.defaultManager createDirectoryAtURL:destinationDir withIntermediateDirectories:YES attributes:nil error:error];
if (*error) {
return nil;
}
[NSFileManager.defaultManager copyItemAtURL:url toURL:destinationUrl error:&error];
if (error) {
return url;
[NSFileManager.defaultManager copyItemAtURL:url toURL:destinationUrl error:error];
if (*error) {
return nil;
} else {
return destinationUrl;
}
Expand Down

0 comments on commit 991368a

Please sign in to comment.