ImportFilesManager.swift 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. //
  2. // ImportFilesManager.swift
  3. // ColorfulWallpaper
  4. //
  5. // Created by nkl on 2024/9/10.
  6. //
  7. import Foundation
  8. import TZImagePickerController
  9. import TSVideoKit
  10. class ImportFilesManager: NSObject, TZImagePickerControllerDelegate , UIDocumentPickerDelegate {
  11. static let shared = ImportFilesManager()
  12. func openPhotoLibrary(parent: UIViewController) {
  13. guard let vc = TZImagePickerController(maxImagesCount: 9, delegate: self) else { return }
  14. vc.allowPickingImage = false
  15. vc.allowPreview = false
  16. vc.allowTakeVideo = true
  17. vc.allowTakePicture = false
  18. vc.allowCameraLocation = false
  19. vc.allowPickingMultipleVideo = true
  20. vc.allowPickingOriginalPhoto = false
  21. vc.naviBgColor = UIColor.white
  22. vc.naviTitleColor = UIColor.black
  23. vc.pickerDelegate = self
  24. parent.present(vc, animated: true)
  25. }
  26. func imagePickerController(_ picker: TZImagePickerController!, didFinishPickingPhotos photos: [UIImage]!, sourceAssets assets: [Any]!, isSelectOriginalPhoto: Bool) {
  27. DispatchQueue.global().async {
  28. for asset in assets {
  29. if let mAsset = asset as? PHAsset {
  30. TZImageManager.default().requestVideoURL(with: mAsset) { videoUrl in
  31. if let url = videoUrl {
  32. self.copyFileToUrl(url: url)
  33. }
  34. } failure: { _ in
  35. }
  36. }
  37. }
  38. }
  39. }
  40. func copyFileToUrl(url: URL) {
  41. let fileId = UUID().uuidString
  42. let path = TSVideoOperator.shared.configuration.fileDir.appendingPathComponent(fileId)
  43. let destinationURL = uniqueFileURL(at: path, originalURL: url)
  44. do {
  45. try FileManager.default.copyItem(at: url, to: destinationURL)
  46. Task {
  47. await self.importVideo(videoId: fileId, from: destinationURL)
  48. }
  49. } catch {
  50. print(error.localizedDescription)
  51. }
  52. }
  53. private func importVideo(videoId: String, from url: URL) async {
  54. let asset = AVAsset(url: url)
  55. let image = await asset.generateThumbnail()
  56. await MainActor.run {
  57. let title = url.lastPathComponent
  58. let pathExt = url.pathExtension.lowercased()
  59. let vPath = videoId + "/" + title
  60. let audios = ["mp3", "wav", "m4a"]
  61. var isRing: Bool = false
  62. if audios.contains(pathExt) {
  63. isRing = true
  64. }
  65. let imageData = image?.jpegData(compressionQuality: 0.8)
  66. let defaultData = UIImage.init(named: "ic_default")?.jpegData(compressionQuality: 0.8)
  67. let finalData = imageData ?? defaultData
  68. TSVideoOperator.shared.dataManager.createVideo(videoId: videoId, videoUrl: nil, audioStream: nil, videoStream: nil, videoTitle: title, artist: "UnKnown".localized(), artwork: finalData, online: false,customTag: isRing, status: .cached, vPath: vPath) { _ in
  69. NotificationCenter.default.post(name: .K_ImportSuccessNotifaction, object: nil)
  70. }
  71. }
  72. }
  73. func uniqueFileURL(at directory: URL, originalURL: URL) -> URL {
  74. let fileManager = FileManager.default
  75. // 检查目录是否存在,如果不存在则创建目录
  76. if !fileManager.fileExists(atPath: directory.path) {
  77. do {
  78. try fileManager.createDirectory(at: directory, withIntermediateDirectories: true, attributes: nil)
  79. } catch {
  80. // 这里可以选择抛出错误或处理错误
  81. }
  82. }
  83. let originalFilename = originalURL.deletingPathExtension().lastPathComponent
  84. let fileExtension = originalURL.pathExtension
  85. var uniqueURL = directory.appendingPathComponent(originalFilename).appendingPathExtension(fileExtension)
  86. var counter = 1
  87. while fileManager.fileExists(atPath: uniqueURL.path) {
  88. uniqueURL = directory.appendingPathComponent("\(originalFilename)-\(counter)").appendingPathExtension(fileExtension)
  89. counter += 1
  90. }
  91. return uniqueURL
  92. }
  93. func openFileDocument(parent: UIViewController,completion: ((Bool, String?, URL?) -> Void)?) {
  94. let documentTypes = ["public.mp3", "public.movie"]
  95. // 音频 mp3,wav,m4a
  96. // .MP3; .AAC; .WAV; .WMA; .CDA; .FLAC; .M4A; .MID; .MKA; .MP2; .MPA; .MPC; .APE; .OFR; .OGG; .RA; .WV; .TTA; .AC3; .DTS
  97. // 视频 mp4,avi, wmv,mov,mpg(mpeg)
  98. let documentVC = UIDocumentPickerViewController(documentTypes: documentTypes, in: .import)
  99. // Appropriate
  100. // let documentVC = UIDocumentPickerViewController(forOpeningContentTypes: [.aiff, .audio, .avi, .wav, .mp3, .movie])
  101. documentVC.delegate = self
  102. documentVC.modalPresentationStyle = .fullScreen
  103. parent.present(documentVC, animated: true)
  104. }
  105. func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
  106. if let first = urls.first {
  107. copyFileToUrl(url: first)
  108. }
  109. }
  110. }