numandev1 / react-native-compressor

🗜️Compress Image, Video, and Audio same like Whatsapp 🚀✨
MIT License
852 stars 85 forks source link

deleting temp file in expo #155

Closed hssdiv closed 1 year ago

hssdiv commented 1 year ago

Current behavior

Not sure if same problem exists in regular react-native app but when using expo, files in "/tmp" folder (NSTemporaryDirectory) can't be deleted with deleteAsync() from 'expo-file-system'

[Error: Location 'file://......../tmp/...' isn't deletable.]

Expected behavior

expected: files stored in "/Library/Caches" folder, where they can be deleted with expo (that's where other temp files from other expo packages like expo-image-picker are being stored)

Platform

ios

currently using workaround with this patch-package

diff --git a/node_modules/react-native-compressor/ios/Compressor.xcodeproj/project.xcworkspace/xcuserdata/mac.xcuserdatad/UserInterfaceState.xcuserstate b/node_modules/react-native-compressor/ios/Compressor.xcodeproj/project.xcworkspace/xcuserdata/mac.xcuserdatad/UserInterfaceState.xcuserstate
new file mode 100644
index 0000000..60571b1
Binary files /dev/null and b/node_modules/react-native-compressor/ios/Compressor.xcodeproj/project.xcworkspace/xcuserdata/mac.xcuserdatad/UserInterfaceState.xcuserstate differ
diff --git a/node_modules/react-native-compressor/ios/Video/VideoCompressor.swift b/node_modules/react-native-compressor/ios/Video/VideoCompressor.swift
index 9296644..78a8f61 100644
--- a/node_modules/react-native-compressor/ios/Video/VideoCompressor.swift
+++ b/node_modules/react-native-compressor/ios/Video/VideoCompressor.swift
@@ -344,8 +344,19 @@ func makeValidUri(filePath: String) -> String {
       }

     func exportVideoHelper(url: URL,asset: AVAsset, bitRate: Int,resultWidth:Float,resultHeight:Float,uuid:String, onProgress: @escaping (Float) -> Void,  onCompletion: @escaping (URL) -> Void, onFailure: @escaping (Error) -> Void){
+        let paths = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask)
+        let cachesDirectory: URL = paths[0]
+        let pathFolder = cachesDirectory.appendingPathComponent("ReactNativeCompressor")
+
+        if !FileManager.default.fileExists(atPath: pathFolder.path) {
+          do {
+              try FileManager.default.createDirectory(atPath: pathFolder.path, withIntermediateDirectories: true, attributes: nil)
+          }
+          catch {}
+        }
+        
         var videoCompressionCounter:Int=0
-        var tmpURL = URL(fileURLWithPath: NSTemporaryDirectory(), isDirectory: true)
+        var tmpURL = pathFolder
           .appendingPathComponent(ProcessInfo().globallyUniqueString)
           .appendingPathExtension("mp4")
         tmpURL = URL(string:makeValidUri(filePath: tmpURL.absoluteString))!
github-actions[bot] commented 1 year ago

👋 @hssdiv Thanks for opening your issue here! If you find this package useful hit the star🌟!