Skip to content

Commit

Permalink
Option to cleanup tmp directory
Browse files Browse the repository at this point in the history
  • Loading branch information
ivpusic committed Sep 7, 2016
1 parent 1b08b60 commit 1096f22
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 2 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,17 @@ ImagePicker.openCamera({
});
```

#### Optional cleanup
Module is creating tmp images which are going to be cleaned up automatically somewhere in the future. If you want to force cleanup, you can use `clean` to clean all tmp files, or `cleanSingle(path)` to clean single tmp file.

```javascript
ImagePicker.clean().then(() => {
console.log('removed all tmp images from tmp directory');
}).catch(e => {
alert(e);
});
```

#### Request Object

| Property | Type | Description |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,16 @@ private void setConfiguration(final ReadableMap options) {
cropping = options.hasKey("cropping") ? options.getBoolean("cropping") : cropping;
}

@ReactMethod
public void clean(final Promise promise) {
promise.resolve(null);
}

@ReactMethod
public void cleanSingle(final String path, final Promise promise) {
promise.resolve(null);
}

@ReactMethod
public void openCamera(final ReadableMap options, final Promise promise) {
int requestCode = CAMERA_PICKER_REQUEST;
Expand Down
25 changes: 25 additions & 0 deletions example/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,25 @@ export default class App extends Component {
}).catch(e => {});
}

cleanupImages() {
ImagePicker.clean().then(() => {
console.log('removed tmp images from tmp directory');
}).catch(e => {
alert(e);
});
}

cleanupSingleImage() {
let image = this.state.image || (this.state.images && this.state.images.length ? this.state.images[0] : null);
console.log('will cleanup image', image);

ImagePicker.cleanSingle(image ? image.uri : null).then(() => {
console.log(`removed tmp image ${image.uri} from tmp directory`);
}).catch(e => {
alert(e);
})
}

pickSingle(cropit) {
ImagePicker.openPicker({
width: 300,
Expand Down Expand Up @@ -117,6 +136,12 @@ export default class App extends Component {
<TouchableOpacity onPress={this.pickMultiple.bind(this)} style={styles.button}>
<Text style={styles.text}>Select Multiple</Text>
</TouchableOpacity>
<TouchableOpacity onPress={this.cleanupImages.bind(this)} style={styles.button}>
<Text style={styles.text}>Cleanup All Images</Text>
</TouchableOpacity>
<TouchableOpacity onPress={this.cleanupSingleImage.bind(this)} style={styles.button}>
<Text style={styles.text}>Cleanup Single Image</Text>
</TouchableOpacity>
</View>;
}
}
58 changes: 57 additions & 1 deletion ios/ImageCropPicker.m
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
#define ERROR_PICKER_CANCEL_KEY @"picker_cancel"
#define ERROR_PICKER_CANCEL_MSG @"User cancelled image selection"

#define ERROR_CLEANUP_ERROR_KEY @"cleanup_error"
#define ERROR_CLEANUP_ERROR_MSG @"Error while cleaning up tmp files"

#define ERROR_CANNOT_SAVE_IMAGE_KEY @"cannot_save_image"
#define ERROR_CANNOT_SAVE_IMAGE_MSG @"Cannot save image. Unable to write to tmp location."

Expand Down Expand Up @@ -113,6 +116,48 @@ - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[picker dismissViewControllerAnimated:YES completion:NULL];
}

- (NSString*) getTmpDirectory {
NSString* TMP_DIRECTORY = @"/react-native-image-crop-picker/";
return [NSTemporaryDirectory() stringByAppendingString:TMP_DIRECTORY];
}

- (BOOL)cleanTmpDirectory {
NSString* tmpDirectoryPath = [self getTmpDirectory];
NSArray* tmpDirectory = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:tmpDirectoryPath error:NULL];

for (NSString *file in tmpDirectory) {
BOOL deleted = [[NSFileManager defaultManager] removeItemAtPath:[NSString stringWithFormat:@"%@%@", tmpDirectoryPath, file] error:NULL];

if (!deleted) {
return NO;
}
}

return YES;
}

RCT_EXPORT_METHOD(cleanSingle:(NSString *) path
resolver:(RCTPromiseResolveBlock)resolve
rejecter:(RCTPromiseRejectBlock)reject) {

BOOL deleted = [[NSFileManager defaultManager] removeItemAtPath:path error:NULL];

if (!deleted) {
reject(ERROR_CLEANUP_ERROR_KEY, ERROR_CLEANUP_ERROR_MSG, nil);
} else {
resolve(nil);
}
}

RCT_REMAP_METHOD(clean, resolver:(RCTPromiseResolveBlock)resolve
rejecter:(RCTPromiseRejectBlock)reject) {
if (![self cleanTmpDirectory]) {
reject(ERROR_CLEANUP_ERROR_KEY, ERROR_CLEANUP_ERROR_MSG, nil);
} else {
resolve(nil);
}
}

RCT_EXPORT_METHOD(openPicker:(NSDictionary *)options
resolver:(RCTPromiseResolveBlock)resolve
rejecter:(RCTPromiseRejectBlock)reject) {
Expand Down Expand Up @@ -158,6 +203,7 @@ - (void)qb_imagePickerController:
NSString *filePath = [self persistFile:data];
if (filePath == nil) {
self.reject(ERROR_CANNOT_SAVE_IMAGE_KEY, ERROR_CANNOT_SAVE_IMAGE_MSG, nil);
[imagePickerController dismissViewControllerAnimated:YES completion:nil];
return;
}

Expand Down Expand Up @@ -212,6 +258,7 @@ - (void) processSingleImagePick:(UIImage*)image withViewController:(UIViewContro
NSString *filePath = [self persistFile:data];
if (filePath == nil) {
self.reject(ERROR_CANNOT_SAVE_IMAGE_KEY, ERROR_CANNOT_SAVE_IMAGE_MSG, nil);
[viewController dismissViewControllerAnimated:YES completion:nil];
return;
}

Expand Down Expand Up @@ -301,6 +348,7 @@ - (void)imageCropViewController:(RSKImageCropViewController *)controller
NSString *filePath = [self persistFile:data];
if (filePath == nil) {
self.reject(ERROR_CANNOT_SAVE_IMAGE_KEY, ERROR_CANNOT_SAVE_IMAGE_MSG, nil);
[controller dismissViewControllerAnimated:YES completion:nil];
return;
}

Expand All @@ -319,8 +367,16 @@ - (void)imageCropViewController:(RSKImageCropViewController *)controller
// at the moment it is not possible to upload image by reading PHAsset
// we are saving image and saving it to the tmp location where we are allowed to access image later
- (NSString*) persistFile:(NSData*)data {
// create tmp directory
NSString *tmpDirFullPath = [self getTmpDirectory];
BOOL dirCreated = [[NSFileManager defaultManager] createDirectoryAtPath: tmpDirFullPath
withIntermediateDirectories:YES attributes:nil error:nil];
if (!dirCreated) {
return nil;
}

// create temp file
NSString *filePath = [NSTemporaryDirectory() stringByAppendingString:[[NSUUID UUID] UUIDString]];
NSString *filePath = [tmpDirFullPath stringByAppendingString:[[NSUUID UUID] UUIDString]];
filePath = [filePath stringByAppendingString:@".jpg"];

// save cropped file
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-image-crop-picker",
"version": "0.8.1",
"version": "0.8.2",
"description": "Select single or multiple images, with croping option",
"main": "index.js",
"scripts": {
Expand Down

0 comments on commit 1096f22

Please sign in to comment.