Skip to content

Instantly share code, notes, and snippets.

@boboboa32
Last active March 11, 2023 10:38
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save boboboa32/1f4305c6b5b1f660b5de7da0da473af9 to your computer and use it in GitHub Desktop.
Save boboboa32/1f4305c6b5b1f660b5de7da0da473af9 to your computer and use it in GitHub Desktop.
Moya file download
import Moya
import SwiftyJSON
enum FileWebService {
case download(url: String, fileName: String?)
var localLocation: URL {
switch self {
case .download(let url, let fileName):
let fileKey: String = url.MD5 // use url's md5 as local file name
let directory: URL = FileSystem.downloadDirectory
var filePath: URL = directory.appendingPathComponent(fileKey)
if let name = fileName {
// append path extension if exit
let pathExtension: String = (name as NSString).pathExtension.lowercased()
filePath = filePath.appendingPathExtension(pathExtension)
}
return filePath
}
}
var downloadDestination: DownloadDestination {
// `createIntermediateDirectories` will create directories in file path
return { _, _ in return (self.localLocation, [.removePreviousFile, .createIntermediateDirectories]) }
}
}
extension FileWebService: TargetType {
var baseURL: URL {
switch self {
case .download(let url, _):
return URL(string: url)!
}
}
var path: String {
switch self {
case .download(_, _):
return ""
}
}
var method: Moya.Method {
switch self {
case .download(_, _):
return .get
}
}
var parameters: [String: Any]? {
switch self {
case .download:
return nil
}
}
var parameterEncoding: ParameterEncoding {
return URLEncoding.default
}
var task: Task {
switch self {
case .download(_, _):
return .download(.request(downloadDestination))
}
}
var sampleData: Data {
return Data()
}
var headers: [String: String]? {
return nil
}
}
struct FileProvider {
static let provider = MoyaProvider<FileWebService>(plugins: [NetworkLoggerPlugin(verbose: WebService.verbose)])
static func request(target: FileWebService, progress: ProgressBlock?, completion: @escaping (WebService.Result) -> Void) -> Cancellable {
return provider.request(target, progress: progress) { result in
switch result {
case let .success(response):
let data = response.data
let json = JSON(data: data)
completion(.success(json))
case .failure(_):
completion(.failure("download fail"))
}
}
}
}
class WebService {
// set false when release
static var verbose: Bool = true
// response result type
enum Result {
case success(JSON)
case failure(String)
}
}
class FileSystem {
static let documentsDirectory: URL = {
let urls = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
return urls[urls.endIndex - 1]
}()
static let cacheDirectory: URL = {
let urls = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask)
return urls[urls.endIndex - 1]
}()
static let downloadDirectory: URL = {
let directory: URL = FileSystem.documentsDirectory.appendingPathComponent("/Download/")
return directory
}()
}
@okiookio
Copy link

how to pause and resume download?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment