1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- //
- // Downloader.swift
- // Learn Genie
- //
- // Created by Suraj Kumar Mandal on 07/09/21.
- //
- import Foundation
- class Downloader {
-
- // class func loadFileSync(url: NSURL, completion:(_ path:String, _ error:NSError?) -> Void) {
- // let documentsUrl = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first! as NSURL
- // let destinationUrl = documentsUrl.appendingPathComponent(url.lastPathComponent!)
- // if FileManager().fileExists(atPath: destinationUrl!.path) {
- // print("file already exists [\(String(describing: destinationUrl?.path))]")
- // completion(destinationUrl!.path, nil)
- // } else if let dataFromURL = NSData(contentsOf: url as URL){
- // if dataFromURL.write(to: destinationUrl!, atomically: true) {
- // print("file saved [\(String(describing: destinationUrl?.path))]")
- // completion(destinationUrl!.path, nil)
- // } else {
- // print("error saving file")
- // let error = NSError(domain:"Error saving file", code:1001, userInfo:nil)
- // completion(destinationUrl!.path, error)
- // }
- // } else {
- // let error = NSError(domain:"Error downloading file", code:1002, userInfo:nil)
- // completion(destinationUrl!.path, error)
- // }
- // }
- //
- // class func loadFileAsync(url: NSURL, completion:(_ path:String, _ error:NSError?) -> Void) {
- // let documentsUrl = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first! as NSURL
- // let destinationUrl = documentsUrl.appendingPathComponent(url.lastPathComponent!)
- // if FileManager().fileExists(atPath: destinationUrl!.path) {
- // print("file already exists [\(String(describing: destinationUrl?.path))]")
- // completion(destinationUrl!.path, nil)
- // } else {
- // let sessionConfig = URLSessionConfiguration.default
- // let session = URLSession(configuration: sessionConfig, delegate: nil, delegateQueue: nil)
- // let request = NSMutableURLRequest(url: url as URL)
- // request.httpMethod = "GET"
- // let task = session.dataTask(with: request as URLRequest, completionHandler: { (data: Data?, response: URLResponse?, error: Error?) -> Void in
- // if (error == nil) {
- // if let response = response as? HTTPURLResponse {
- // print("response=\(response)")
- // if response.statusCode == 200 {
- // if data.writeToURL(destinationUrl, atomically: true) {
- // print("file saved [\(destinationUrl!.path)]")
- // completion(destinationUrl!.path, error as NSError?)
- // } else {
- // print("error saving file")
- // let error = NSError(domain:"Error saving file", code:1001, userInfo:nil)
- // completion(destinationUrl!.path, error)
- // }
- // }
- // }
- // }
- // else {
- // print("Failure: \(String(describing: error?.localizedDescription))");
- // completion(destinationUrl!.path, error as NSError?)
- // }
- // })
- // task.resume()
- // }
- // }
- }
|