react-native-compressor 1.3.4 → 1.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (37) hide show
  1. package/README.md +52 -1
  2. package/android/.gradle/6.1.1/fileHashes/fileHashes.lock +0 -0
  3. package/android/.gradle/buildOutputCleanup/buildOutputCleanup.lock +0 -0
  4. package/android/.gradle/buildOutputCleanup/cache.properties +2 -2
  5. package/android/.gradle/checksums/checksums.lock +0 -0
  6. package/android/.gradle/checksums/sha1-checksums.bin +0 -0
  7. package/android/build.gradle +1 -1
  8. package/android/src/main/java/com/.DS_Store +0 -0
  9. package/android/src/main/java/com/reactnativecompressor/.DS_Store +0 -0
  10. package/android/src/main/java/com/reactnativecompressor/CompressorModule.java +46 -1
  11. package/android/src/main/java/com/reactnativecompressor/Utils/RealPathUtil.java +210 -0
  12. package/android/src/main/java/com/reactnativecompressor/Utils/Utils.java +26 -1
  13. package/android/src/main/java/com/reactnativecompressor/Video/VideoModule.java +6 -0
  14. package/ios/.DS_Store +0 -0
  15. package/ios/Compressor-Bridging-Header.h +1 -1
  16. package/ios/Compressor.m +118 -12
  17. package/ios/Image/ImageCompressor.h +2 -0
  18. package/ios/Image/ImageCompressor.m +153 -1
  19. package/ios/Video/VideoCompressor.swift +34 -34
  20. package/lib/commonjs/Video/index.js +1 -1
  21. package/lib/commonjs/Video/index.js.map +1 -1
  22. package/lib/commonjs/index.js +22 -1
  23. package/lib/commonjs/index.js.map +1 -1
  24. package/lib/commonjs/utils/index.js +18 -4
  25. package/lib/commonjs/utils/index.js.map +1 -1
  26. package/lib/module/Video/index.js +1 -1
  27. package/lib/module/Video/index.js.map +1 -1
  28. package/lib/module/index.js +6 -3
  29. package/lib/module/index.js.map +1 -1
  30. package/lib/module/utils/index.js +9 -4
  31. package/lib/module/utils/index.js.map +1 -1
  32. package/lib/typescript/index.d.ts +5 -2
  33. package/lib/typescript/utils/index.d.ts +3 -0
  34. package/package.json +1 -1
  35. package/src/Video/index.tsx +1 -1
  36. package/src/index.tsx +13 -1
  37. package/src/utils/index.tsx +14 -3
package/README.md CHANGED
@@ -33,7 +33,7 @@
33
33
  #### For React Native<0.65
34
34
 
35
35
  ```sh
36
- yarn add react-native-compressor@0.5.15
36
+ yarn add react-native-compressor@rnlessthan65
37
37
  ```
38
38
 
39
39
  #### For React Native 0.65 or greater
@@ -334,6 +334,57 @@ type FileSystemUploadOptions = (
334
334
  };
335
335
  ```
336
336
 
337
+ ### Get Metadata Of Video
338
+
339
+ if you want to get metadata of video than you can use this function
340
+
341
+ ```js
342
+ import { getVideoMetaData } from 'react-native-compressor';
343
+
344
+ const metaData = await getVideoMetaData(filePath);
345
+ ```
346
+
347
+ ```
348
+ {
349
+ "duration": "6",
350
+ "extension": "mp4",
351
+ "height": "1080",
352
+ "size": "16940.0",
353
+ "width": "1920"
354
+ }
355
+ ```
356
+
357
+ - ###### `getVideoMetaData(path: string)`
358
+
359
+ ### Get Real Path
360
+
361
+ if you want to convert
362
+
363
+ - `content://` to `file:///` for android
364
+ - `ph://` to `file:///` for IOS
365
+
366
+ the you can you `getRealPath` function like this
367
+
368
+ ```js
369
+ import { getRealPath } from 'react-native-compressor';
370
+
371
+ const realPath = await getRealPath(fileUri, 'video'); // file://file_path.extension
372
+ ```
373
+
374
+ - ###### `getRealPath(path: string, type: string = 'video'|'image')`
375
+
376
+ ### Get Temp file Path
377
+
378
+ if you wanna make random file path in cache folder then you can use this method like this
379
+
380
+ ```js
381
+ import { generateFilePath } from 'react-native-compressor';
382
+
383
+ const randomFilePathForSaveFile = await generateFilePath('mp4'); // file://file_path.mp4
384
+ ```
385
+
386
+ - ##### `generateFilePath(fileextension: string)`
387
+
337
388
  ## Contributing
