Advertisement
Guest User

Untitled

a guest
Jun 15th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.05 KB | None | 0 0
  1. func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL) {
  2. guard let url = downloadTask.originalRequest?.url else { return }
  3. let documentsPath = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
  4. let destinationURL = documentsPath.appendingPathComponent(url.lastPathComponent)
  5. let booksPath = documentsPath.appendingPathComponent(self.saveTo, isDirectory: true).appendingPathComponent(url.lastPathComponent)
  6.  
  7. let pattern = "_\.\+-=!@#\$\%\^\&\*()!"
  8. let title = url.lastPathComponent.replacingOccurrences(of: pattern, with: " ", options: .regularExpression, range: nil)
  9.  
  10. let _ = CoreDataHelper.shared.create(params: ["title" : title], objectType: Book.self)
  11.  
  12. // delete original copy
  13. try? FileManager.default.removeItem(at: destinationURL)
  14.  
  15. // copy from temp to Document
  16. do {
  17. try FileManager.default.copyItem(at: location, to: booksPath)
  18. } catch let error {
  19. print("Copy Error: (error.localizedDescription)")
  20. }
  21. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement