Fix error reporting, path handling and extension naming on iOS (#90)
* `saveFile` used to return always `YES`, now it returns whether or not the save has been saved. * `generateFilePath` didn’t do any check about the path. Now it creates the in the app directory. If for some reason it fails, it throws an Exception. * `saveFile` can handle png extension and path issues. A bug on a `nil` fileSize has been fixed.
This commit is contained in:
parent
544dd9d473
commit
b92d8de42a
|
@ -29,8 +29,7 @@ bool saveImage(NSString * fullPath, UIImage * image, NSString * format, float qu
|
||||||
}
|
}
|
||||||
|
|
||||||
NSFileManager* fileManager = [NSFileManager defaultManager];
|
NSFileManager* fileManager = [NSFileManager defaultManager];
|
||||||
[fileManager createFileAtPath:fullPath contents:data attributes:nil];
|
return [fileManager createFileAtPath:fullPath contents:data attributes:nil];
|
||||||
return YES;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
NSString * generateFilePath(NSString * ext, NSString * outputPath)
|
NSString * generateFilePath(NSString * ext, NSString * outputPath)
|
||||||
|
@ -41,7 +40,15 @@ NSString * generateFilePath(NSString * ext, NSString * outputPath)
|
||||||
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
|
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
|
||||||
directory = [paths firstObject];
|
directory = [paths firstObject];
|
||||||
} else {
|
} else {
|
||||||
directory = outputPath;
|
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
|
||||||
|
NSString *documentsDirectory = [paths objectAtIndex:0];
|
||||||
|
directory = [documentsDirectory stringByAppendingPathComponent:outputPath];
|
||||||
|
NSError *error;
|
||||||
|
[[NSFileManager defaultManager] createDirectoryAtPath:directory withIntermediateDirectories:YES attributes:nil error:&error];
|
||||||
|
if (error) {
|
||||||
|
NSLog(@"Error creating documents subdirectory: %@", error);
|
||||||
|
@throw [NSException exceptionWithName:@"InvalidPathException" reason:[NSString stringWithFormat:@"Error creating documents subdirectory: %@", error] userInfo:nil];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
NSString* name = [[NSUUID UUID] UUIDString];
|
NSString* name = [[NSUUID UUID] UUIDString];
|
||||||
|
@ -94,7 +101,21 @@ RCT_EXPORT_METHOD(createResizedImage:(NSString *)path
|
||||||
callback:(RCTResponseSenderBlock)callback)
|
callback:(RCTResponseSenderBlock)callback)
|
||||||
{
|
{
|
||||||
CGSize newSize = CGSizeMake(width, height);
|
CGSize newSize = CGSizeMake(width, height);
|
||||||
NSString* fullPath = generateFilePath(@"jpg", outputPath);
|
|
||||||
|
//Set image extension
|
||||||
|
NSString *extension = @"jpg";
|
||||||
|
if ([format isEqualToString:@"PNG"]) {
|
||||||
|
extension = @"png";
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
NSString* fullPath;
|
||||||
|
@try {
|
||||||
|
fullPath = generateFilePath(extension, outputPath);
|
||||||
|
} @catch (NSException *exception) {
|
||||||
|
callback(@[@"Invalid output path.", @""]);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
[_bridge.imageLoader loadImageWithURLRequest:[RCTConvert NSURLRequest:path] callback:^(NSError *error, UIImage *image) {
|
[_bridge.imageLoader loadImageWithURLRequest:[RCTConvert NSURLRequest:path] callback:^(NSError *error, UIImage *image) {
|
||||||
if (error || image == nil) {
|
if (error || image == nil) {
|
||||||
|
@ -128,7 +149,7 @@ RCT_EXPORT_METHOD(createResizedImage:(NSString *)path
|
||||||
|
|
||||||
// Compress and save the image
|
// Compress and save the image
|
||||||
if (!saveImage(fullPath, scaledImage, format, quality)) {
|
if (!saveImage(fullPath, scaledImage, format, quality)) {
|
||||||
callback(@[@"Can't save the image. Check your compression format.", @""]);
|
callback(@[@"Can't save the image. Check your compression format and your output path", @""]);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
NSURL *fileUrl = [[NSURL alloc] initFileURLWithPath:fullPath];
|
NSURL *fileUrl = [[NSURL alloc] initFileURLWithPath:fullPath];
|
||||||
|
@ -139,7 +160,7 @@ RCT_EXPORT_METHOD(createResizedImage:(NSString *)path
|
||||||
NSDictionary *response = @{@"path": fullPath,
|
NSDictionary *response = @{@"path": fullPath,
|
||||||
@"uri": fileUrl.absoluteString,
|
@"uri": fileUrl.absoluteString,
|
||||||
@"name": fileName,
|
@"name": fileName,
|
||||||
@"size": fileSize == nil ? 0 : fileSize
|
@"size": fileSize == nil ? @(0) : fileSize
|
||||||
};
|
};
|
||||||
|
|
||||||
callback(@[[NSNull null], response]);
|
callback(@[[NSNull null], response]);
|
||||||
|
|
Loading…
Reference in New Issue