-
Notifications
You must be signed in to change notification settings - Fork 134
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Default to use aria2 for downloads if installed
- Loading branch information
1 parent
1da3d5c
commit e870ae8
Showing
7 changed files
with
318 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
import Foundation | ||
|
||
/// A LocalizedError that represents a non-zero exit code from running aria2c. | ||
struct Aria2CError: LocalizedError { | ||
var code: Code | ||
|
||
init?(exitStatus: Int32) { | ||
guard let code = Code(rawValue: exitStatus) else { return nil } | ||
self.code = code | ||
} | ||
|
||
var errorDescription: String? { | ||
"aria2c error: \(code.description)" | ||
} | ||
|
||
// https://github.com/aria2/aria2/blob/master/src/error_code.h | ||
enum Code: Int32, CustomStringConvertible { | ||
case undefined = -1 | ||
// Ignoring, not an error | ||
// case finished = 0 | ||
case unknownError = 1 | ||
case timeOut | ||
case resourceNotFound | ||
case maxFileNotFound | ||
case tooSlowDownloadSpeed | ||
case networkProblem | ||
case inProgress | ||
case cannotResume | ||
case notEnoughDiskSpace | ||
case pieceLengthChanged | ||
case duplicateDownload | ||
case duplicateInfoHash | ||
case fileAlreadyExists | ||
case fileRenamingFailed | ||
case fileOpenError | ||
case fileCreateError | ||
case fileIoError | ||
case dirCreateError | ||
case nameResolveError | ||
case metalinkParseError | ||
case ftpProtocolError | ||
case httpProtocolError | ||
case httpTooManyRedirects | ||
case httpAuthFailed | ||
case bencodeParseError | ||
case bittorrentParseError | ||
case magnetParseError | ||
case optionError | ||
case httpServiceUnavailable | ||
case jsonParseError | ||
case removed | ||
case checksumError | ||
|
||
var description: String { | ||
switch self { | ||
case .undefined: | ||
return "Undefined" | ||
case .unknownError: | ||
return "Unknown error" | ||
case .timeOut: | ||
return "Timed out" | ||
case .resourceNotFound: | ||
return "Resource not found" | ||
case .maxFileNotFound: | ||
return "Maximum number of file not found errors reached" | ||
case .tooSlowDownloadSpeed: | ||
return "Download speed too slow" | ||
case .networkProblem: | ||
return "Network problem" | ||
case .inProgress: | ||
return "Unfinished downloads in progress" | ||
case .cannotResume: | ||
return "Remote server did not support resume when resume was required to complete download" | ||
case .notEnoughDiskSpace: | ||
return "Not enough disk space available" | ||
case .pieceLengthChanged: | ||
return "Piece length was different from one in .aria2 control file" | ||
case .duplicateDownload: | ||
return "Duplicate download" | ||
case .duplicateInfoHash: | ||
return "Duplicate info hash torrent" | ||
case .fileAlreadyExists: | ||
return "File already exists" | ||
case .fileRenamingFailed: | ||
return "Renaming file failed" | ||
case .fileOpenError: | ||
return "Could not open existing file" | ||
case .fileCreateError: | ||
return "Could not create new file or truncate existing file" | ||
case .fileIoError: | ||
return "File I/O error" | ||
case .dirCreateError: | ||
return "Could not create directory" | ||
case .nameResolveError: | ||
return "Name resolution failed" | ||
case .metalinkParseError: | ||
return "Could not parse Metalink document" | ||
case .ftpProtocolError: | ||
return "FTP command failed" | ||
case .httpProtocolError: | ||
return "HTTP response header was bad or unexpected" | ||
case .httpTooManyRedirects: | ||
return "Too many redirects occurred" | ||
case .httpAuthFailed: | ||
return "HTTP authorization failed" | ||
case .bencodeParseError: | ||
return "Could not parse bencoded file (usually \".torrent\" file)" | ||
case .bittorrentParseError: | ||
return "\".torrent\" file was corrupted or missing information" | ||
case .magnetParseError: | ||
return "Magnet URI was bad" | ||
case .optionError: | ||
return "Bad/unrecognized option was given or unexpected option argument was given" | ||
case .httpServiceUnavailable: | ||
return "HTTP service unavailable" | ||
case .jsonParseError: | ||
return "Could not parse JSON-RPC request" | ||
case .removed: | ||
return "Reserved. Not used." | ||
case .checksumError: | ||
return "Checksum validation failed" | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import Foundation | ||
import PromiseKit | ||
|
||
/// Attempt and retry a task that fails with resume data up to `maximumRetryCount` times | ||
func attemptResumableTask<T>( | ||
maximumRetryCount: Int = 3, | ||
delayBeforeRetry: DispatchTimeInterval = .seconds(2), | ||
_ body: @escaping (Data?) -> Promise<T> | ||
) -> Promise<T> { | ||
var attempts = 0 | ||
func attempt(with resumeData: Data? = nil) -> Promise<T> { | ||
attempts += 1 | ||
return body(resumeData).recover { error -> Promise<T> in | ||
guard | ||
attempts < maximumRetryCount, | ||
let resumeData = (error as NSError).userInfo[NSURLSessionDownloadTaskResumeData] as? Data | ||
else { throw error } | ||
|
||
return after(delayBeforeRetry).then(on: nil) { attempt(with: resumeData) } | ||
} | ||
} | ||
return attempt() | ||
} | ||
|
||
/// Attempt and retry a task up to `maximumRetryCount` times | ||
func attemptRetryableTask<T>( | ||
maximumRetryCount: Int = 3, | ||
delayBeforeRetry: DispatchTimeInterval = .seconds(2), | ||
_ body: @escaping () -> Promise<T> | ||
) -> Promise<T> { | ||
var attempts = 0 | ||
func attempt() -> Promise<T> { | ||
attempts += 1 | ||
return body().recover { error -> Promise<T> in | ||
guard attempts < maximumRetryCount else { throw error } | ||
return after(delayBeforeRetry).then(on: nil) { attempt() } | ||
} | ||
} | ||
return attempt() | ||
} |
Oops, something went wrong.