react-native-compressor 1.6.4 → 1.7.1

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 (192) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +41 -27
  3. package/android/build.gradle +95 -42
  4. package/android/gradle.properties +5 -0
  5. package/android/src/main/AndroidManifest.xml +0 -1
  6. package/android/src/main/AndroidManifestNew.xml +2 -0
  7. package/android/src/main/java/com/reactnativecompressor/CompressorModule.java +107 -43
  8. package/android/src/main/java/com/reactnativecompressor/CompressorPackage.java +47 -19
  9. package/android/src/main/java/com/reactnativecompressor/Image/ImageCompressor.java +28 -16
  10. package/android/src/main/java/com/reactnativecompressor/Image/utils/ImageCompressorOptions.java +5 -1
  11. package/android/src/main/java/com/reactnativecompressor/Utils/MediaCache.java +65 -0
  12. package/android/src/main/java/com/reactnativecompressor/Utils/Utils.java +162 -3
  13. package/android/src/main/java/com/reactnativecompressor/Video/VideoCompressorHelper.java +7 -4
  14. package/android/src/main/java/com/reactnativecompressor/Video/VideoModule.java +11 -22
  15. package/android/src/newarch/CompressorSpec.java +9 -0
  16. package/android/src/newarch/VideoCompressorSpec.java +9 -0
  17. package/android/src/oldarch/CompressorSpec.java +34 -0
  18. package/android/src/oldarch/VideoCompressorSpec.java +26 -0
  19. package/ios/Compressor-Bridging-Header.h +1 -0
  20. package/ios/Compressor.h +9 -1
  21. package/ios/Compressor.mm +427 -0
  22. package/ios/Compressor.xcodeproj/project.pbxproj +6 -36
  23. package/ios/Image/ImageCompressor.h +12 -7
  24. package/ios/Image/ImageCompressor.m +164 -67
  25. package/ios/Image/ImageCompressorOptions.h +1 -0
  26. package/ios/Image/ImageCompressorOptions.m +5 -1
  27. package/ios/Utils/Downloader.h +16 -0
  28. package/ios/Utils/Downloader.m +150 -0
  29. package/ios/Utils/MediaCache.h +11 -0
  30. package/ios/Utils/MediaCache.m +51 -0
  31. package/ios/Video/VideoCompressor.swift +14 -5
  32. package/lib/commonjs/Audio/index.js +13 -14
  33. package/lib/commonjs/Audio/index.js.map +1 -1
  34. package/lib/commonjs/Image/index.js +26 -7
  35. package/lib/commonjs/Image/index.js.map +1 -1
  36. package/lib/commonjs/Main.js +29 -0
  37. package/lib/commonjs/Main.js.map +1 -0
  38. package/lib/commonjs/Spec/NativeCompressor.js +10 -0
  39. package/lib/commonjs/Spec/NativeCompressor.js.map +1 -0
  40. package/lib/commonjs/Spec/NativeVideoCompressor.js +10 -0
  41. package/lib/commonjs/Spec/NativeVideoCompressor.js.map +1 -0
  42. package/lib/commonjs/Video/index.js +17 -25
  43. package/lib/commonjs/Video/index.js.map +1 -1
  44. package/lib/commonjs/expo-plugin/compressor.js +1 -4
  45. package/lib/commonjs/expo-plugin/compressor.js.map +1 -1
  46. package/lib/commonjs/index.js +21 -28
  47. package/lib/commonjs/index.js.map +1 -1
  48. package/lib/commonjs/utils/index.js +32 -51
  49. package/lib/commonjs/utils/index.js.map +1 -1
  50. package/lib/module/Audio/index.js +13 -11
  51. package/lib/module/Audio/index.js.map +1 -1
  52. package/lib/module/Image/index.js +27 -6
  53. package/lib/module/Image/index.js.map +1 -1
  54. package/lib/module/Main.js +22 -0
  55. package/lib/module/Main.js.map +1 -0
  56. package/lib/module/Spec/NativeCompressor.js +3 -0
  57. package/lib/module/Spec/NativeCompressor.js.map +1 -0
  58. package/lib/module/Spec/NativeVideoCompressor.js +3 -0
  59. package/lib/module/Spec/NativeVideoCompressor.js.map +1 -0
  60. package/lib/module/Video/index.js +18 -19
  61. package/lib/module/Video/index.js.map +1 -1
  62. package/lib/module/expo-plugin/compressor.js +1 -3
  63. package/lib/module/expo-plugin/compressor.js.map +1 -1
  64. package/lib/module/index.js +8 -5
  65. package/lib/module/index.js.map +1 -1
  66. package/lib/module/utils/index.js +26 -34
  67. package/lib/module/utils/index.js.map +1 -1
  68. package/lib/typescript/Audio/index.d.ts +2 -1
  69. package/lib/typescript/Audio/index.d.ts.map +1 -0
  70. package/lib/typescript/Image/index.d.ts +11 -6
  71. package/lib/typescript/Image/index.d.ts.map +1 -0
  72. package/lib/typescript/Main.d.ts +4 -0
  73. package/lib/typescript/Main.d.ts.map +1 -0
  74. package/lib/typescript/Spec/NativeCompressor.d.ts +14 -0
  75. package/lib/typescript/Spec/NativeCompressor.d.ts.map +1 -0
  76. package/lib/typescript/Spec/NativeVideoCompressor.d.ts +13 -0
  77. package/lib/typescript/Spec/NativeVideoCompressor.d.ts.map +1 -0
  78. package/lib/typescript/Video/index.d.ts +5 -3
  79. package/lib/typescript/Video/index.d.ts.map +1 -0
  80. package/lib/typescript/expo-plugin/compressor.d.ts +2 -3
  81. package/lib/typescript/expo-plugin/compressor.d.ts.map +1 -0
  82. package/lib/typescript/index.d.ts +7 -3
  83. package/lib/typescript/index.d.ts.map +1 -0
  84. package/lib/typescript/utils/index.d.ts +6 -4
  85. package/lib/typescript/utils/index.d.ts.map +1 -0
  86. package/package.json +62 -38
  87. package/react-native-compressor.podspec +24 -1
  88. package/src/Audio/index.tsx +6 -4
  89. package/src/Image/index.tsx +38 -5
  90. package/src/Main.tsx +41 -0
  91. package/src/Spec/NativeCompressor.ts +15 -0
  92. package/src/Spec/NativeVideoCompressor.ts +14 -0
  93. package/src/Video/index.tsx +28 -11
  94. package/src/expo-plugin/compressor.ts +2 -1
  95. package/src/index.tsx +7 -2
  96. package/src/utils/index.tsx +11 -5
  97. package/android/.gradle/6.9/executionHistory/executionHistory.lock +0 -0
  98. package/android/.gradle/6.9/fileChanges/last-build.bin +0 -0
  99. package/android/.gradle/6.9/fileHashes/fileHashes.bin +0 -0
  100. package/android/.gradle/6.9/fileHashes/fileHashes.lock +0 -0
  101. package/android/.gradle/6.9/gc.properties +0 -0
  102. package/android/.gradle/buildOutputCleanup/buildOutputCleanup.lock +0 -0
  103. package/android/.gradle/buildOutputCleanup/cache.properties +0 -2
  104. package/android/.gradle/checksums/checksums.lock +0 -0
  105. package/android/.gradle/checksums/md5-checksums.bin +0 -0
  106. package/android/.gradle/checksums/sha1-checksums.bin +0 -0
  107. package/android/.gradle/vcs-1/gc.properties +0 -0
  108. package/android/.idea/caches/build_file_checksums.ser +0 -0
  109. package/android/.idea/compiler.xml +0 -6
  110. package/android/.idea/gradle.xml +0 -20
  111. package/android/.idea/jarRepositories.xml +0 -45
  112. package/android/.idea/libraries/Gradle__androidx_annotation_annotation_1_1_0.xml +0 -13
  113. package/android/.idea/libraries/Gradle__androidx_appcompat_appcompat_1_0_2_aar.xml +0 -24
  114. package/android/.idea/libraries/Gradle__androidx_arch_core_core_common_2_0_0.xml +0 -11
  115. package/android/.idea/libraries/Gradle__androidx_arch_core_core_runtime_2_0_0_aar.xml +0 -19
  116. package/android/.idea/libraries/Gradle__androidx_asynclayoutinflater_asynclayoutinflater_1_0_0_aar.xml +0 -19
  117. package/android/.idea/libraries/Gradle__androidx_autofill_autofill_1_1_0_aar.xml +0 -16
  118. package/android/.idea/libraries/Gradle__androidx_collection_collection_1_0_0.xml +0 -11
  119. package/android/.idea/libraries/Gradle__androidx_coordinatorlayout_coordinatorlayout_1_0_0_aar.xml +0 -24
  120. package/android/.idea/libraries/Gradle__androidx_core_core_1_1_0_aar.xml +0 -16
  121. package/android/.idea/libraries/Gradle__androidx_cursoradapter_cursoradapter_1_0_0_aar.xml +0 -19
  122. package/android/.idea/libraries/Gradle__androidx_customview_customview_1_0_0_aar.xml +0 -19
  123. package/android/.idea/libraries/Gradle__androidx_documentfile_documentfile_1_0_0_aar.xml +0 -19
  124. package/android/.idea/libraries/Gradle__androidx_drawerlayout_drawerlayout_1_0_0_aar.xml +0 -24
  125. package/android/.idea/libraries/Gradle__androidx_fragment_fragment_1_0_0_aar.xml +0 -24
  126. package/android/.idea/libraries/Gradle__androidx_interpolator_interpolator_1_0_0_aar.xml +0 -19
  127. package/android/.idea/libraries/Gradle__androidx_legacy_legacy_support_core_ui_1_0_0_aar.xml +0 -19
  128. package/android/.idea/libraries/Gradle__androidx_legacy_legacy_support_core_utils_1_0_0_aar.xml +0 -19
  129. package/android/.idea/libraries/Gradle__androidx_lifecycle_lifecycle_common_2_0_0.xml +0 -11
  130. package/android/.idea/libraries/Gradle__androidx_lifecycle_lifecycle_livedata_2_0_0_aar.xml +0 -19
  131. package/android/.idea/libraries/Gradle__androidx_lifecycle_lifecycle_livedata_core_2_0_0_aar.xml +0 -19
  132. package/android/.idea/libraries/Gradle__androidx_lifecycle_lifecycle_runtime_2_0_0_aar.xml +0 -19
  133. package/android/.idea/libraries/Gradle__androidx_lifecycle_lifecycle_viewmodel_2_0_0_aar.xml +0 -19
  134. package/android/.idea/libraries/Gradle__androidx_loader_loader_1_0_0_aar.xml +0 -19
  135. package/android/.idea/libraries/Gradle__androidx_localbroadcastmanager_localbroadcastmanager_1_0_0_aar.xml +0 -19
  136. package/android/.idea/libraries/Gradle__androidx_print_print_1_0_0_aar.xml +0 -24
  137. package/android/.idea/libraries/Gradle__androidx_slidingpanelayout_slidingpanelayout_1_0_0_aar.xml +0 -19
  138. package/android/.idea/libraries/Gradle__androidx_swiperefreshlayout_swiperefreshlayout_1_0_0_aar.xml +0 -24
  139. package/android/.idea/libraries/Gradle__androidx_vectordrawable_vectordrawable_1_0_1_aar.xml +0 -19
  140. package/android/.idea/libraries/Gradle__androidx_vectordrawable_vectordrawable_animated_1_0_0_aar.xml +0 -19
  141. package/android/.idea/libraries/Gradle__androidx_versionedparcelable_versionedparcelable_1_1_0_aar.xml +0 -13
  142. package/android/.idea/libraries/Gradle__androidx_viewpager_viewpager_1_0_0_aar.xml +0 -19
  143. package/android/.idea/libraries/Gradle__com_facebook_fbjni_fbjni_java_only_0_2_2.xml +0 -13
  144. package/android/.idea/libraries/Gradle__com_facebook_fresco_drawee_2_5_0_aar.xml +0 -15
  145. package/android/.idea/libraries/Gradle__com_facebook_fresco_fbcore_2_5_0_aar.xml +0 -18
  146. package/android/.idea/libraries/Gradle__com_facebook_fresco_fresco_2_5_0_aar.xml +0 -18
  147. package/android/.idea/libraries/Gradle__com_facebook_fresco_imagepipeline_2_5_0_aar.xml +0 -18
  148. package/android/.idea/libraries/Gradle__com_facebook_fresco_imagepipeline_base_2_5_0_aar.xml +0 -18
  149. package/android/.idea/libraries/Gradle__com_facebook_fresco_imagepipeline_native_2_5_0_aar.xml +0 -15
  150. package/android/.idea/libraries/Gradle__com_facebook_fresco_imagepipeline_okhttp3_2_5_0_aar.xml +0 -15
  151. package/android/.idea/libraries/Gradle__com_facebook_fresco_memory_type_ashmem_2_5_0_aar.xml +0 -15
  152. package/android/.idea/libraries/Gradle__com_facebook_fresco_memory_type_java_2_5_0_aar.xml +0 -15
  153. package/android/.idea/libraries/Gradle__com_facebook_fresco_memory_type_native_2_5_0_aar.xml +0 -15
  154. package/android/.idea/libraries/Gradle__com_facebook_fresco_nativeimagefilters_2_5_0_aar.xml +0 -15
  155. package/android/.idea/libraries/Gradle__com_facebook_fresco_nativeimagetranscoder_2_5_0_aar.xml +0 -15
  156. package/android/.idea/libraries/Gradle__com_facebook_fresco_ui_common_2_5_0_aar.xml +0 -15
  157. package/android/.idea/libraries/Gradle__com_facebook_infer_annotation_infer_annotation_0_18_0.xml +0 -13
  158. package/android/.idea/libraries/Gradle__com_facebook_react_react_native_0_66_1_aar.xml +0 -14
  159. package/android/.idea/libraries/Gradle__com_facebook_soloader_annotation_0_10_1.xml +0 -13
  160. package/android/.idea/libraries/Gradle__com_facebook_soloader_nativeloader_0_10_1.xml +0 -13
  161. package/android/.idea/libraries/Gradle__com_facebook_soloader_soloader_0_10_1_aar.xml +0 -15
  162. package/android/.idea/libraries/Gradle__com_facebook_yoga_proguard_annotations_1_19_0.xml +0 -13
  163. package/android/.idea/libraries/Gradle__com_google_code_findbugs_jsr305_3_0_2.xml +0 -13
  164. package/android/.idea/libraries/Gradle__com_googlecode_mp4parser_isoparser_1_0_6.xml +0 -13
  165. package/android/.idea/libraries/Gradle__com_squareup_okhttp3_okhttp_4_9_1.xml +0 -13
  166. package/android/.idea/libraries/Gradle__com_squareup_okhttp3_okhttp_urlconnection_4_9_1.xml +0 -13
  167. package/android/.idea/libraries/Gradle__com_squareup_okio_okio_2_9_0.xml +0 -13
  168. package/android/.idea/libraries/Gradle__io_github_lizhangqu_coreprogress_1_0_2_aar.xml +0 -18
  169. package/android/.idea/libraries/Gradle__javax_inject_javax_inject_1.xml +0 -13
  170. package/android/.idea/libraries/Gradle__org_aspectj_aspectjrt_1_8_2.xml +0 -13
  171. package/android/.idea/libraries/Gradle__org_jetbrains_annotations_13_0.xml +0 -13
  172. package/android/.idea/libraries/Gradle__org_jetbrains_kotlin_kotlin_annotations_jvm_1_3_72.xml +0 -13
  173. package/android/.idea/libraries/Gradle__org_jetbrains_kotlin_kotlin_stdlib_1_4_10.xml +0 -13
  174. package/android/.idea/libraries/Gradle__org_jetbrains_kotlin_kotlin_stdlib_common_1_4_10.xml +0 -13
  175. package/android/.idea/libraries/Gradle__org_jetbrains_kotlin_kotlin_stdlib_jdk7_1_4_10.xml +0 -13
  176. package/android/.idea/libraries/Gradle__org_jetbrains_kotlin_kotlin_stdlib_jdk8_1_4_10.xml +0 -13
  177. package/android/.idea/libraries/Gradle__org_jetbrains_kotlinx_kotlinx_coroutines_android_1_4_0.xml +0 -13
  178. package/android/.idea/libraries/Gradle__org_jetbrains_kotlinx_kotlinx_coroutines_core_jvm_1_4_0.xml +0 -13
  179. package/android/.idea/misc.xml +0 -9
  180. package/android/.idea/modules/android.iml +0 -138
  181. package/android/.idea/modules.xml +0 -8
  182. package/android/.idea/vcs.xml +0 -6
  183. package/android/gradle/wrapper/gradle-wrapper.jar +0 -0
  184. package/android/gradle/wrapper/gradle-wrapper.properties +0 -5
  185. package/android/gradlew +0 -183
  186. package/android/gradlew.bat +0 -100
  187. package/android/local.properties +0 -8
  188. package/ios/Compressor.m +0 -351
  189. package/ios/Compressor.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist +0 -8
  190. package/ios/Compressor.xcodeproj/project.xcworkspace/xcuserdata/apple.xcuserdatad/UserInterfaceState.xcuserstate +0 -0
  191. package/ios/Compressor.xcodeproj/xcshareddata/xcschemes/Compressor.xcscheme +0 -67
  192. package/ios/Compressor.xcodeproj/xcuserdata/apple.xcuserdatad/xcschemes/xcschememanagement.plist +0 -22
