react-native-compressor 1.12.0 → 1.14.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.
@@ -115,7 +115,7 @@ dependencies {
115
115
  implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.4"
116
116
  implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.4"
117
117
  implementation 'org.mp4parser:isoparser:1.9.56'
118
- implementation 'com.github.banketree:AndroidLame-kotlin:v0.0.1'
118
+ implementation 'com.github.kaushik-naik:TAndroidLame:277c2ab4b0'
119
119
  implementation 'javazoom:jlayer:1.0.1'
120
120
  }
121
121
 
@@ -7,10 +7,6 @@
7
7
 
8
8
 
9
9
  import AVFoundation
10
-
11
-
12
-
13
- let AlAsset_Library_Scheme = "assets-library"
14
10
  class AudioMain{
15
11
  static func compress_audio(_ fileUrl: String, optionMap: NSDictionary, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
16
12
  do {
@@ -366,18 +366,10 @@ class ImageCompressor {
366
366
  var size = CGSize.zero
367
367
  var scale: CGFloat = 1
368
368
  var resizeMode = RCTResizeMode.contain
369
- var assetID = ""
370
- var results: PHFetchResult<PHAsset>?
369
+ let assetID = imagePath.replacingOccurrences(of: "ph://", with: "")
370
+ let results = PHAsset.fetchAssets(withLocalIdentifiers: [assetID], options: nil)
371
371
 
372
- if imageURL?.scheme?.caseInsensitiveCompare("assets-library") == .orderedSame {
373
- assetID = imageURL?.absoluteString ?? ""
374
- results = PHAsset.fetchAssets(withALAssetURLs: [imageURL!], options: nil)
375
- } else {
376
- assetID = imagePath.replacingOccurrences(of: "ph://", with: "")
377
- results = PHAsset.fetchAssets(withLocalIdentifiers: [assetID], options: nil)
378
- }
379
-
380
- guard let asset = results?.firstObject else {
372
+ guard let asset = results.firstObject else {
381
373
  return
382
374
  }
383
375
 
@@ -28,23 +28,47 @@ class ImageMain {
28
28
  static func getImageMetaData(_ filePath: String, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
29
29
  let options = ImageCompressorOptions.fromDictionary([:])
30
30
  ImageCompressor.getAbsoluteImagePath(filePath, options: options) { ImagePath in
31
- if ImagePath.hasPrefix("file://") {
32
- let absoluteImagePath = URL(string: ImagePath)!.path
33
- Utils.getFileSize(absoluteImagePath) { fileSize in
34
- let _extension = (absoluteImagePath as NSString).pathExtension
35
- let imageData = NSData(contentsOfFile: absoluteImagePath)!
36
- let imageSource = CGImageSourceCreateWithData(imageData, nil)!
37
- let imageProperties = CGImageSourceCopyPropertiesAtIndex(imageSource, 0, nil)!
38
- let exif=imageProperties as NSDictionary
39
- let mutableExif = exif.mutableCopy() as! NSMutableDictionary
40
- if let fileSizeInt = Int(fileSize as! String) {
41
- mutableExif.setValue(fileSizeInt, forKey: "size")
42
- }
43
- mutableExif.setValue(_extension, forKey: "extension")
44
- resolve(mutableExif)
45
- } reject: { code, message, error in
46
- reject(code, message, error)
31
+ guard ImagePath.hasPrefix("file://") else {
32
+ reject("INVALID_PATH", "Image path must start with file://", nil)
33
+ return
34
+ }
35
+
36
+ guard let url = URL(string: ImagePath) else {
37
+ reject("INVALID_URL", "Failed to create URL from image path", nil)
38
+ return
39
+ }
40
+
41
+ let absoluteImagePath = url.path
42
+ let fileURL = url as CFURL
43
+
44
+ Utils.getFileSize(absoluteImagePath) { fileSize in
45
+ let _extension = (absoluteImagePath as NSString).pathExtension
46
+
47
+ // Use CGImageSourceCreateWithURL to avoid loading entire file into memory (prevents OOM crashes for large images)
48
+ guard let imageSource = CGImageSourceCreateWithURL(fileURL, nil) else {
49
+ reject("INVALID_IMAGE", "Failed to create image source from URL", nil)
50
+ return
51
+ }
52
+
53
+ guard let imageProperties = CGImageSourceCopyPropertiesAtIndex(imageSource, 0, nil) else {
54
+ reject("NO_PROPERTIES", "Failed to get image properties", nil)
55
+ return
56
+ }
57
+
58
+ let exif = imageProperties as NSDictionary
59
+ guard let mutableExif = exif.mutableCopy() as? NSMutableDictionary else {
60
+ reject("COPY_ERROR", "Failed to create mutable copy of image properties", nil)
61
+ return
62
+ }
63
+
64
+ if let fileSizeString = fileSize as? String, let fileSizeInt = Int(fileSizeString) {
65
+ mutableExif.setValue(fileSizeInt, forKey: "size")
47
66
  }
67
+
68
+ mutableExif.setValue(_extension, forKey: "extension")
69
+ resolve(mutableExif)
70
+ } reject: { code, message, error in
71
+ reject(code, message, error)
48
72
  }
49
73
  }
50
74
  }
@@ -17,10 +17,7 @@ class Utils {
17
17
  }
18
18
 
19
19
  static func makeValidUri(filePath: String) -> String {
20
- let fileWithUrl = URL(fileURLWithPath: filePath)
21
- let absoluteUrl = fileWithUrl.deletingLastPathComponent()
22
- let fileUrl = "file://\(absoluteUrl.path)/\(fileWithUrl.lastPathComponent)"
23
- return fileUrl;
20
+ return (filePath.starts(with: "file://") ? URL(string: filePath)! : URL(fileURLWithPath: filePath)).absoluteURL.absoluteString
24
21
  }
25
22
 
26
23
  static func getFileSize(from urlString: String, completion: @escaping (NSNumber?, Error?) -> Void) {
@@ -73,6 +70,8 @@ class Utils {
73
70
  let fileSize = attrs[.size] as? UInt64 {
74
71
  let fileSizeString = String(fileSize)
75
72
  resolve(fileSizeString)
73
+ } else {
74
+ reject("FILE_SIZE_ERROR", "Failed to get file size for path: \(filePath)", nil)
76
75
  }
77
76
  }
78
77
  } catch {
@@ -1,5 +1,4 @@
1
1
  import Foundation
2
- import AssetsLibrary
3
2
  import AVFoundation
4
3
  import Photos
5
4
  import MobileCoreServices
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-compressor",
3
- "version": "1.12.0",
3
+ "version": "1.14.0",
4
4
  "description": "Compress Image, Video, and Audio same like Whatsapp & Auto/Manual Compression | Background Upload | Download File | Create Video Thumbnail",
5
5
  "main": "lib/commonjs/index",
6
6
  "module": "lib/module/index",