338
389
 
339
390
  See the [contributing guide](CONTRIBUTING.md) to learn how to contribute to the repository and the development workflow.
@@ -1,2 +1,2 @@
1
- #Tue Jan 04 23:36:59 PKT 2022
2
- gradle.version=6.1.1
1
+ #Thu Jan 27 18:54:59 PKT 2022
2
+ gradle.version=6.9
@@ -57,6 +57,6 @@ dependencies {
57
57
  //noinspection GradleDynamicVersion
58
58
  implementation "com.facebook.react:react-native:+" // From node_modules
59
59
  implementation 'io.github.lizhangqu:coreprogress:1.0.2'
60
- implementation 'com.github.nomi9995:VideoCompressor:90adcba85a'
60
+ implementation 'com.github.nomi9995:VideoCompressor:715bcc16e9'
61
61
  // implementation project(path: ':videocompressor')
62
62
  }
Binary file
@@ -9,6 +9,7 @@ import androidx.annotation.NonNull;
9
9
  import androidx.annotation.Nullable;
10
10
  import androidx.annotation.RequiresApi;
11
11
 
12
+ import com.facebook.react.bridge.Arguments;
12
13
  import com.facebook.react.bridge.Promise;
13
14
  import com.facebook.react.bridge.ReactApplicationContext;
14
15
  import com.facebook.react.bridge.ReactContext;
@@ -20,10 +21,15 @@ import com.facebook.react.module.annotations.ReactModule;
20
21
  import com.facebook.react.modules.core.DeviceEventManagerModule;
21
22
  import com.reactnativecompressor.Image.ImageCompressor;
22
23
  import com.reactnativecompressor.Image.utils.ImageCompressorOptions;
24
+ import com.reactnativecompressor.Utils.Utils;
23
25
  import com.reactnativecompressor.Video.VideoCompressorHelper;
24
26
  import static com.reactnativecompressor.Utils.Utils.generateCacheFilePath;
27
+ import static com.reactnativecompressor.Utils.Utils.getRealPath;
28
+
25
29
  import com.reactnativecompressor.Audio.AudioCompressor;
26
30
 
31
+ import java.io.File;
32
+
27
33
  @ReactModule(name = CompressorModule.NAME)
