@@ -19,46 +19,63 @@ public extension ACMNetworking {
1919 /// - Returns:
2020 /// - Void
2121 func download( to endpoint: ACMBaseEndpoint ,
22- currentRetryCount: Int ? = 0 ,
23- onSuccess: ACMGenericCallbacks . DownloadCallback ,
24- onError: ACMGenericCallbacks . ErrorCallback )
22+ currentRetryCount _: Int ? = 0 ,
23+ onSuccess: ACMGenericCallbacks . DownloadCallback ,
24+ onProgress: ACMGenericCallbacks . ProgressCallback = nil ,
25+ onError: ACMGenericCallbacks . ErrorCallback = nil )
2526 {
2627 guard let urlRequest = generateURLRequest ( endpoint: endpoint) else { return }
2728
2829 downloadTask = endpoint. session ( delegate: self ) . downloadTask ( with: urlRequest) { [ weak self] url, urlResponse, error in
2930 guard let self else { return }
3031
3132 self . handleNilErrorResponse ( with: endpoint, error: error, onError: onError)
33+ self . handleNilResponse ( with: endpoint, response: urlResponse, onError: onError)
3234 self . handleConnectivityError ( with: endpoint, error: error, onError: onError)
3335
34- guard let url else { return }
35-
36- guard let urlResponse else { return }
36+ guard let url,
37+ let urlResponse,
38+ let httpResponse = self . handleHttpResponse ( with: endpoint, response: urlResponse, onError: onError) ,
39+ let pathExtension = httpResponse. url? . pathExtension
40+ else {
41+ self . cancel ( )
42+ return
43+ }
3744
3845 self . cancel ( )
46+ self . taskProgress? . invalidate ( )
47+ self . taskProgress = nil
3948
4049 do {
41- let data = try Data ( contentsOf: url)
42-
43- let model = ACMDownloadModel ( data: data, localURL: url, response: urlResponse)
44- onSuccess ? ( model)
45- } catch { }
50+ let searchUrl = try FileManager . default. url ( for: . documentDirectory, in: . userDomainMask, appropriateFor: nil , create: true )
51+ let destinationDirectory = searchUrl
52+ . appendingPathComponent ( " ACMNetworking " )
4653
54+ if !FileManager. default. fileExists ( atPath: destinationDirectory. relativePath) {
55+ try FileManager . default. createDirectory ( at: destinationDirectory, withIntermediateDirectories: true )
56+ }
4757
48- /* guard let data = self.handleData(with: endpoint, data: data, onError: onError) else { return }
49- guard let httpResponse = self.handleHttpResponse(with: endpoint, response: response, onError: onError) else { return }
58+ let destination = destinationDirectory . appendingPathComponent ( url . lastPathComponent )
59+ . appendingPathExtension ( pathExtension )
5060
51- // Check if response is in valid http range
52- guard self.validateResponse(with: httpResponse) else {
53- self.executeRetry(with: endpoint, httpResponse: httpResponse, data: data, currentRetryCount: currentRetryCount, onSuccess: onSuccess, onError: onError)
54- return
55- }
61+ try FileManager . default. moveItem ( at: url, to: destination)
5662
57- self.cancel( )
63+ let data = try Data ( contentsOf : destination )
5864
59- self.handleResult(with: endpoint, data: data, onSuccess: onSuccess, onError: onError)
60- */
65+ let model = ACMDownloadModel ( data: data, localURL: destination, response: urlResponse)
66+ onSuccess ? ( model)
67+ } catch let e {
68+ let errorMessage = String ( format: ACMNetworkConstants . genericErrorMessage, e. localizedDescription)
69+ ACMBaseLogger . warning ( errorMessage)
70+ onError ? ( ACMBaseNetworkError ( message: ACMNetworkConstants . errorMessage, log: errorMessage, endpoint: endpoint) )
71+ }
6172 }
73+
74+ taskProgress = downloadTask? . progress. observe ( \. fractionCompleted, changeHandler: { progress, _ in
75+ let model = ACMProgressModel ( progress: progress. fractionCompleted)
76+ onProgress ? ( model)
77+ } )
78+
6279 downloadTask? . resume ( )
6380 }
6481}
0 commit comments