Download.swift 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. //
  2. // Download.swift
  3. // Runner
  4. //
  5. // Created by Yayan Dwi on 24/04/20.
  6. // Copyright © 2020 The Chromium Authors. All rights reserved.
  7. //
  8. import Foundation
  9. import Alamofire
  10. public class Download {
  11. public init() {}
  12. var delegate : DownloadDelegate?
  13. public func getDelegate() -> DownloadDelegate? {
  14. return delegate
  15. }
  16. private var downloadBufferQueue = DispatchQueue(label: "DOWNLOAD_BUFFER", attributes: .concurrent)
  17. var DOWNLOAD_BUFFER = [Data?]()
  18. var DOWNLOAD_SESSION = [Session]()
  19. var DOWNLOAD_URL = "https://newuniverse.io/filepalio/image/"
  20. public func start(forKey: String, delegate: DownloadDelegate){
  21. self.delegate = delegate
  22. let download = Nexilis.getDownload(forKey: forKey)
  23. if download == nil {
  24. Nexilis.addDownload(forKey: forKey, download: self)
  25. }
  26. _ = Nexilis.write(message: CoreMessage_TMessageBank.getImageDownload(p_image_id: forKey))
  27. }
  28. var onDownloadProgress: ((String, Double) -> ())?
  29. public func start(forKey: String, completion: @escaping (String, Double)->()) {
  30. self.onDownloadProgress = completion
  31. let download = Nexilis.getDownload(forKey: forKey)
  32. if download == nil {
  33. Nexilis.addDownload(forKey: forKey, download: self)
  34. }
  35. _ = Nexilis.write(message: CoreMessage_TMessageBank.getImageDownload(p_image_id: forKey))
  36. }
  37. public func startHTTP(forKey: String, completion: @escaping (String, Double)->()) {
  38. _ = startHTTP(filename: forKey, baseURL: DOWNLOAD_URL, completion: completion)
  39. }
  40. public func startHTTP(filename: String, baseURL: String, completion: @escaping (String, Double)->()) -> DownloadRequest {
  41. var sep = ""
  42. if baseURL.last != "/" {
  43. sep = "/"
  44. }
  45. let fullURL = "\(baseURL)\(sep)\(filename)"
  46. do {
  47. // let destination = DownloadRequest.suggestedDownloadDestination(for: .documentDirectory, in: .userDomainMask, options: [.removePreviousFile, .createIntermediateDirectories])
  48. let downloadRequest = AF.download(fullURL)
  49. .downloadProgress(queue: downloadBufferQueue) { progress in
  50. completion(filename,progress.fractionCompleted*100)
  51. }
  52. .responseData { result in
  53. if let successResponse = result.value {
  54. //print("Response success")
  55. do {
  56. let documentDir = try FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
  57. let url = documentDir.appendingPathComponent(filename)
  58. //print("write file \(url.path)")
  59. try successResponse.write(to: url)
  60. completion(filename,100)
  61. }
  62. catch {}
  63. }
  64. else {
  65. let statusCode = result.response?.statusCode
  66. //print("Response fail: \(statusCode)")
  67. completion(filename,0)
  68. }
  69. }
  70. return downloadRequest
  71. }
  72. catch {}
  73. }
  74. func put(part: Int, buffer: Data){
  75. downloadBufferQueue.async (flags: .barrier){
  76. self.DOWNLOAD_BUFFER.insert(buffer, at: part)
  77. }
  78. }
  79. func size() -> Int {
  80. var size = 0
  81. downloadBufferQueue.sync {
  82. for b in DOWNLOAD_BUFFER {
  83. size += b?.count ?? 0
  84. }
  85. }
  86. return size
  87. }
  88. func remove() -> Data {
  89. var result = Data()
  90. downloadBufferQueue.sync {
  91. for i in DOWNLOAD_BUFFER {
  92. if let b = i {
  93. result.append(contentsOf: b)
  94. }
  95. }
  96. }
  97. return result
  98. }
  99. }