123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259 |
- //
- // MultiTaskDown.swift
- // Pods
- //
- // Created by 100Years on 2025/3/17.
- //
- 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 地址
- /// - 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
- }
-
-
- guard let cachedFileURL = checkURLString(
- from: urlString,
- fileEx: fileEx,
- cacheDirectory: cacheDirectory,
- progressHandler: progressHandler,
- completion: completion
- ) else {
- return nil
- }
-
- let fileManager = FileManager.default
- //检查文件是否已存在于缓存中
- if fileManager.fileExists(atPath: cachedFileURL.path) {
- print("文件已存在于缓存中: \(cachedFileURL)")
- completion(cachedFileURL.path, nil)
- return nil
- }
-
-
- let downloader = TSMultiTaskDownloader.shared
- 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 对不对
- 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
- }
- }
|