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