Skip to content
/ easy-dl Public

EasyDL makes it easy to download multiple files with progress in Swift

License

Notifications You must be signed in to change notification settings

koher/easy-dl

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

72 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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"),

Cartfile

github "koher/easy-dl" "0.2.0"

License

The MIT License

About

EasyDL makes it easy to download multiple files with progress in Swift

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published