28
34
  public class CompressorModule extends ReactContextBaseJavaModule {
29
35
  public static final String NAME = "Compressor";
@@ -54,6 +60,7 @@ public class CompressorModule extends ReactContextBaseJavaModule {
54
60
  ReadableMap optionMap,
55
61
  Promise promise) {
56
62
  try {
63
+ imagePath=Utils.getRealPath(imagePath,reactContext);
57
64
  final ImageCompressorOptions options = ImageCompressorOptions.fromMap(optionMap);
58
65
 
59
66
  if(options.compressionMethod==ImageCompressorOptions.CompressionMethod.auto)
@@ -98,7 +105,7 @@ public class CompressorModule extends ReactContextBaseJavaModule {
98
105
 
99
106
  //General
100
107
  @ReactMethod
101
- public void generateFile(String extension, Promise promise) {
108
+ public void generateFilePath(String extension, Promise promise) {
102
109
  try {
103
110
  final String outputUri =generateCacheFilePath(extension,reactContext);
104
111
  promise.resolve(outputUri);
@@ -106,4 +113,42 @@ public class CompressorModule extends ReactContextBaseJavaModule {
106
113
  promise.reject(e);
107
114
  }
108
115
  }
116
+
117
+ @ReactMethod
118
+ public void getRealPath(String path,String type, Promise promise) {
119
+ try {
120
+ final String realPath =Utils.getRealPath(path,reactContext);
121
+ promise.resolve("file://"+realPath);
122
+ } catch (Exception e) {
123
+ promise.reject(e);
124
+ }
125
+ }
126
+
127
+ @ReactMethod
128
+ public void getVideoMetaData(String filePath, Promise promise) {
129
+ try {
130
+ filePath=Utils.getRealPath(filePath,reactContext);
131
+ Uri uri= Uri.parse(filePath);
132
+ String srcPath = uri.getPath();
133
+ MediaMetadataRetriever metaRetriever = new MediaMetadataRetriever();
134
+ metaRetriever.setDataSource(srcPath);
135
+ File file=new File(srcPath);
136
+ float sizeInKBs = file.length()/1024;
137
+ int actualHeight =Integer.parseInt(metaRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT));
138
+ int actualWidth = Integer.parseInt(metaRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH));
139
+ long duration = Long.parseLong(metaRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION));
140
+ String extension = filePath.substring(filePath.lastIndexOf(".")+1);
141
+
142
+ WritableMap params = Arguments.createMap();
143
+ params.putString("size", String.valueOf(sizeInKBs));
144
+ params.putString("width", String.valueOf(actualWidth));
145
+ params.putString("height", String.valueOf(actualHeight));
146
+ params.putString("duration", String.valueOf(duration/1000));
147
+ params.putString("extension", extension);
148
+
149
+ promise.resolve(params);
150
+ } catch (Exception e) {
151
+ promise.reject(e);
152
+ }
153
+ }
109
154
  }
@@ -0,0 +1,210 @@
1
+ package com.reactnativecompressor.Utils;
2
+
3
+ import android.annotation.SuppressLint;
4
+ import android.content.ContentUris;
5
+ import android.content.Context;
6
+ import android.database.Cursor;
7
+ import android.net.Uri;
8
+ import android.os.Build;
9
+ import android.os.Environment;
10
+ import android.provider.DocumentsContract;
11
+ import android.provider.MediaStore;
12
+
13
+ import androidx.loader.content.CursorLoader;
14
+
15
+ public class RealPathUtil {
16
+
17
+ public static String getRealPath(Context context, Uri fileUri) {
18
+ String realPath;
19
+ // SDK < API11
20
+ if (Build.VERSION.SDK_INT < 11) {
21
+ realPath = RealPathUtil.getRealPathFromURI_BelowAPI11(context, fileUri);
22
+ }
23
+ // SDK >= 11 && SDK < 19
24
+ else if (Build.VERSION.SDK_INT < 19) {
25
+ realPath = RealPathUtil.getRealPathFromURI_API11to18(context, fileUri);
26
+ }
27
+ // SDK > 19 (Android 4.4) and up
28
+ else {
29
+ realPath = RealPathUtil.getRealPathFromURI_API19(context, fileUri);
30
+ }
31
+ return realPath;
32
+ }
33
+
34
+
35
+ @SuppressLint("NewApi")
36
+ public static String getRealPathFromURI_API11to18(Context context, Uri contentUri) {
37
+ String[] proj = {MediaStore.Images.Media.DATA};
38
+ String result = null;
39
+
40
+ CursorLoader cursorLoader = new CursorLoader(context, contentUri, proj, null, null, null);
41
+ Cursor cursor = cursorLoader.loadInBackground();
42
+
43
+ if (cursor != null) {
44
+ int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
45
+ cursor.moveToFirst();
46
+ result = cursor.getString(column_index);
47
+ cursor.close();
48
+ }
49
+ return result;
50
+ }
51
+
52
+ public static String getRealPathFromURI_BelowAPI11(Context context, Uri contentUri) {
53
+ String[] proj = {MediaStore.Images.Media.DATA};
54
+ Cursor cursor = context.getContentResolver().query(contentUri, proj, null, null, null);
55
+ int column_index = 0;
56
+ String result = "";
57
+ if (cursor != null) {
58
+ column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
59
+ cursor.moveToFirst();
60
+ result = cursor.getString(column_index);
61
+ cursor.close();
62
+ return result;
63
+ }
64
+ return result;
65
+ }
66
+
67
+ /**
68
+ * Get a file path from a Uri. This will get the the path for Storage Access
69
+ * Framework Documents, as well as the _data field for the MediaStore and
70
+ * other file-based ContentProviders.
71
+ *
72
+ * @param context The context.
73
+ * @param uri The Uri to query.
74
+ * @author paulburke
75
+ */
76
+ @SuppressLint("NewApi")
77
+ public static String getRealPathFromURI_API19(final Context context, final Uri uri) {
78
+
79
+ final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
80
+
81
+ // DocumentProvider
82
+ if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) {
83
+ // ExternalStorageProvider
84
+ if (isExternalStorageDocument(uri)) {
85
+ final String docId = DocumentsContract.getDocumentId(uri);
86
+ final String[] split = docId.split(":");
87
+ final String type = split[0];
88
+
89
+ if ("primary".equalsIgnoreCase(type)) {
90
+ return Environment.getExternalStorageDirectory() + "/" + split[1];
91
+ }
92
+
93
+ // TODO handle non-primary volumes
94
+ }
95
+ // DownloadsProvider
96
+ else if (isDownloadsDocument(uri)) {
97
+
98
+ final String id = DocumentsContract.getDocumentId(uri);
99
+ final Uri contentUri = ContentUris.withAppendedId(
100
+ Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));
101
+
102
+ return getDataColumn(context, contentUri, null, null);
103
+ }
104
+ // MediaProvider
105
+ else if (isMediaDocument(uri)) {
106
+ final String docId = DocumentsContract.getDocumentId(uri);
107
+ final String[] split = docId.split(":");
108
+ final String type = split[0];
109
+
110
+ Uri contentUri = null;
111
+ if ("image".equals(type)) {
112
+ contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
113
+ } else if ("video".equals(type)) {
114
+ contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
115
+ } else if ("audio".equals(type)) {
116
+ contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
117
+ }
118
+
119
+ final String selection = "_id=?";
120
+ final String[] selectionArgs = new String[]{
121
+ split[1]
122
+ };
123
+
124
+ return getDataColumn(context, contentUri, selection, selectionArgs);
125
+ }
126
+ }
127
+ // MediaStore (and general)
128
+ else if ("content".equalsIgnoreCase(uri.getScheme())) {
129
+
130
+ // Return the remote address
131
+ if (isGooglePhotosUri(uri))
132
+ return uri.getLastPathSegment();
133
+
134
+ return getDataColumn(context, uri, null, null);
135
+ }
136
+ // File
137
+ else if ("file".equalsIgnoreCase(uri.getScheme())) {
138
+ return uri.getPath();
139
+ }
140
+
141
+ return null;
142
+ }
143
+
144
+ /**
145
+ * Get the value of the data column for this Uri. This is useful for
146
+ * MediaStore Uris, and other file-based ContentProviders.
147
+ *
148
+ * @param context The context.
149
+ * @param uri The Uri to query.
150
+ * @param selection (Optional) Filter used in the query.
151
+ * @param selectionArgs (Optional) Selection arguments used in the query.
152
+ * @return The value of the _data column, which is typically a file path.
153
+ */
154
+ public static String getDataColumn(Context context, Uri uri, String selection,
155
+ String[] selectionArgs) {
156
+
157
+ Cursor cursor = null;
158
+ final String column = "_data";
159
+ final String[] projection = {
160
+ column
161
+ };
162
+
163
+ try {
164
+ cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs,
165
+ null);
166
+ if (cursor != null && cursor.moveToFirst()) {
167
+ final int index = cursor.getColumnIndexOrThrow(column);
168
+ return cursor.getString(index);
169
+ }
170
+ } finally {
171
+ if (cursor != null)
172
+ cursor.close();
173
+ }
174
+ return null;
175
+ }
176
+
177
+
178
+ /**
179
+ * @param uri The Uri to check.
180
+ * @return Whether the Uri authority is ExternalStorageProvider.
181
+ */
182
+ public static boolean isExternalStorageDocument(Uri uri) {
183
+ return "com.android.externalstorage.documents".equals(uri.getAuthority());
184
+ }
185
+
186
+ /**
187
+ * @param uri The Uri to check.
188
+ * @return Whether the Uri authority is DownloadsProvider.
189
+ */
190
+ public static boolean isDownloadsDocument(Uri uri) {
191
+ return "com.android.providers.downloads.documents".equals(uri.getAuthority());
192
+ }
193
+
194
+ /**
195
+ * @param uri The Uri to check.
196
+ * @return Whether the Uri authority is MediaProvider.
197
+ */
198
+ public static boolean isMediaDocument(Uri uri) {
199
+ return "com.android.providers.media.documents".equals(uri.getAuthority());
200
+ }
201
+
202
+ /**
203
+ * @param uri The Uri to check.
204
+ * @return Whether the Uri authority is Google Photos.
205
+ */
206
+ public static boolean isGooglePhotosUri(Uri uri) {
207
+ return "com.google.android.apps.photos.content".equals(uri.getAuthority());
208
+ }
209
+
210
+ }
@@ -1,5 +1,8 @@
1
1
  package com.reactnativecompressor.Utils;
