PQDownloadModel.swift 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. //
  2. // PQDownloadModel.swift
  3. // PQSpeed
  4. //
  5. // Created by SanW on 2020/9/10.
  6. // Copyright © 2020 BytesFlow. All rights reserved.
  7. //
  8. import UIKit
  9. import BFNetRequestKit
  10. // MARK: - 文件扩展名
  11. /// 文件扩展名
  12. public enum FileExtensionType: String {
  13. case normal = "" // 无后缀名
  14. case png // png
  15. case jpg // jpg
  16. case gif // gif
  17. case pdf // pdf
  18. case mp4 // mp4
  19. case mp3 // mp3
  20. case m4a // m4a
  21. case txt // txt
  22. case zip // zip
  23. /// 文件类型
  24. /// - Returns: <#description#>
  25. public func fileType() -> String {
  26. var fileType: String = "unknow"
  27. switch self {
  28. case .png, .jpg:
  29. fileType = "image"
  30. case .gif:
  31. fileType = "gif"
  32. case .mp3, .m4a:
  33. fileType = "voice"
  34. case .mp4:
  35. fileType = "video"
  36. default:
  37. fileType = "unknow"
  38. }
  39. return fileType
  40. }
  41. }
  42. // MARK: - 下载model
  43. /// 下载model
  44. public class PQDownloadModel: NSObject {
  45. public var name: String? // 资源名
  46. public var fileExtensionType: FileExtensionType? // 文件后缀类型
  47. public var realFileExtensionType: FileExtensionType? // 真实文件后缀类型
  48. public var mimeType: String? { // 媒体类型
  49. didSet {
  50. // if mimeType?.contains("jpeg") ?? false { // image/jpeg
  51. // realFileExtensionType = .jpg
  52. // }else if mimeType?.contains("gif") ?? false { // image/gif
  53. // realFileExtensionType = .gif
  54. // }else if mimeType?.contains("pdf") ?? false{ // application/pdf
  55. // realFileExtensionType = .pdf
  56. // }else if mimeType?.contains("mp4") ?? false{ // video/mp4
  57. // realFileExtensionType = .mp4
  58. // }else if mimeType?.contains("mp3") ?? false{ // audio/mpeg
  59. // realFileExtensionType = .mp3
  60. // }else if mimeType?.contains("m4a") ?? false { // audio/x-m4a/audio/m4a
  61. // realFileExtensionType = .m4a
  62. // }
  63. if mimeType?.contains("m4a") ?? false { // audio/x-m4a,audio/m4a
  64. realFileExtensionType = .m4a
  65. }
  66. }
  67. }
  68. public var sourceURL: String? // 源地址
  69. public var filePath: String? // 下载到本地地址
  70. public var imageURL: String? // 图片地址
  71. public var totalLength: Int64? // 总大小
  72. public var downloadLength: Int64? // 已下载大小
  73. public var task: URLSessionDataTask? // 下载任务
  74. public var progress: Float? // 下载进度
  75. public var state: downloadState? // 下载状态
  76. public var progressHandle: ProgressHandle? // 进度的回调
  77. public var stateHandle: StateHandle? // 状态回调
  78. public var fileHandle: FileHandle? // 文件句柄
  79. }
  80. // MARK: - 下载状态
  81. /// 下载状态
  82. public enum downloadState: Int {
  83. case downloading = 0 // 下载中
  84. case compelte = 1 // 下载完成
  85. case error = 2 // 下载失败
  86. case pause = 3 // 暂停下载
  87. case cancel = 4 // 取消下载
  88. }
  89. // 进度的回调
  90. public typealias ProgressHandle = (_ progress: Float, _ downloadLength: Int64?, _ totalLength: Int64?) -> Void
  91. // 状态回调
  92. public typealias StateHandle = (_ state: downloadState, _ url: String, _ localPath: String?, _ error: PQError?) -> Void