@@ -33,6 +33,9 @@ public class ImageCompressorOptions {
33
33
  case "returnableOutputType":
34
34
  options.returnableOutputType = ReturnableOutputType.valueOf(map.getString(key));
35
35
  break;
36
+ case "uuid":
37
+ options.uuid = map.getString(key);
38
+ break;
36
39
  }
37
40
  }
38
41
 
@@ -55,11 +58,12 @@ public class ImageCompressorOptions {
55
58
  auto, manual
56
59
  }
57
60
 
58
- public CompressionMethod compressionMethod = CompressionMethod.manual;
61
+ public CompressionMethod compressionMethod = CompressionMethod.auto;
59
62
  public int maxWidth = 1280;
60
63
  public int maxHeight = 1280;
61
64
  public float quality = 0.8f;
62
65
  public InputType input = InputType.uri;
63
66
  public OutputType output = OutputType.jpg;
67
+ public String uuid = "";
64
68
  public ReturnableOutputType returnableOutputType = ReturnableOutputType.uri;
65
69
  }
@@ -0,0 +1,65 @@
1
+ package com.reactnativecompressor.Utils;
2
+
3
+ import android.util.Log;
4
+
5
+ import java.io.File;
6
+ import java.util.ArrayList;
7
+ import java.util.List;
8
+
9
+ public class MediaCache {
10
+
11
+ private static final String TAG = "MediaCache";
12
+ private static List<String> completedImagePaths = new ArrayList<>();
13
+
14
+ public static void addCompletedImagePath(String imagePath) {
15
+ if (imagePath != null) {
16
+ // Your code to add the imagePath to a list or perform other actions if needed.
17
+ completedImagePaths.add(imagePath);
18
+ Log.d(TAG, "Successfully added image path: " + imagePath);
19
+ }
20
+ }
21
+
22
+ public static void removeCompletedImagePath(String imagePath) {
23
+ if (imagePath != null) {
24
+ if (completedImagePaths.contains(imagePath)) {
25
+ // Image path exists in the list, so remove it
26
+ completedImagePaths.remove(imagePath);
27
+
28
+ if (imagePath.startsWith("file://")) {
29
+ imagePath = imagePath.substring(7); // Remove "file://"
30
+ }
31
+
32
+ // Remove the image file
33
+ File file = new File(imagePath);
34
+ if (file.exists()) {
35
+ if (file.delete()) {
36
+ Log.d(TAG, "Successfully deleted image file: " + imagePath);
37
+ } else {
38
+ Log.d(TAG, "Failed to delete image file: " + imagePath);
39
+ }
40
+ } else {
41
+ Log.d(TAG, "Image file not found: " + imagePath);
42
+ }
43
+ } else {
44
+ Log.d(TAG, "Image path not found in the completedImagePaths list: " + imagePath);
45
+ }
46
+ }
47
+ }
48
+
49
+ public static void cleanupCache() {
50
+ // Iterate through the list of completed image paths and delete the corresponding files
51
+ for (String imagePath : completedImagePaths) {
52
+ File file = new File(imagePath);
53
+ if (file.exists()) {
54
+ if (file.delete()) {
55
+ Log.d(TAG, "Successfully deleted image file during cache cleanup: " + imagePath);
56
+ } else {
57
+ Log.d(TAG, "Failed to delete image file during cache cleanup: " + imagePath);
58
+ }
59
+ } else {
60
+ Log.d(TAG, "Image file not found during cache cleanup: " + imagePath);
61
+ }
62
+ }
63
+ completedImagePaths.clear();
64
+ }
65
+ }
@@ -13,10 +13,26 @@ import com.facebook.react.bridge.WritableMap;
13
13
  import com.facebook.react.modules.core.DeviceEventManagerModule;
