Skip to content

Commit

Permalink
fix(ios): fail to trim video with spaces in filename
Browse files Browse the repository at this point in the history
  • Loading branch information
maitrungduc1410 authored Oct 9, 2024
1 parent 4bed8f9 commit 23e733c
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 13 deletions.
12 changes: 6 additions & 6 deletions example/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ PODS:
- React-Core (= 0.72.3)
- React-jsi (= 0.72.3)
- ReactCommon/turbomodule/core (= 0.72.3)
- ffmpeg-kit-ios-https (6.0)
- ffmpeg-kit-ios-min (6.0)
- fmt (6.2.1)
- glog (0.3.5)
- hermes-engine (0.72.3):
Expand Down Expand Up @@ -319,8 +319,8 @@ PODS:
- react-native-image-picker (7.1.2):
- RCT-Folly (= 2021.07.22.00)
- React-Core
- react-native-video-trim (2.1.0):
- ffmpeg-kit-ios-https (~> 6.0)
- react-native-video-trim (2.2.6):
- ffmpeg-kit-ios-min (~> 6.0)
- RCT-Folly (= 2021.07.22.00)
- React-Core
- React-NativeModulesApple (0.72.3):
Expand Down Expand Up @@ -483,7 +483,7 @@ DEPENDENCIES:

SPEC REPOS:
trunk:
- ffmpeg-kit-ios-https
- ffmpeg-kit-ios-min
- fmt
- libevent
- SocketRocket
Expand Down Expand Up @@ -578,7 +578,7 @@ SPEC CHECKSUMS:
DoubleConversion: 5189b271737e1565bdce30deb4a08d647e3f5f54
FBLazyVector: 4cce221dd782d3ff7c4172167bba09d58af67ccb
FBReactNativeSpec: c6bd9e179757b3c0ecf815864fae8032377903ef
ffmpeg-kit-ios-https: f9c838aa5400a6c5ba12324c6b798f52197ba354
ffmpeg-kit-ios-min: 4e9a088f4ee9629435960b9d68e54848975f1931
fmt: ff9d55029c625d3757ed641535fd4a75fedc7ce9
glog: 04b94705f318337d7ead9e6d17c019bd9b1f6b1b
hermes-engine: 10fbd3f62405c41ea07e71973ea61e1878d07322
Expand All @@ -599,7 +599,7 @@ SPEC CHECKSUMS:
React-jsinspector: b511447170f561157547bc0bef3f169663860be7
React-logger: c5b527272d5f22eaa09bb3c3a690fee8f237ae95
react-native-image-picker: 51fdb07f7ef722988c8a219e1416153760a25e4d
react-native-video-trim: 31b20304981bbbfda10dba6d03f3cff0a14d5b15
react-native-video-trim: 3a038cc884b652cb3525699e20f734a15d7959ce
React-NativeModulesApple: c57f3efe0df288a6532b726ad2d0322a9bf38472
React-perflogger: 6bd153e776e6beed54c56b0847e1220a3ff92ba5
React-RCTActionSheet: c0b62af44e610e69d9a2049a682f5dba4e9dff17
Expand Down
2 changes: 2 additions & 0 deletions example/ios/VideoTrimExample/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,7 @@
<false/>
<key>NSPhotoLibraryUsageDescription</key>
<string>Need access to select image</string>
<key>NSPhotoLibraryAddUsageDescription</key>
<string>App needs access to your Photo Library to save videos.</string>
</dict>
</plist>
8 changes: 4 additions & 4 deletions example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,10 @@ export default function App() {
isValidFile(url2).then((res) => console.log('3isValidVideo:', res));
const url3 =
'https://file-examples.com/storage/fe825adda4669e5de9419e0/2017/11/file_example_MP3_5MG.mp3';
// showEditor(result.assets![0]?.uri || '', {
showEditor(url3, {
type: 'audio',
outputExt: 'wav',
showEditor(result.assets![0]?.uri || '', {
// showEditor(url3, {
// type: 'audio',
// outputExt: 'wav',
// maxDuration: 20,
// closeWhenFinish: false,
minDuration: 10,
Expand Down
40 changes: 38 additions & 2 deletions ios/VideoTrim.swift
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,9 @@ class VideoTrim: RCTEventEmitter, AssetLoaderDelegate, UIDocumentPickerDelegate
}

let destPath = URL(string: uri)
guard let destPath = destPath else { return }
let newPath = renameFile(at: destPath!, newName: "beforeTrim")

guard let destPath = newPath else { return }

DispatchQueue.main.async {
self.vc = VideoTrimmerViewController()
Expand Down Expand Up @@ -509,7 +511,7 @@ class VideoTrim: RCTEventEmitter, AssetLoaderDelegate, UIDocumentPickerDelegate

func assetLoader(_ loader: AssetLoader, didFailWithError error: any Error, forKey key: String) {
let message = "Failed to load \(key): \(error.localizedDescription)"
print(message)
print("Failed to load \(key)", message)

self.onError(message: message, code: .failToLoadMedia)
vc?.onAssetFailToLoad()
Expand Down Expand Up @@ -698,4 +700,38 @@ class VideoTrim: RCTEventEmitter, AssetLoaderDelegate, UIDocumentPickerDelegate
}
}
}

private func renameFile(at url: URL, newName: String) -> URL? {
let fileManager = FileManager.default

// Get the directory of the existing file
let directory = url.deletingLastPathComponent()

// Get the file extension
let fileExtension = url.pathExtension

// Create the new file URL with the new name and the same extension
let newFileURL = directory.appendingPathComponent(newName).appendingPathExtension(fileExtension)

// Check if a file with the new name already exists
if fileManager.fileExists(atPath: newFileURL.path) {
do {
// If the file exists, remove it first to avoid conflicts
try fileManager.removeItem(at: newFileURL)
} catch {
print("Error removing existing file: \(error)")
return nil
}
}

do {
// Rename (move) the file
try fileManager.moveItem(at: url, to: newFileURL)
print("File renamed successfully to \(newFileURL.absoluteString)")
return newFileURL
} catch {
print("Error renaming file: \(error)")
return nil
}
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-native-video-trim",
"version": "2.2.6",
"version": "2.2.7",
"description": "Video trimmer for your React Native app",
"main": "lib/commonjs/index",
"module": "lib/module/index",
Expand Down

0 comments on commit 23e733c

Please sign in to comment.