Swiftpack.co - koher/easy-dl as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by koher.
koher/easy-dl 0.3.0-beta.2
EasyDL makes it easy to download multiple files with progress in Swift
⭐️ 8
🕓 25 weeks ago
.package(url: "https://github.com/koher/easy-dl.git", from: "0.3.0-beta.2")

EasyDL

EasyDL makes it easy to download multiple files in Swift.

let downloader = Downloader(items: [(url1, file1), (url2, file2)])

downloader.progress { bytesDownloaded, bytesExpectedToDownload in
    print("\(bytesDownloaded) / \(bytesExpectedToDownload!)")
}

downloader.completion { result in
    switch result {
    case .success:
        let data1 = try! Data(contentsOf: URL(fileURLWithPath: file1))
        let data2 = try! Data(contentsOf: URL(fileURLWithPath: file2))
        ...
    case .cancel:
        ...
    case let .failure(error):
        ...
    }
}

Flexible strategies

It is possible to choose download strategies for a Downloader and/or each Item.

enum Strategy {
    case always, ifUpdated, ifNotCached
}
let item1 = Item(url: url1, destination: file1) // `.ifUpdated` by default
let item2 = Item(url: url2, destination: file2, strategy: .always)
let item3 = Item(url: url3, destination: file3, strategy: .ifNotCached)

let downloader = Downloader(items: [item1, item2, item3])

Flexible progress handling

Following three overloads of progress are available.

downloader.progress { (bytesDownloaded: Int64, bytesExpectedToDownload: Int64?) in
    print("\(bytesDownloaded) / \(bytesExpectedToDownload!)")
}
downloader.progress { (rate: Float?) in
    print("\(rate!) / 1.0")
}
downloader.progress { (
    bytesDownloaded: Int64,
    bytesExpectedToDownload: Int64?,
    currentItemIndex: Int,
    bytesDownloadedForCurrentItem: Int64,
    bytesExpectedToDownloadForCurrentItem: Int64?
) in
    print("\(currentItemIndex) / \(downloader.items.count)")
}

Also precise progress or non-precise progress can be designated.

let downloader = Downloader(items: [(url1, file1), (url2, file2)], needsPreciseProgress: false)

Usually, a Downloader gets sizes of the Items by sending HEAD requests and summing up Content-Lengths in the response headers before starting downloads. When needsPreciseProgress is false, a Downloader omits those HEAD request. Then progress for Float? calls a callback with pseudo progress, which is calculated on the assumption that all Items has a same size. That is, the amout of the progress for one Item is 1.0 / Float(items.count).

Installation

Swift Package Manager

Package.swift

Use Swift Package Manager. Add the following to dependencies in your Package.swift file.

.package(url: "https://github.com/koher/easy-dl.git", from: "0.2.0"),

Carthage

Cartfile

github "koher/easy-dl" "0.2.0"

License

The MIT License

GitHub

link
Stars: 8
Last commit: 25 weeks ago
jonrohan Something's broken? Yell at me @ptrpavlik. Praise and feedback (and money) is also welcome.

Release Notes

30 weeks ago
  • Make requestHeaders of Downloader.init non-Optional (489e129c36dfe64cdf31ba5b8d653d4200af25c0)
  • Add requestHeaders to Downloader.Item (899ae938088e4cf40e33f668fdb11d8f568c488a)
  • Introduce DownloadingError to make it easier to handle errors by their causes (d7f5508a66c651667fd1944a9a9c981493305d8f)
    • Downloader.ResponseError was replaced with DownloadingError.response

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics