Marino Faggiana 7 years ago
parent
commit
26fe34c7f6
1 changed files with 11 additions and 18 deletions
  1. 11 18
      PickerFileProvider/FileProvider.swift

+ 11 - 18
PickerFileProvider/FileProvider.swift

@@ -162,26 +162,19 @@ class FileProvider: NSFileProviderExtension {
                     if (!metadata.directory) {
                         
                         let identifierPathUrl = fileProviderStorageURL!.appendingPathComponent(metadata.fileID)
-                        let toPath = "\(identifierPathUrl.path)/\(metadata.fileNameView)"
                         let atPath = "\(directoryUser)/\(metadata.fileID)"
+                        let toPath = "\(identifierPathUrl.path)/\(metadata.fileNameView)"
+                        
+                        do {
+                            try FileManager.default.createDirectory(atPath: identifierPathUrl.path, withIntermediateDirectories: true, attributes: nil)
+                        } catch let error {
+                            print("error: \(error)")
+                        }
                             
-                        if !FileManager.default.fileExists(atPath: toPath) {
-
-                            do {
-                                try FileManager.default.createDirectory(atPath: identifierPathUrl.path, withIntermediateDirectories: true, attributes: nil)
-                            } catch let error {
-                                print("error: \(error)")
-                            }
-                            
-                            if FileManager.default.fileExists(atPath: atPath) {
-                                do {
-                                    try FileManager.default.copyItem(atPath: atPath, toPath: toPath)
-                                } catch let error {
-                                    print("error: \(error)")
-                                }
-                            } else {
-                                FileManager.default.createFile(atPath: toPath, contents: nil, attributes: nil)
-                            }
+                        if FileManager.default.fileExists(atPath: atPath) {
+                            _ = self.copyFile(atPath, toPath: toPath)
+                        } else {
+                            FileManager.default.createFile(atPath: toPath, contents: nil, attributes: nil)
                         }
                     }