Skip to content

Commit 01e8975

Browse files
committed
added support for Video-Assets on iOS (copyAssetsVideoIOS) and setReadable() on Android
1 parent 98b108d commit 01e8975

File tree

5 files changed

+99
-4
lines changed

5 files changed

+99
-4
lines changed

FS.common.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,13 @@ var RNFS = {
241241
});
242242
},
243243

244+
// setReadable for Android
245+
setReadable(filepath : string, readable: boolean, ownerOnly: boolean) : Promise<boolean> {
246+
return RNFSManager.setReadable(filepath, readable, ownerOnly).then( (result) => {
247+
return result;
248+
})
249+
},
250+
244251
stat(filepath: string): Promise<StatResult> {
245252
return RNFSManager.stat(normalizeFilePath(filepath)).then((result) => {
246253
return {
@@ -287,6 +294,14 @@ var RNFS = {
287294
return RNFSManager.copyAssetsFileIOS(imageUri, destPath, width, height, scale, compression, resizeMode );
288295
},
289296

297+
// iOS only
298+
// Copies fotos from asset-library (camera-roll) to a specific location
299+
// with a given width or height
300+
// @see: https://developer.apple.com/reference/photos/phimagemanager/1616964-requestimageforasset
301+
copyAssetsVideoIOS(imageUri: string, destPath: string): Promise<string> {
302+
return RNFSManager.copyAssetsVideoIOS(imageUri, destPath);
303+
},
304+
290305
writeFile(filepath: string, contents: string, encodingOrOptions?: any): Promise<void> {
291306
var b64;
292307

README.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -400,14 +400,28 @@ Note: Android only. Will overwrite destPath if it already exists
400400

401401
### `copyAssetsFileIOS(imageUri: string, destPath: string, width: number, height: number, scale : number = 1.0, compression : number = 1.0, resizeMode : string = 'contain' ): Promise<string>`
402402

403-
iOS-ony: copies a file from camera-roll, that is prefixed with "assets-library://asset/asset.JPG?..."
403+
iOS-only: copies a file from camera-roll, that is prefixed with "assets-library://asset/asset.JPG?..."
404404
to a specific destination. It will download the original from iCloud if necessary.
405+
405406
If width and height is > 0, the image will be resized to a specific size and a specific compression rate.
406407
If scale is below 1, the image will be scaled according to the scale-factor (between 0.0 and 1.0)
407408
The resizeMode is also considered.
409+
410+
*Video-Support:*
411+
412+
One can use this method also to create a thumbNail from a video in a specific size.
413+
Currently it is impossible to specify a concrete position, the OS will decide wich
414+
Thumbnail you'll get then.
415+
To copy a video from assets-library and save it as a mp4-file, refer to copyAssetsVideoIOS.
416+
408417
Further information: https://developer.apple.com/reference/photos/phimagemanager/1616964-requestimageforasset
409418
The promise will on success return the final destination of the file, as it was defined in the destPath-parameter.
410419

420+
### copyAssetsVideoIOS(videoUri: string, destPath: string)
421+
422+
iOS-only: copies a video from assets-library, that is prefixed with 'assets-library://asset/asset.MOV?...'
423+
to a specific destination.
424+
411425
### `unlink(filepath: string): Promise<void>`
412426

413427
Unlinks the item at `filepath`. If the item does not exist, an error will be thrown.

RNFSManager.m

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -556,7 +556,15 @@ - (dispatch_queue_t)methodQueue
556556

557557
/**
558558
* iOS Only: copy images from the assets-library (camera-roll) to a specific path, asuming
559-
* JPEG-Images. Video-Support will follow up, not implemented yet.
559+
* JPEG-Images.
560+
*
561+
* Video-Support:
562+
*
563+
* One can use this method also to create a thumbNail from a video.
564+
* Currently it is impossible to specify a concrete position, the OS will decide wich
565+
* Thumbnail you'll get then.
566+
* To copy a video from assets-library and save it as a mp4-file, use the method
567+
* copyAssetsVideoIOS.
560568
*
561569
* It is also supported to scale the image via scale-factor (0.0-1.0) or with a specific
562570
* width and height. Also the resizeMode will be considered.
@@ -633,10 +641,52 @@ - (dispatch_queue_t)methodQueue
633641

634642
}
635643
}];
644+
}
645+
646+
/**
647+
* iOS Only: copy videos from the assets-library (camera-roll) to a specific path as mp4-file.
648+
*
649+
* To create a thumbnail from the video, refer to copyAssetsFileIOS
650+
*/
651+
RCT_EXPORT_METHOD(copyAssetsVideoIOS: (NSString *) imageUri
652+
atFilepath: (NSString *) destination
653+
resolver: (RCTPromiseResolveBlock) resolve
654+
rejecter: (RCTPromiseRejectBlock) reject)
655+
656+
{
657+
NSURL* url = [NSURL URLWithString:imageUri];
658+
__block NSURL* videoURL = [NSURL URLWithString:destination];
659+
660+
PHFetchResult *phAssetFetchResult = [PHAsset fetchAssetsWithALAssetURLs:@[url] options:nil];
661+
PHAsset *phAsset = [phAssetFetchResult firstObject];
662+
dispatch_group_t group = dispatch_group_create();
663+
dispatch_group_enter(group);
664+
665+
[[PHImageManager defaultManager] requestAVAssetForVideo:phAsset options:nil resultHandler:^(AVAsset *asset, AVAudioMix *audioMix, NSDictionary *info) {
636666

637-
667+
if ([asset isKindOfClass:[AVURLAsset class]]) {
668+
NSURL *url = [(AVURLAsset *)asset URL];
669+
NSLog(@"Final URL %@",url);
670+
NSData *videoData = [NSData dataWithContentsOfURL:url];
671+
672+
BOOL writeResult = [videoData writeToFile:destination atomically:true];
673+
674+
if(writeResult) {
675+
NSLog(@"video success");
676+
}
677+
else {
678+
NSLog(@"video failure");
679+
}
680+
dispatch_group_leave(group);
681+
// use URL to get file content
682+
}
683+
}];
684+
dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
685+
resolve(destination);
638686
}
639687

688+
689+
640690
- (NSNumber *)dateToTimeIntervalNumber:(NSDate *)date
641691
{
642692
return @([date timeIntervalSince1970]);

android/src/main/java/com/rnfs/RNFSManager.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,22 @@ private void copyInputStream(InputStream in, String source, String destination,
431431
}
432432
}
433433

434+
@ReactMethod
435+
public void setReadable(String filepath, Boolean readable, Boolean ownerOnly, Promise promise) {
436+
try {
437+
File file = new File(filepath);
438+
439+
if (!file.exists()) throw new Exception("File does not exist");
440+
441+
file.setReadable(readable, ownerOnly);
442+
443+
promise.resolve(true);
444+
} catch (Exception ex) {
445+
ex.printStackTrace();
446+
reject(promise, filepath, ex);
447+
}
448+
}
449+
434450
@ReactMethod
435451
public void stat(String filepath, Promise promise) {
436452
try {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-native-fs",
3-
"version": "2.3.2",
3+
"version": "2.6.0",
44
"description": "Native filesystem access for react-native",
55
"main": "FS.common.js",
66
"scripts": {

0 commit comments

Comments
 (0)