Skip to content
This repository was archived by the owner on Jun 16, 2023. It is now read-only.

Commit 2be9213

Browse files
fix: handle file writing errors (#3268)
Co-authored-by: Cristiano Coelho <cristianocca@hotmail.com>
1 parent 7108d69 commit 2be9213

File tree

3 files changed

+56
-30
lines changed

3 files changed

+56
-30
lines changed

ios/RN/RNCamera.m

+32-19
Original file line numberDiff line numberDiff line change
@@ -949,33 +949,46 @@ - (void)takePicture:(NSDictionary *)options resolve:(RCTPromiseResolveBlock)reso
949949
path = [RNFileSystem generatePathInDirectory:[[RNFileSystem cacheDirectoryPath] stringByAppendingPathComponent:@"Camera"] withExtension:imageExtension];
950950
}
951951

952+
bool success = YES;
953+
952954
if (![options[@"doNotSave"] boolValue]) {
953-
response[@"uri"] = [RNImageUtils writeImage:destData toPath:path];
955+
NSString* pathRes = [RNImageUtils writeImage:destData toPath:path];
956+
if (!pathRes) {
957+
reject(@"E_IMAGE_CAPTURE_FAILED", @"Image could not be saved: file write failed.", nil);
958+
success = NO;
959+
} else {
960+
response[@"uri"] = pathRes;
961+
}
962+
954963
}
955-
response[@"width"] = @(takenImage.size.width);
956-
response[@"height"] = @(takenImage.size.height);
964+
965+
if (success) {
966+
response[@"width"] = @(takenImage.size.width);
967+
response[@"height"] = @(takenImage.size.height);
957968

958-
if ([options[@"base64"] boolValue]) {
959-
response[@"base64"] = [destData base64EncodedStringWithOptions:0];
960-
}
969+
if ([options[@"base64"] boolValue]) {
970+
response[@"base64"] = [destData base64EncodedStringWithOptions:0];
971+
}
961972

962-
if ([options[@"exif"] boolValue]) {
963-
response[@"exif"] = metadata;
973+
if ([options[@"exif"] boolValue]) {
974+
response[@"exif"] = metadata;
964975

965-
// No longer needed since we always get the photo metadata now
966-
//[RNImageUtils updatePhotoMetadata:imageSampleBuffer withAdditionalData:@{ @"Orientation": @(imageRotation) } inResponse:response]; // TODO
967-
}
976+
// No longer needed since we always get the photo metadata now
977+
//[RNImageUtils updatePhotoMetadata:imageSampleBuffer withAdditionalData:@{ @"Orientation": @(imageRotation) } inResponse:response]; // TODO
978+
}
968979

969-
response[@"pictureOrientation"] = @([self.orientation integerValue]);
970-
response[@"deviceOrientation"] = @([self.deviceOrientation integerValue]);
980+
response[@"pictureOrientation"] = @([self.orientation integerValue]);
981+
response[@"deviceOrientation"] = @([self.deviceOrientation integerValue]);
982+
983+
if (useFastMode) {
984+
[self onPictureSaved:@{@"data": response, @"id": options[@"id"]}];
985+
} else {
986+
resolve(response);
987+
}
988+
}
989+
971990
self.orientation = nil;
972991
self.deviceOrientation = nil;
973-
974-
if (useFastMode) {
975-
[self onPictureSaved:@{@"data": response, @"id": options[@"id"]}];
976-
} else {
977-
resolve(response);
978-
}
979992
}
980993
else{
981994
reject(@"E_IMAGE_CAPTURE_FAILED", @"Image could not be saved", error);

ios/RN/RNCameraManager.m

+21-10
Original file line numberDiff line numberDiff line change
@@ -391,20 +391,31 @@ + (NSDictionary *)barcodeDetectorConstants
391391
}
392392

393393
[view onPictureTaken:@{}];
394+
395+
bool success = YES;
394396

395397
NSData *photoData = UIImageJPEGRepresentation(generatedPhoto, quality);
396398
if (![options[@"doNotSave"] boolValue]) {
397-
response[@"uri"] = [RNImageUtils writeImage:photoData toPath:path];
398-
}
399-
response[@"width"] = @(generatedPhoto.size.width);
400-
response[@"height"] = @(generatedPhoto.size.height);
401-
if ([options[@"base64"] boolValue]) {
402-
response[@"base64"] = [photoData base64EncodedStringWithOptions:0];
399+
NSString* pathRes = [RNImageUtils writeImage:photoData toPath:path];
400+
if (!pathRes) {
401+
reject(@"E_IMAGE_CAPTURE_FAILED", @"Image could not be saved: file write failed.", nil);
402+
success = NO;
403+
} else {
404+
response[@"uri"] = pathRes;
405+
}
403406
}
404-
if (useFastMode) {
405-
[view onPictureSaved:@{@"data": response, @"id": options[@"id"]}];
406-
} else {
407-
resolve(response);
407+
408+
if (success) {
409+
response[@"width"] = @(generatedPhoto.size.width);
410+
response[@"height"] = @(generatedPhoto.size.height);
411+
if ([options[@"base64"] boolValue]) {
412+
response[@"base64"] = [photoData base64EncodedStringWithOptions:0];
413+
}
414+
if (useFastMode) {
415+
[view onPictureSaved:@{@"data": response, @"id": options[@"id"]}];
416+
} else {
417+
resolve(response);
418+
}
408419
}
409420
#else
410421
[view takePicture:options resolve:resolve reject:reject];

ios/RN/RNImageUtils.m

+3-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,9 @@ + (UIImage *)mirrorImage:(UIImage *)image
6363

6464
+ (NSString *)writeImage:(NSData *)image toPath:(NSString *)path
6565
{
66-
[image writeToFile:path atomically:YES];
66+
if (![image writeToFile:path atomically:YES]) {
67+
return nil;
68+
}
6769
NSURL *fileURL = [NSURL fileURLWithPath:path];
6870
return [fileURL absoluteString];
6971
}

0 commit comments

Comments
 (0)