14
14
  import numan.dev.videocompressor.VideoCompressTask;
15
15
  import numan.dev.videocompressor.VideoCompressor;
16
+
17
+ import okhttp3.Call;
18
+ import okhttp3.Callback;
19
+ import okhttp3.OkHttpClient;
20
+ import okhttp3.Request;
21
+ import okhttp3.Response;
22
+ import okhttp3.ResponseBody;
23
+
24
+ import java.io.BufferedInputStream;
16
25
  import java.io.File;
26
+ import java.io.FileOutputStream;
27
+ import java.io.IOException;
28
+ import java.net.HttpURLConnection;
29
+ import java.net.URL;
30
+ import java.net.URLConnection;
17
31
  import java.util.HashMap;
18
32
  import java.util.Map;
19
33
  import java.util.UUID;
34
+ import java.util.concurrent.Semaphore;
35
+ import java.util.concurrent.atomic.AtomicReference;
20
36
 
21
37
  public class Utils {
22
38
  static int videoCompressionThreshold=10;
@@ -41,8 +57,10 @@ public class Utils {
41
57
  }
42
58
  @Override
43
59
  public void onFinish(boolean result) {
60
+ String fileUrl="file://"+destinationPath;
44
61
  //convert finish,result(true is success,false is fail)
45
- promise.resolve("file://"+destinationPath);
62
+ promise.resolve(fileUrl);
63
+ MediaCache.removeCompletedImagePath(fileUrl);
46
64
  }
47
65
 
48
66
  @Override
@@ -97,7 +115,25 @@ public class Utils {
97
115
  .emit(eventName, params);
98
116
  }
99
117
 
100
- public static String getRealPath(String fileUrl,ReactApplicationContext reactContext){
118
+ public static int getFileSizeFromURL(String urlString) {
119
+ URL url;
120
+ HttpURLConnection conn = null;
121
+ try {
122
+ url = new URL(urlString);
123
+ conn = (HttpURLConnection) url.openConnection();
124
+ conn.setRequestMethod("HEAD");
125
+ conn.getInputStream();
126
+ return conn.getContentLength();
127
+ } catch (IOException e) {
128
+ throw new RuntimeException(e);
129
+ } finally {
130
+ if (conn != null) {
131
+ conn.disconnect();
132
+ }
133
+ }
134
+ }
135
+
136
+ public static String getRealPath(String fileUrl,ReactApplicationContext reactContext, String... args){
101
137
  if(fileUrl.startsWith("content://"))
102
138
  {
103
139
  try {
@@ -105,9 +141,132 @@ public class Utils {
105
141
  fileUrl= RealPathUtil.getRealPath(reactContext,uri);
106
142
  }
107
143
  catch (Exception ex) {
108
- Log.d(TAG, " Please see this issue: https://github.com/Shobbak/react-native-compressor/issues/25");
144
+ Log.d(TAG, " Please see this issue: https://github.com/numandev1/react-native-compressor/issues/25");
109
145
  }
110
146
  }
147
+ else if(fileUrl.startsWith("http://")||fileUrl.startsWith("https://"))
148
+ {
149
+ String uuid = (args.length > 0) ? args[0] : "";
150
+ fileUrl=downloadMediaWithProgress(fileUrl,reactContext, uuid);
151
+ Log.d(TAG, "getRealPath: "+fileUrl);
152
+ }
153
+
111
154
  return fileUrl;
112
155
  }
156
+
157
+ public static String downloadMediaWithProgress(String mediaUrl, ReactApplicationContext reactContext, String uuid) {
158
+ downloadCompression[0]=0;
159
+ OkHttpClient client = new OkHttpClient();
160
+
161
+ Request request = new Request.Builder()
162
+ .url(mediaUrl)
163
+ .build();
164
+
165
+ final Semaphore semaphore = new Semaphore(0); // Semaphore to wait for the download to complete
166
+ final AtomicReference<String> filePathRef = new AtomicReference<>(null); // To store the file path
167
+
168
+ // Perform the request asynchronously
169
+ client.newCall(request).enqueue(new Callback() {
170
+ @Override
171
+ public void onResponse(Call call, Response response) throws IOException {
172
+ if (response.isSuccessful()) {
173
+ ResponseBody responseBody = response.body();
174
+ if (responseBody != null) {
175
+ String fileExtension = "unknown"; // Default extension
176
+
177
+ // Detect the file extension based on the Content-Type header
178
+ String contentType = response.header("Content-Type");
179
+ if (contentType != null) {
180
+ if (contentType.equals("image/jpeg")) {
181
+ fileExtension = "jpg";
182
+ } else if (contentType.equals("image/png")) {
183
+ fileExtension = "png";
184
+ } else if (contentType.equals("video/mp4")) {
185
+ fileExtension = "mp4";
186
+ }
187
+ }
188
+
189
+ File cacheDir = reactContext.getCacheDir();
190
+ String randomFileName = UUID.randomUUID().toString() + "." + fileExtension;
191
+ File mediaFile = new File(cacheDir, randomFileName);
192
+
193
+ try (FileOutputStream fos = new FileOutputStream(mediaFile)) {
194
+ BufferedInputStream inputStream = new BufferedInputStream(responseBody.byteStream());
195
+ byte[] buffer = new byte[4096];
196
+ int bytesRead;
197
+ long totalBytesRead = 0;
198
+ long totalBytes = responseBody.contentLength();
199
+
200
+ if (totalBytes <= 0) {
201
+ totalBytes = 31457280;
202
+ }
203
+
204
+ Log.d(TAG, totalBytesRead+" totalBytesRead "+totalBytes);
205
+ Log.d(TAG, response.toString()+" responseBody "+responseBody.toString());
206
+ while ((bytesRead = inputStream.read(buffer)) != -1) {
207
+ fos.write(buffer, 0, bytesRead);
208
+ totalBytesRead += bytesRead;
209
+ double progressRatio = (double) totalBytesRead / totalBytes;
210
+ int progress = (int) (progressRatio * 100);
211
+ sendProgressUpdate(progress, uuid, reactContext);
212
+ }
213
+ fos.flush();
214
+ String filePath="file://" + mediaFile.getAbsolutePath();
215
+ MediaCache.addCompletedImagePath(filePath);
216
+ filePathRef.set(filePath); // Set the file path
217
+ } catch (IOException e) {
218
+ e.printStackTrace();
219
+ sendErrorResult(e.getMessage(), uuid, reactContext);
220
+ } finally {
221
+ semaphore.release(); // Release the semaphore when download is complete
222
+ }
223
+ }
224
+ } else {
225
+ sendErrorResult("Failed to download media: " + response.message(), uuid, reactContext);
226
+ semaphore.release(); // Release the semaphore in case of error
227
+ }
228
+ }
229
+
230
+ @Override
231
+ public void onFailure(Call call, IOException e) {
232
+ e.printStackTrace();
233
+ sendErrorResult(e.getMessage(), uuid, reactContext);
234
+ semaphore.release(); // Release the semaphore in case of failure
235
+ }
236
+ });
237
+
238
+ try {
239
+ semaphore.acquire(); // Wait for the download to complete
240
+ } catch (InterruptedException e) {
241
+ e.printStackTrace();
242
+ }
243
+
244
+ return filePathRef.get(); // Return the file path
245
+ }
246
+ static final int[] downloadCompression = {0};
247
+ private static void sendProgressUpdate(int progress, String uuid, ReactApplicationContext reactContext) {
248
+ int roundProgress=Math.round(progress);
249
+ if(roundProgress%videoCompressionThreshold==0&&roundProgress> downloadCompression[0]) {
250
+ WritableMap params = Arguments.createMap();
251
+ WritableMap data = Arguments.createMap();
252
+ params.putString("uuid", uuid);
253
+ data.putDouble("progress", progress / 100.0);
254
+ params.putMap("data", data);
255
+ sendEvent(reactContext, "downloadProgress", params);
256
+ Log.d(TAG, "downloadProgress: " + (progress / 100.0));
257
+ downloadCompression[0] =roundProgress;
258
+ }
259
+ }
260
+
261
+ private static void sendErrorResult(String error, String uuid, ReactApplicationContext reactContext) {
262
+ WritableMap params = Arguments.createMap();
263
+ WritableMap data = Arguments.createMap();
264
+ params.putString("uuid", uuid);
265
+ params.putString("error", error);
266
+ params.putMap("data", data);
267
+ Log.d(TAG, "videoDownloadError: "+error);
268
+ sendEvent(reactContext, "videoDownloadError", params);
269
+ }
270
+
271
+
113
272
  }
@@ -96,11 +96,11 @@ public class VideoCompressorHelper {
96
96
  auto, manual
97
97
  }
98
98
 
99
- public VideoCompressorHelper.CompressionMethod compressionMethod = VideoCompressorHelper.CompressionMethod.manual;
99
+ public VideoCompressorHelper.CompressionMethod compressionMethod = VideoCompressorHelper.CompressionMethod.auto;
100
100
  public float bitrate = 0;
101
101
  public String uuid = "";
102
102
  public float maxSize = 640.0f;
103
- public float minimumFileSizeForCompress = 16.0f;
103
+ public float minimumFileSizeForCompress = 0.0f;
104
104
 
105
105
  public static VideoCompressorHelper fromMap(ReadableMap map) {
106
106
  final VideoCompressorHelper options = new VideoCompressorHelper();
@@ -116,12 +116,15 @@ public class VideoCompressorHelper {
116
116
  case "maxSize":
117
117
  options.maxSize = (float) map.getDouble(key);
118
118
  break;
119
- case "uuid":
119
+ case "uuid":
120
120
  options.uuid = map.getString(key);
121
121
  break;
122
122
  case "minimumFileSizeForCompress":
123
123
  options.minimumFileSizeForCompress =(float) map.getDouble(key);
124
124
  break;
125
+ case "bitrate":
126
+ options.bitrate = (float) map.getDouble(key);
127
+ break;
125
128
 
126
129
  }
127
130
  }
@@ -149,7 +152,7 @@ public class VideoCompressorHelper {
149
152
  int bitrate=Integer.parseInt(metaRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_BITRATE));
150
153
 
151
154
  boolean isPortrait = height > width;
152
- int maxSize = 1920;
155
+ int maxSize = (int) options.maxSize;
153
156
  if(isPortrait && height > maxSize){
154
157
  width = (int) (((float)maxSize/height)*width);
155
158
  height = maxSize;
@@ -8,13 +8,10 @@ import androidx.annotation.Nullable;
8
8
 
9
9
  import com.facebook.react.bridge.Promise;
10
10
  import com.facebook.react.bridge.ReactApplicationContext;
11
- import com.facebook.react.bridge.ReactContext;
12
- import com.facebook.react.bridge.ReactContextBaseJavaModule;
13
11
  import com.facebook.react.bridge.ReactMethod;
14
12
  import com.facebook.react.bridge.ReadableMap;
15
- import com.facebook.react.bridge.WritableMap;
16
- import com.facebook.react.module.annotations.ReactModule;
17
- import com.facebook.react.modules.core.DeviceEventManagerModule;
13
+ import com.reactnativecompressor.Utils.MediaCache;
14
+ import com.reactnativecompressor.VideoCompressorSpec;
18
15
  import com.reactnativecompressor.Utils.RealPathUtil;
19
16
 
20
17
  import static com.reactnativecompressor.Utils.Utils.getRealPath;
@@ -23,14 +20,15 @@ import static com.reactnativecompressor.Video.VideoCompressorHelper.video_deacti
23
20
  import static com.reactnativecompressor.Video.VideoCompressorHelper.video_upload_helper;
24
21
  import static com.reactnativecompressor.Utils.Utils.cancelCompressionHelper;
25
22
 
26
- @ReactModule(name = VideoModule.NAME)
27
- public class VideoModule extends ReactContextBaseJavaModule {
23
+
24
+ public class VideoModule extends VideoCompressorSpec {
28
25
  public static final String NAME = "VideoCompressor";
29
26
  private static final String TAG = "react-native-compessor";
30
27
  private final ReactApplicationContext reactContext;
31
- public VideoModule(ReactApplicationContext reactContext) {
32
- super(reactContext);
33
- this.reactContext = reactContext;
28
+
29
+ public VideoModule(ReactApplicationContext context) {
30
+ super(context);
31
+ this.reactContext = context;
34
32
  }
35
33
 
36
34
  @NonNull
@@ -39,14 +37,6 @@ public class VideoModule extends ReactContextBaseJavaModule {
39
37
  return NAME;
40
38
  }
41
39
 
42
- private void sendEvent(ReactContext reactContext,
43
- String eventName,
44
- @Nullable WritableMap params) {
45
- reactContext
46
- .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
47
- .emit(eventName, params);
48
- }
49
-
50
40
  //Video
51
41
  @ReactMethod
52
42
  public void compress(
@@ -54,7 +44,8 @@ public class VideoModule extends ReactContextBaseJavaModule {
54
44
  ReadableMap optionMap,
55
45
  Promise promise) {
56
46
  final VideoCompressorHelper options = VideoCompressorHelper.fromMap(optionMap);
57
- fileUrl=getRealPath(fileUrl,reactContext);
47
+
48
+ fileUrl=getRealPath(fileUrl,reactContext,options.uuid);
58
49
 
59
50
  if(options.compressionMethod==VideoCompressorHelper.CompressionMethod.auto)
60
51
  {
@@ -64,8 +55,6 @@ public class VideoModule extends ReactContextBaseJavaModule {
64
55
  {
65
56
  VideoCompressorHelper.VideoCompressManual(fileUrl,options,promise,reactContext);
66
57
  }
67
-
68
-
69
58
  }
70
59
 
71
60
  @ReactMethod
@@ -117,7 +106,7 @@ public class VideoModule extends ReactContextBaseJavaModule {
117
106
  }
118
107
 
119
108
  @ReactMethod
120
- public void removeListeners(Integer count) {
109
+ public void removeListeners(double count) {
121
110
  // Keep: Required for RN built in Event Emitter Calls.
122
111
  }
123
112
  }
@@ -0,0 +1,9 @@
1
+ package com.reactnativecompressor;
2
+
3
+ import com.facebook.react.bridge.ReactApplicationContext;
4
+
5
+ abstract class CompressorSpec extends NativeCompressorSpec {
6
+ CompressorSpec(ReactApplicationContext context) {
7
+ super(context);
8
+ }
9
+ }
@@ -0,0 +1,9 @@
1
+ package com.reactnativecompressor;
2
+
3
+ import com.facebook.react.bridge.ReactApplicationContext;
4
+
5
+ public abstract class VideoCompressorSpec extends NativeVideoCompressorSpec {
6
+ protected VideoCompressorSpec(ReactApplicationContext context) {
7
+ super(context);
8
+ }
9
+ }
@@ -0,0 +1,34 @@
1
+ package com.reactnativecompressor;
2
+
3
+ import com.facebook.react.bridge.ReactApplicationContext;
4
+ import com.facebook.react.bridge.ReactContextBaseJavaModule;
5
+ import com.facebook.react.bridge.Promise;
6
+ import com.facebook.react.bridge.ReadableMap;
7
+
8
+ abstract class CompressorSpec extends ReactContextBaseJavaModule {
9
+ CompressorSpec(ReactApplicationContext context) {
10
+ super(context);
11
+ }
12
+
13
+ public abstract void image_compress(
14
+ String imagePath,
15
+ ReadableMap optionMap,
16
+ Promise promise);
17
+
18
+ public abstract void compress_audio(
19
+ String fileUrl,
20
+ ReadableMap optionMap,
21
+ Promise promise);
22
+
23
+ public abstract void generateFilePath(String extension, Promise promise);
24
+
25
+ public abstract void getRealPath(String path, String type, Promise promise);
26
+
27
+ public abstract void getVideoMetaData(String filePath, Promise promise);
28
+
29
+ public abstract void getFileSize(String filePath, Promise promise);
30
+
31
+ public abstract void addListener(String eventName);
32
+
33
+ public abstract void removeListeners(double count);
34
+ }
@@ -0,0 +1,26 @@
1
+ package com.reactnativecompressor;
2
+
3
+ import com.facebook.react.bridge.ReactApplicationContext;
4
+ import com.facebook.react.bridge.ReactContextBaseJavaModule;
5
+ import com.facebook.react.bridge.Promise;
6
+ import com.facebook.react.bridge.ReadableMap;
7
+
8
+ public abstract class VideoCompressorSpec extends ReactContextBaseJavaModule {
9
+ public VideoCompressorSpec(ReactApplicationContext context) {
10
+ super(context);
11
+ }
12
+
13
+ public abstract void compress(String fileUrl, ReadableMap optionMap, Promise promise);
14
+
15
+ public abstract void cancelCompression(String uuid);
16
+
17
+ public abstract void upload(String fileUrl, ReadableMap options, Promise promise);
18
+
19
+ public abstract void activateBackgroundTask(ReadableMap options, Promise promise);
20
+
21
+ public abstract void deactivateBackgroundTask(ReadableMap options, Promise promise);
22
+
23
+ public abstract void addListener(String eventName);
24
+
25
+ public abstract void removeListeners(double count);
26
+ }
@@ -4,3 +4,4 @@
4
4
  #import <React/RCTBridgeModule.h>
5
5
  #import <React/RCTEventEmitter.h>
6
6
  #import "ImageCompressor.h"
7
+ #import "MediaCache.h"
package/ios/Compressor.h CHANGED
@@ -1,5 +1,13 @@
1
+ #import <React/RCTEventEmitter.h>
2
+
3
+ #ifdef RCT_NEW_ARCH_ENABLED
4
+ #import "RNCompressorSpec.h"
5
+
6
+ @interface Compressor : RCTEventEmitter <NativeCompressorSpec>
7
+ #else
1
8
  #import <React/RCTBridgeModule.h>
2
9
 
3
- @interface Compressor : NSObject <RCTBridgeModule>
10
+ @interface Compressor : RCTEventEmitter <RCTBridgeModule>
11
+ #endif
4
12
 
5
13
  @end