Guest User

Untitled

a guest
May 22nd, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.33 KB | None | 0 0
  1. import Alamofire
  2.  
  3. typealias DownloadOptions = DownloadRequest.DownloadOptions
  4. typealias DownloadDestination = DownloadRequest.DownloadFileDestination
  5. typealias DownloadingProgressHandler = (_ completedUnitCount: Int64, _ totalUnitCount: Int64) -> Void
  6. typealias DownloadingEndHandler = (_ success: Bool, _ destination: URL?, _ fileName: String?) -> Void
  7.  
  8. protocol Downloadable: Hashable {
  9. var downloadURL: String! { get }
  10. var downloadDestination: DownloadDestination? { get }
  11. var acceptableMIMETypes: [String] { get }
  12. }
  13.  
  14. func ==<T: Downloadable>(lhs: T, rhs: T) -> Bool {
  15. return lhs.hashValue == rhs.hashValue
  16. }
  17.  
  18. class DownloadsManager: Alamofire.SessionManager {
  19. static let sharedManager = DownloadsManager()
  20. private(set) var downloads: Dictionary<Int, APIRequest> = [:]
  21.  
  22. private init(){
  23. let configuration = URLSessionConfiguration.background(withIdentifier: "com.earlyaccess.apps.YaBeam.background")
  24. configuration.timeoutIntervalForRequest = 30
  25. super.init(configuration: configuration)
  26. }
  27.  
  28. @discardableResult func download<T: Downloadable>(entity: T!, progressHandler: DownloadingProgressHandler? = nil, endHandler: DownloadingEndHandler? = nil) -> APIRequest {
  29. if let request = downloads[entity.hashValue] {
  30. request.cancel()
  31. }
  32.  
  33. let finalDestination: DownloadDestination = entity.downloadDestination != nil ? entity.downloadDestination! : { (url, response) in
  34. let fileManager = FileManager.default
  35. let directoryURL = fileManager.urls(for: .documentDirectory, in: .userDomainMask)[0]
  36. let pathComponent = response.suggestedFilename ?? ""
  37.  
  38. return (directoryURL.appendingPathComponent(pathComponent), DownloadOptions.removePreviousFile)
  39. }
  40. let destination: DownloadDestination = { (url, response) in
  41. let destination = finalDestination(url, response)
  42.  
  43. if FileManager.default.fileExists(atPath: destination.destinationURL.path) {
  44. do {
  45. try FileManager.default.removeItem(atPath: destination.destinationURL.path)
  46. } catch {}
  47. }
  48. return (destination.destinationURL, DownloadOptions.removePreviousFile)
  49. }
  50.  
  51. return download(entity.downloadURL, to: destination)
  52. .downloadProgress { (progress) in
  53. progressHandler?(progress.completedUnitCount, progress.totalUnitCount)
  54. }
  55. .response(completionHandler: { (response) in
  56. var failed = false
  57.  
  58. if response.error != nil ||
  59. response.response?.statusCode != 200 ||
  60. (entity.acceptableMIMETypes.isNotEmpty && !entity.acceptableMIMETypes.contains(response.response?.mimeType ?? "")) {
  61.  
  62. failed = true
  63. }
  64. var destinationURL: URL? = nil
  65. var fileName: String? = nil
  66.  
  67. if let temporaryURL = response.temporaryURL, let _response = response.response {
  68. destinationURL = finalDestination(temporaryURL, _response).destinationURL
  69. fileName = destinationURL?.lastPathComponent
  70. }
  71. endHandler?(!failed, destinationURL, fileName)
  72. })
  73. }
  74. }
Add Comment
Please, Sign In to add comment