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-alpha
Makes it easy to download multiple files with progress in Swift
⭐️ 6
🕓 8 hours ago
.package(url: "https://github.com/koher/easy-dl.git", from: "0.3.0-alpha")

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: 6
Last commit: 10 hours ago
jonrohan Something's broken? Yell at me @ptrpavlik. Praise and feedback (and money) is also welcome.

Release Notes

Support Swift Concurrency
10 hours ago
  • Add new download functions for async/await (ac2ac8f5a4ab79d435a3ee67949b57251dd895d8)
  • Downloader is isolated by MainActor (1085b7d2b7bde6ddf36679ec440b0158674d27c8)
  • Replace the type of byte sizes Int64 with Int (bb9e3fe4e7b3235e4f882b5ce842fcecbdae0e6d)
    • Because 32-bit Swift environments are no longer supported in practice and then Int is easier to handle than Int64 is
  • Rename Strategy to CachePolicy (103bbd4db624ac04c037f48e50371caa083daef8)
Old New
Downloader.Strategy Downloader.CachePolicy
.always reloadIgnoringLocalCacheData
.ifUpdated returnCacheDataIfUnmodifiedElseLoad
.ifNotCached returnCacheDataElseLoad
  • Rename labels of Downloader.init (56e8ba37a4801454fbe1fa54e25fd7c8c4f252b8)
    • commonCachePolicy to cachePolicy
    • commonRequestHeaders to requestHeaders
  • Quit supporting Carthage (c41525bbdb0894862df2bd739a01a45227be24ff)
    • Because Xcode supports SwiftPM now
  • Remove internal abstractions of URLSession (#10)

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