33// VideoEditor
44// Created by Coder ACJHP on 27.03.2026.
55
6-
76import Foundation
87import PhotosUI
98import AVFoundation
109import UniformTypeIdentifiers
1110
1211final class LocalMediaImportService : MediaImportService {
13-
12+
1413 private let fileManager : FileManager
15-
14+
1615 init ( fileManager: FileManager = . default) {
1716 self . fileManager = fileManager
1817 }
19-
18+
2019 func importPickedItems( _ results: [ PHPickerResult ] ) async throws -> [ ProjectFactory . ImportedMedia ] {
2120 var imported : [ ProjectFactory . ImportedMedia ] = [ ]
2221 imported. reserveCapacity ( results. count)
23-
22+
2423 for result in results {
2524 let provider = result. itemProvider
26-
25+
2726 if provider. hasItemConformingToTypeIdentifier ( UTType . movie. identifier) {
2827 let url = try await copyToAppStorage ( from: provider, type: . movie)
29-
28+
3029 let asset = AVURLAsset ( url: url)
3130 let duration = try await asset. load ( . duration)
3231 let seconds = duration. seconds
33-
32+
3433 imported. append (
3534 ProjectFactory . ImportedMedia (
3635 asset: . video( url) ,
@@ -39,17 +38,17 @@ final class LocalMediaImportService: MediaImportService {
3938 )
4039 continue
4140 }
42-
41+
4342 if provider. hasItemConformingToTypeIdentifier ( UTType . image. identifier) {
4443 let url = try await copyToAppStorage ( from: provider, type: . image)
4544 imported. append ( ProjectFactory . ImportedMedia ( asset: . image( url) ) )
4645 continue
4746 }
4847 }
49-
48+
5049 return imported
5150 }
52-
51+
5352 func copyToAppStorage( from provider: NSItemProvider , type: UTType ) async throws -> URL {
5453 try await withCheckedThrowingContinuation { continuation in
5554 provider. loadFileRepresentation ( forTypeIdentifier: type. identifier) { [ weak self] url, error in
@@ -63,12 +62,12 @@ final class LocalMediaImportService: MediaImportService {
6362 )
6463 return
6564 }
66-
65+
6766 if let error {
6867 continuation. resume ( throwing: error)
6968 return
7069 }
71-
70+
7271 guard let url else {
7372 continuation. resume (
7473 throwing: NSError (
@@ -79,27 +78,27 @@ final class LocalMediaImportService: MediaImportService {
7978 )
8079 return
8180 }
82-
81+
8382 do {
8483 let folder = try fileManager. url (
8584 for: . cachesDirectory,
8685 in: . userDomainMask,
8786 appropriateFor: nil ,
8887 create: true
8988 ) . appendingPathComponent ( " ImportedMedia " , isDirectory: true )
90-
89+
9190 if !fileManager. fileExists ( atPath: folder. path) {
9291 try fileManager. createDirectory ( at: folder, withIntermediateDirectories: true )
9392 }
94-
93+
9594 let ext = url. pathExtension
9695 let filename = UUID ( ) . uuidString + ( ext. isEmpty ? " " : " . \( ext) " )
9796 let destination = folder. appendingPathComponent ( filename)
98-
97+
9998 if fileManager. fileExists ( atPath: destination. path) {
10099 try fileManager. removeItem ( at: destination)
101100 }
102-
101+
103102 try fileManager. copyItem ( at: url, to: destination)
104103 continuation. resume ( returning: destination)
105104 } catch {
0 commit comments