PQDownloadFileManager.swift 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. //
  2. // PQDownloadFileManager.swift
  3. // PQSpeed
  4. //
  5. // Created by SanW on 2020/12/9.
  6. // Copyright © 2020 BytesFlow. All rights reserved.
  7. //
  8. import UIKit
  9. class PQDownloadFileManager: NSObject {
  10. /// 创建文件
  11. /// - Parameter url: 原地址
  12. /// - Returns: <#description#>
  13. class func createDownloadFilePath(url: String, fileExtensionType: FileExtensionType?) -> String {
  14. let fileManager = FileManager.default
  15. let filePath = downloadFileLocalPath(url: url, fileExtensionType: fileExtensionType)
  16. if !fileManager.fileExists(atPath: filePath) {
  17. let isFinished = fileManager.createFile(atPath: filePath, contents: nil, attributes: nil)
  18. BFLog(message: "生成本地地址:\(url),localPath = \(filePath),isFinished:\(isFinished)")
  19. } else {
  20. BFLog(message: "已存在本地地址:\(url),localPath = \(filePath)")
  21. }
  22. return filePath
  23. }
  24. /// 获取文件本地存储地址
  25. /// - Parameter url: 原地址
  26. /// - Returns: <#description#>
  27. class func downloadFileLocalPath(url: String, fileExtensionType: FileExtensionType?) -> String {
  28. if url.hasPrefix(downloadDirectory) {
  29. return url
  30. }
  31. let type: String = fileExtensionType?.rawValue ?? url.pathExtension
  32. BFLog(message: "localPath : \(downloadDirectory + url.kf.md5 + (type.count > 0 ? ".\(type)" : ""))")
  33. return downloadDirectory + url.kf.md5 + (type.count > 0 ? ".\(type)" : "")
  34. }
  35. /// 获取已缓存大小
  36. /// - Parameter url: <#url description#>
  37. /// - Returns: <#description#>
  38. class func downloadFileLength(url: String, fileExtensionType: FileExtensionType?) -> Int64 {
  39. let fileManager = FileManager.default
  40. let filePath = downloadFileLocalPath(url: url, fileExtensionType: fileExtensionType)
  41. if fileManager.fileExists(atPath: filePath) {
  42. let att = try? fileManager.attributesOfItem(atPath: filePath)
  43. return Int64((att?[FileAttributeKey.size] as? UInt64) ?? 0)
  44. }
  45. return 0
  46. }
  47. /// 移除已下载文件
  48. /// - Parameter url: <#url description#>
  49. /// - Returns: <#description#>
  50. class func removeDownloadFile(url: String, fileExtensionType: FileExtensionType?) {
  51. let fileManager = FileManager.default
  52. var path = url
  53. if !path.hasPrefix(downloadDirectory) {
  54. path = downloadFileLocalPath(url: url, fileExtensionType: fileExtensionType)
  55. }
  56. if fileManager.fileExists(atPath: path) {
  57. BFLog(message: "删除本地文件 == \(path)")
  58. try? fileManager.removeItem(atPath: path)
  59. }
  60. }
  61. /// 获取下载的总数量
  62. /// - Returns: <#description#>
  63. class func downloadTotalFile() -> [String]? {
  64. let fileManager = FileManager.default
  65. let subpaths = fileManager.subpaths(atPath: downloadDirectory)
  66. BFLog(message: "已下载的总文件 == \(subpaths ?? [])")
  67. return subpaths
  68. }
  69. }