Skip to content

Commit

Permalink
自定义下载文件名称 MQZHot#5 Option to use a custom file name for downloaded file.
Browse files Browse the repository at this point in the history
  • Loading branch information
MQZHot committed May 4, 2018
1 parent cc792d8 commit 0c46bd2
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 14 deletions.
2 changes: 1 addition & 1 deletion DaisyNet.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,10 @@
children = (
D63F09981FA605E300D15881 /* DaisyNet.swift */,
D63F099C1FA605E400D15881 /* RequestManager.swift */,
D63F099B1FA605E400D15881 /* DownloadManager.swift */,
D63F099A1FA605E300D15881 /* CacheManager.swift */,
D63F09971FA605E300D15881 /* DaisyLog.swift */,
D63F09991FA605E300D15881 /* DaisyValue.swift */,
D63F099B1FA605E400D15881 /* DownloadManager.swift */,
D63F09A91FA6131000D15881 /* CacheKey.swift */,
);
path = DaisyNet;
Expand Down
Binary file not shown.
17 changes: 14 additions & 3 deletions DaisyNet/DaisyNet/DaisyNet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -80,17 +80,28 @@ public func removeObjectCache(_ url: String, params: [String: Any]? = nil, dynam

// MARK: - 下载

/// 下载
/// 文件下载
///
/// - Parameters:
/// - url: url
/// - method: .get .post ... 默认.get
/// - parameters: 参数
/// - dynamicParams: 变化的参数,例如 时间戳-token 等
/// - encoding: 编码方式
/// - headers: 请求头
/// - fileName: 自定义文件名,需要带文件扩展名
/// - Returns: DownloadTaskManager
public func download(
_ url: String,
method: HTTPMethod = .get,
parameters: Parameters? = nil,
dynamicParams: Parameters? = nil,
encoding: ParameterEncoding = URLEncoding.default,
headers: HTTPHeaders? = nil)
headers: HTTPHeaders? = nil,
fileName: String? = nil)
->DownloadTaskManager
{
return DownloadManager.default.download(url, method: method, parameters: parameters, dynamicParams: dynamicParams, encoding: encoding, headers: headers)
return DownloadManager.default.download(url, method: method, parameters: parameters, dynamicParams: dynamicParams, encoding: encoding, headers: headers, fileName: fileName)
}

/// 取消下载
Expand Down
29 changes: 20 additions & 9 deletions DaisyNet/DaisyNet/DownloadManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ class DownloadManager {
parameters: Parameters? = nil,
dynamicParams: Parameters? = nil,
encoding: ParameterEncoding = URLEncoding.default,
headers: HTTPHeaders? = nil)
headers: HTTPHeaders? = nil,
fileName: String? = nil)
->DownloadTaskManager
{
let key = cacheKey(url, parameters, dynamicParams)
Expand All @@ -32,7 +33,7 @@ class DownloadManager {
dynamicTempParam.forEach { (arg) in
tempParam[arg.key] = arg.value
}
taskManager.download(url, method: method, parameters: tempParam, encoding: encoding, headers: headers)
taskManager.download(url, method: method, parameters: tempParam, encoding: encoding, headers: headers, fileName: fileName)
self.downloadTasks[key] = taskManager
taskManager.cancelCompletion = {
self.downloadTasks.removeValue(forKey: key)
Expand Down Expand Up @@ -137,7 +138,6 @@ public class DownloadTaskManager {
fileprivate var cccompletion: (()->())?
var cacheDictionary = [String: Data]()
private var key: String

init(_ url: String,
parameters: Parameters? = nil,
dynamicParams: Parameters? = nil) {
Expand All @@ -149,10 +149,11 @@ public class DownloadTaskManager {
method: HTTPMethod = .get,
parameters: Parameters? = nil,
encoding: ParameterEncoding = URLEncoding.default,
headers: HTTPHeaders? = nil)
headers: HTTPHeaders? = nil,
fileName: String?)
-> DownloadTaskManager
{
let destination = downloadDestination()
let destination = downloadDestination(fileName)
let resumeData = getResumeData(key)
if let resumeData = resumeData {
downloadRequest = manager.download(resumingWith: resumeData, to: destination)
Expand All @@ -169,6 +170,7 @@ public class DownloadTaskManager {
configuration.httpAdditionalHeaders = SessionManager.defaultHTTPHeaders
return SessionManager(configuration: configuration)
}()

/// 下载进度
@discardableResult
public func downloadProgress(progress: @escaping ((Double) -> Void)) -> DownloadTaskManager {
Expand Down Expand Up @@ -196,12 +198,21 @@ public class DownloadTaskManager {
})
}
/// 下载文件位置
private func downloadDestination() -> DownloadRequest.DownloadFileDestination {
///
/// - Parameter fileName: 自定义文件名
/// - Returns: 下载位置
private func downloadDestination(_ fileName: String?) -> DownloadRequest.DownloadFileDestination {
let destination: DownloadRequest.DownloadFileDestination = { _, response in
let cachesURL = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask)[0]
let fileURL = cachesURL.appendingPathComponent(response.suggestedFilename ?? "")
self.saveFilePath(fileURL.absoluteString)
return (fileURL, [.removePreviousFile, .createIntermediateDirectories])
if let fileName = fileName {
let fileURL = cachesURL.appendingPathComponent(fileName)
self.saveFilePath(fileURL.absoluteString)
return (fileURL, [.removePreviousFile, .createIntermediateDirectories])
} else {
let fileURL = cachesURL.appendingPathComponent(response.suggestedFilename!)
self.saveFilePath(fileURL.absoluteString)
return (fileURL, [.removePreviousFile, .createIntermediateDirectories])
}
}
return destination
}
Expand Down
2 changes: 1 addition & 1 deletion DaisyNet/DownloadTableViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class DownloadTableViewController: UITableViewController {
DaisyNet.downloadCancel(url)
case .suspend: /// 下载
/// hud
DaisyNet.download(url).downloadProgress {[weak self] _ in
DaisyNet.download(url, fileName: "\(indexPath.row)---.mp4").downloadProgress {[weak self] _ in
self?.update(indexPath)
}.response {[weak self] _ in
self?.update(indexPath)
Expand Down

0 comments on commit 0c46bd2

Please sign in to comment.