2
2
 
3
+ import android.net.Uri;
4
+ import android.util.Log;
5
+
3
6
  import androidx.annotation.Nullable;
4
7
 
5
8
  import com.facebook.react.bridge.Arguments;
@@ -17,6 +20,7 @@ import java.util.UUID;
17
20
 
18
21
  public class Utils {
19
22
  static int videoCompressionThreshold=10;
23
+ private static final String TAG = "react-native-compessor";
20
24
  static Map<String, VideoCompressTask> compressorExports = new HashMap<>();
21
25
 
22
26
  public static String generateCacheFilePath(String extension, ReactApplicationContext reactContext){
@@ -43,7 +47,14 @@ public class Utils {
43
47
 
44
48
  @Override
45
49
  public void onError(String errorMessage) {
46
- promise.reject("Compression has canncelled");
50
+ if(errorMessage.equals(("class java.lang.AssertionError")))
51
+ {
52
+ promise.resolve(srcPath);
53
+ }
54
+ else
55
+ {
56
+ promise.reject("Compression has canncelled");
57
+ }
47
58
  }
48
59
 
49
60
  @Override
@@ -85,4 +96,18 @@ public class Utils {
85
96
  .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
86
97
  .emit(eventName, params);
87
98
  }
99
+
100
+ public static String getRealPath(String fileUrl,ReactApplicationContext reactContext){
101
+ if(fileUrl.startsWith("content://"))
102
+ {
103
+ try {
104
+ Uri uri= Uri.parse(fileUrl);
105
+ fileUrl= RealPathUtil.getRealPath(reactContext,uri);
106
+ }
107
+ catch (Exception ex) {
108
+ Log.d(TAG, " Please see this issue: https://github.com/Shobbak/react-native-compressor/issues/25");
109
+ }
110
+ }
111
+ return fileUrl;
112
+ }
88
113
  }
@@ -1,5 +1,6 @@
1
1
  package com.reactnativecompressor.Video;
2
2
 
3
+ import android.net.Uri;
3
4
  import android.util.Log;
4
5
 
5
6
  import androidx.annotation.NonNull;
@@ -14,14 +15,18 @@ import com.facebook.react.bridge.ReadableMap;
14
15
  import com.facebook.react.bridge.WritableMap;
15
16
  import com.facebook.react.module.annotations.ReactModule;
16
17
  import com.facebook.react.modules.core.DeviceEventManagerModule;
18
+ import com.reactnativecompressor.Utils.RealPathUtil;
17
19
 
20
+ import static com.reactnativecompressor.Utils.Utils.getRealPath;
18
21
  import static com.reactnativecompressor.Video.VideoCompressorHelper.video_activateBackgroundTask_helper;
19
22
  import static com.reactnativecompressor.Video.VideoCompressorHelper.video_deactivateBackgroundTask_helper;
20
23
  import static com.reactnativecompressor.Video.VideoCompressorHelper.video_upload_helper;
21
24
  import static com.reactnativecompressor.Utils.Utils.cancelCompressionHelper;
25
+
22
26
  @ReactModule(name = VideoModule.NAME)
23
27
  public class VideoModule extends ReactContextBaseJavaModule {
24
28
  public static final String NAME = "VideoCompressor";
29
+ private static final String TAG = "react-native-compessor";
25
30
  private final ReactApplicationContext reactContext;
26
31
  public VideoModule(ReactApplicationContext reactContext) {
27
32
  super(reactContext);
@@ -49,6 +54,7 @@ public class VideoModule extends ReactContextBaseJavaModule {
49
54
  ReadableMap optionMap,
50
55
  Promise promise) {
51
56
  final VideoCompressorHelper options = VideoCompressorHelper.fromMap(optionMap);
57
+ fileUrl=getRealPath(fileUrl,reactContext);
52
58
 
53
59
  if(options.compressionMethod==VideoCompressorHelper.CompressionMethod.auto)
54
60
  {
package/ios/.DS_Store CHANGED
Binary file
@@ -3,4 +3,4 @@
3
3
  //
4
4
  #import <React/RCTBridgeModule.h>
5
5
  #import <React/RCTEventEmitter.h>
6
-
6
+ #import "ImageCompressor.h"
package/ios/Compressor.m CHANGED
@@ -9,6 +9,30 @@
9
9
  #define AlAsset_Library_Scheme @"assets-library"
10
10
  @implementation Compressor
11
11
  AVAssetWriter *assetWriter=nil;
12
+ static NSArray *metadatas;
13
+
14
+ - (NSArray *)metadatas
15
+ {
16
+ if (!metadatas) {
17
+ metadatas = @[
18
+ @"albumName",
19
+ @"artist",
20
+ @"comment",
21
+ @"copyrights",
22
+ @"creationDate",
23
+ @"date",
24
+ @"encodedby",
25
+ @"genre",
26
+ @"language",
27
+ @"location",
28
+ @"lastModifiedDate",
29
+ @"performer",
30
+ @"publisher",
31
+ @"title"
32
+ ];
33
+ }
34
+ return metadatas;
35
+ }
12
36
 
13
37
  RCT_EXPORT_MODULE()
14
38
 
@@ -20,17 +44,19 @@ RCT_EXPORT_METHOD(
20
44
  rejecter: (RCTPromiseRejectBlock) reject) {
21
45
  @try {
22
46
  ImageCompressorOptions *options = [ImageCompressorOptions fromDictionary:optionsDict];
47
+ [ImageCompressor getAbsoluteImagePath:imagePath completionHandler:^(NSString* absoluteImagePath){
48
+ if(options.autoCompress)
49
+ {
50
+ NSString *result = [ImageCompressor autoCompressHandler:absoluteImagePath options:options];
51
+ resolve(result);
52
+ }
53
+ else
54
+ {
55
+ NSString *result = [ImageCompressor manualCompressHandler:absoluteImagePath options:options];
56
+ resolve(result);
57
+ }
58
+ }];
23
59
 
24
- if(options.autoCompress)
25
- {
26
- NSString *result = [ImageCompressor autoCompressHandler:imagePath options:options];
27
- resolve(result);
28
- }
29
- else
30
- {
31
- NSString *result = [ImageCompressor manualCompressHandler:imagePath options:options];
32
- resolve(result);
33
- }
34
60
  }
35
61
  @catch (NSException *exception) {
36
62
  reject(exception.name, exception.reason, nil);
@@ -176,7 +202,7 @@ RCT_EXPORT_METHOD(
176
202
 
177
203
  //general
178
204
  RCT_EXPORT_METHOD(
179
- generateFile: (NSString*) extension
205
+ generateFilePath: (NSString*) extension
180
206
  resolver: (RCTPromiseResolveBlock) resolve
181
207
  rejecter: (RCTPromiseRejectBlock) reject) {
182
208
  @try {
@@ -188,6 +214,30 @@ RCT_EXPORT_METHOD(
188
214
  }
189
215
  }
190
216
 
217
+ RCT_EXPORT_METHOD(
218
+ getRealPath: (NSString*) path
219
+ type: (NSString*) type
220
+ resolver: (RCTPromiseResolveBlock) resolve
221
+ rejecter: (RCTPromiseRejectBlock) reject) {
222
+ @try {
223
+ if([type isEqualToString:@"video"])
224
+ {
225
+ [ImageCompressor getAbsoluteVideoPath:path completionHandler:^(NSString* absoluteImagePath){
226
+ resolve(absoluteImagePath);
227
+ }];
228
+ }
229
+ else
230
+ {
231
+ [ImageCompressor getAbsoluteImagePath:path completionHandler:^(NSString* absoluteImagePath){
232
+ resolve(absoluteImagePath);
233
+ }];
234
+ }
235
+ }
236
+ @catch (NSException *exception) {
237
+ reject(exception.name, exception.reason, nil);
238
+ }
239
+ }
240
+
191
241
  //general
192
242
  RCT_EXPORT_METHOD(
193
243
  getFileSize: (NSString*) filePath
@@ -216,6 +266,63 @@ RCT_EXPORT_METHOD(
216
266
  }
217
267
  }
218
268
 
269
+
270
+ RCT_EXPORT_METHOD(
271
+ getVideoMetaData: (NSString*) filePath
272
+ resolver: (RCTPromiseResolveBlock) resolve
273
+ rejecter: (RCTPromiseRejectBlock) reject) {
274
+ @try {
275
+ [ImageCompressor getAbsoluteVideoPath:filePath completionHandler:^(NSString *absoluteImagePath) {
276
+ if([absoluteImagePath containsString:@"file://"])
277
+ {
278
+ absoluteImagePath=[absoluteImagePath stringByReplacingOccurrencesOfString:@"file://"
279
+ withString:@""];
280
+ }
281
+ NSFileManager *fileManager = [NSFileManager defaultManager];
282
+
283
+ BOOL isDir;
284
+ if (![fileManager fileExistsAtPath:absoluteImagePath isDirectory:&isDir] || isDir){
285
+ NSError *err = [NSError errorWithDomain:@"file not found" code:-15 userInfo:nil];
286
+ reject([NSString stringWithFormat: @"%lu", (long)err.code], err.localizedDescription, err);
287
+ return;
288
+ }
289
+ NSDictionary *attrs = [fileManager attributesOfItemAtPath: absoluteImagePath error: NULL];
290
+ UInt32 fileSize = [attrs fileSize];
291
+ NSString *fileSizeString = [@(fileSize) stringValue];
292
+
293
+ NSMutableDictionary *result = [NSMutableDictionary new];
294
+ NSDictionary *assetOptions = @{AVURLAssetPreferPreciseDurationAndTimingKey: @YES};
295
+ AVURLAsset *asset = [AVURLAsset URLAssetWithURL:[NSURL fileURLWithPath:absoluteImagePath] options:assetOptions];\
296
+ AVAssetTrack *avAsset = [[asset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];
297
+ CGSize size = [avAsset naturalSize];
298
+ NSString *extension = [[absoluteImagePath lastPathComponent] pathExtension];
299
+ CMTime time = [asset duration];
300
+ int seconds = ceil(time.value/time.timescale);
301
+ [result setObject:[NSString stringWithFormat: @"%.2f", size.width] forKey:@"width"];
302
+ [result setObject:[NSString stringWithFormat: @"%.2f", size.height] forKey:@"height"];
303
+ [result setObject:extension forKey:@"extension"];
304
+ [result setObject:fileSizeString forKey:@"size"];
305
+ [result setObject:[@(seconds) stringValue] forKey:@"duration"];
306
+ NSArray *keys = [NSArray arrayWithObjects:@"commonMetadata", nil];
307
+ [asset loadValuesAsynchronouslyForKeys:keys completionHandler:^{
308
+ // string keys
309
+ for (NSString *key in [self metadatas]) {
310
+ NSArray *items = [AVMetadataItem metadataItemsFromArray:asset.commonMetadata
311
+ withKey:key
312
+ keySpace:AVMetadataKeySpaceCommon];
313
+ for (AVMetadataItem *item in items) {
314
+ [result setObject:item.value forKey:key];
315
+ }
316
+ }
317
+ resolve(result);
318
+ }];
319
+ }];
320
+ }
321
+ @catch (NSException *exception) {
322
+ reject(exception.name, exception.reason, nil);
323
+ }
324
+ }
325
+
219
326
  @end
220
327
 
221
328
 
@@ -242,4 +349,3 @@ RCT_EXTERN_METHOD(deactivateBackgroundTask: (NSDictionary *)options
242
349
  RCT_EXTERN_METHOD(cancelCompression:(NSString *)uuid)
243
350
 
244
351
  @end
245
-
@@ -12,4 +12,6 @@
12
12
  +(NSString *)autoCompressHandler:(NSString *)imagePath options:(ImageCompressorOptions*)options;
13
13
  + (NSString *)manualCompress:(UIImage *)image output:(enum OutputType)output quality:(float)quality outputExtension:(NSString*)outputExtension isBase64:(Boolean)isBase64;
14
14
  + (UIImage *)scaleAndRotateImage:(UIImage *)image;
15
+ + (void)getAbsoluteImagePath:(NSString *)imagePath completionHandler:(void (^)(NSString *absoluteImagePath))completionHandler;
16
+ +(void)getAbsoluteVideoPath:(NSString *)videoPath completionHandler:(void (^)(NSString *absoluteImagePath))completionHandler;
15
17
  @end