|
@@ -7,108 +7,6 @@
|
|
|
|
|
|
public extension TSCommonTool {
|
|
|
|
|
|
- // /// 多任务下载并缓存文件,依据 URL 的后缀名动态设置文件名
|
|
|
- // /// - Parameters:
|
|
|
- // /// - url: 文件的 URL 地址
|
|
|
- // /// - completion: 完成回调,返回本地缓存路径或错误
|
|
|
- // public static func multidownloadAndCacheFile(
|
|
|
- // from urlString: String,
|
|
|
- // fileEx:String? = nil,
|
|
|
- // cacheDirectory:String = "cacheAll",
|
|
|
- // progressHandler: @escaping (Double) -> Void,
|
|
|
- // completion: @escaping (String?, Error?) -> Void) -> TSMultiTaskDownloader?
|
|
|
- // {
|
|
|
- //
|
|
|
- // guard let url = URL(string: urlString) else{
|
|
|
- // completion(nil, NSError(domain: "url null", code: 0))
|
|
|
- // return nil
|
|
|
- // }
|
|
|
- //
|
|
|
- //
|
|
|
- // if !urlString.contains("http") && urlString.contains("/"){
|
|
|
- // completion(urlString.fillCachePath, nil)
|
|
|
- // return nil
|
|
|
- // }
|
|
|
- //
|
|
|
- // let fileManager = FileManager.default
|
|
|
- //
|
|
|
- // // 获取缓存目录下的 `cacheVideo` 文件夹路径
|
|
|
- // let cachesDirectory = fileManager.urls(for: .cachesDirectory, in: .userDomainMask).first!
|
|
|
- // let cacheVideoDirectory = cachesDirectory.appendingPathComponent(cacheDirectory)
|
|
|
- //
|
|
|
- // // 创建 `cacheVideo` 文件夹(如果不存在)
|
|
|
- // if !fileManager.fileExists(atPath: cacheVideoDirectory.path) {
|
|
|
- // do {
|
|
|
- // try fileManager.createDirectory(at: cacheVideoDirectory, withIntermediateDirectories: true, attributes: nil)
|
|
|
- // } catch {
|
|
|
- // completion(nil, error)
|
|
|
- // return nil
|
|
|
- // }
|
|
|
- // }
|
|
|
- //
|
|
|
- // var fileName = url.path.md5
|
|
|
- //
|
|
|
- // // 使用 URL 的 MD5 哈希值作为缓存文件名,附加 URL 的后缀名
|
|
|
- // var fileExtension = fileEx
|
|
|
- // fileExtension = fileExtension ?? (url.pathExtension.isEmpty ? "" : url.pathExtension)
|
|
|
- // if let fileExtension = fileExtension,fileExtension.count > 0 {
|
|
|
- // fileName = url.path.md5 + ".\(fileExtension)"
|
|
|
- // }
|
|
|
- //
|
|
|
- // let cachedFileURL = cacheVideoDirectory.appendingPathComponent(fileName)
|
|
|
- //
|
|
|
- // //检查文件是否已存在于缓存中
|
|
|
- // if fileManager.fileExists(atPath: cachedFileURL.path) {
|
|
|
- // print("文件已存在于缓存中: \(cachedFileURL)")
|
|
|
- // completion(cachedFileURL.path, nil)
|
|
|
- // return nil
|
|
|
- // }
|
|
|
- //
|
|
|
- //
|
|
|
- // let downloader = TSMultiTaskDownloader.shared
|
|
|
- //
|
|
|
- // let url1 = URL(string: "https://example.com/file1.zip")!
|
|
|
- // let url2 = URL(string: "https://example.com/file2.zip")!
|
|
|
- //
|
|
|
- // downloader.downloadFile(from: url, progressHandler: { progress in
|
|
|
- // print("Download progress for file1: \(progress * 100)%")
|
|
|
- // progressHandler(progress)
|
|
|
- // }, completionHandler: { tempFileURL, error in
|
|
|
- // if let error = error {
|
|
|
- // DispatchQueue.main.async {
|
|
|
- // completion(nil, error)
|
|
|
- // }
|
|
|
- // return
|
|
|
- // }
|
|
|
- //
|
|
|
- // guard let tempFileURL = tempFileURL else {
|
|
|
- // DispatchQueue.main.async {
|
|
|
- // completion(nil, NSError(domain: "DownloadError", code: -1, userInfo: [NSLocalizedDescriptionKey: "临时文件路径不存在"]))
|
|
|
- // }
|
|
|
- // return
|
|
|
- // }
|
|
|
- //
|
|
|
- // do {
|
|
|
- // if fileManager.fileExists(atPath: cachedFileURL.path) {
|
|
|
- // try fileManager.removeItem(atPath:cachedFileURL.path)
|
|
|
- // dePrint("下载成功,移除已有的旧文件: \(cachedFileURL)")
|
|
|
- // }
|
|
|
- // try fileManager.moveItem(at: tempFileURL, to: cachedFileURL)
|
|
|
- // dePrint("文件下载并缓存成功: \(cachedFileURL)")
|
|
|
- // DispatchQueue.main.async {
|
|
|
- // completion(cachedFileURL.path, nil)
|
|
|
- // }
|
|
|
- // } catch {
|
|
|
- // dePrint("文件下载成功,但失败:\(error)")
|
|
|
- // DispatchQueue.main.async {
|
|
|
- // completion(nil, error)
|
|
|
- // }
|
|
|
- // }
|
|
|
- // })
|
|
|
- //
|
|
|
- // return downloader
|
|
|
- // }
|
|
|
-
|
|
|
/// 多任务下载并缓存文件,依据 URL 的后缀名动态设置文件名
|
|
|
/// - Parameters:
|
|
|
/// - url: 文件的 URL 地址
|
|
@@ -116,6 +14,7 @@ public extension TSCommonTool {
|
|
|
public static func multidownloadAndCacheFile(
|
|
|
from urlString: String,
|
|
|
fileEx:String? = nil,
|
|
|
+ missingEx:String? = nil,
|
|
|
cacheDirectory:String = "cacheAll",
|
|
|
progressHandler: @escaping (Double) -> Void,
|
|
|
completion: @escaping (String?, Error?) -> Void) -> TSMultiTaskDownloader?
|
|
@@ -130,8 +29,8 @@ public extension TSCommonTool {
|
|
|
guard let cachedFileURL = checkURLString(
|
|
|
from: urlString,
|
|
|
fileEx: fileEx,
|
|
|
+ missingEx: missingEx,
|
|
|
cacheDirectory: cacheDirectory,
|
|
|
- progressHandler: progressHandler,
|
|
|
completion: completion
|
|
|
) else {
|
|
|
return nil
|
|
@@ -186,74 +85,5 @@ public extension TSCommonTool {
|
|
|
return downloader
|
|
|
}
|
|
|
|
|
|
- //检查 url 对不对
|
|
|
- public static func checkURLString(
|
|
|
- from urlString: String,
|
|
|
- fileEx:String? = nil,
|
|
|
- cacheDirectory:String = "cacheAll",
|
|
|
- progressHandler:((Double) -> Void)? = nil,
|
|
|
- completion:((String?, Error?) -> Void)? = nil
|
|
|
- )->URL?
|
|
|
- {
|
|
|
- guard let url = URL(string: urlString) else{
|
|
|
- completion?(nil, NSError(domain: "url null", code: 0))
|
|
|
- return nil
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- if !urlString.contains("http") && urlString.contains("/"){
|
|
|
- completion?(urlString.fillCachePath, nil)
|
|
|
- return nil
|
|
|
- }
|
|
|
-
|
|
|
- let fileManager = FileManager.default
|
|
|
-
|
|
|
- // 获取缓存目录下的 `cacheVideo` 文件夹路径
|
|
|
- let cachesDirectory = fileManager.urls(for: .cachesDirectory, in: .userDomainMask).first!
|
|
|
- let cacheVideoDirectory = cachesDirectory.appendingPathComponent(cacheDirectory)
|
|
|
-
|
|
|
- // 创建 `cacheVideo` 文件夹(如果不存在)
|
|
|
- if !fileManager.fileExists(atPath: cacheVideoDirectory.path) {
|
|
|
- do {
|
|
|
- try fileManager.createDirectory(at: cacheVideoDirectory, withIntermediateDirectories: true, attributes: nil)
|
|
|
- } catch {
|
|
|
- completion?(nil, error)
|
|
|
- return nil
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- var fileName = url.path.md5
|
|
|
-
|
|
|
- // 使用 URL 的 MD5 哈希值作为缓存文件名,附加 URL 的后缀名
|
|
|
- var fileExtension = fileEx
|
|
|
- fileExtension = fileExtension ?? (url.pathExtension.isEmpty ? "" : url.pathExtension)
|
|
|
- if let fileExtension = fileExtension,fileExtension.count > 0 {
|
|
|
- fileName = url.path.md5 + ".\(fileExtension)"
|
|
|
- }
|
|
|
-
|
|
|
- let cachedFileURL = cacheVideoDirectory.appendingPathComponent(fileName)
|
|
|
- return cachedFileURL
|
|
|
|
|
|
- }
|
|
|
-
|
|
|
- //获取 urlstring 本地的缓存 url path
|
|
|
- public static func getCachedURLString(
|
|
|
- from urlString: String,
|
|
|
- fileEx:String? = nil,
|
|
|
- cacheDirectory:String = "cacheAll")->URL?{
|
|
|
-
|
|
|
- if let cachedFileURL = checkURLString(
|
|
|
- from: urlString,
|
|
|
- fileEx: fileEx,
|
|
|
- cacheDirectory: cacheDirectory
|
|
|
- ){
|
|
|
- //检查文件是否已存在于缓存中
|
|
|
- if FileManager.default.fileExists(atPath: cachedFileURL.path) {
|
|
|
- print("文件已存在于缓存中: \(cachedFileURL)")
|
|
|
- return cachedFileURL
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- return nil
|
|
|
- }
|
|
|
}
|