LivePhotoSource.swift 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. //
  2. // LivePhotoSource.swift
  3. // Kingfisher
  4. //
  5. // Created by onevcat on 2024/10/01.
  6. //
  7. // Copyright (c) 2024 Wei Wang <onevcat@gmail.com>
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining a copy
  10. // of this software and associated documentation files (the "Software"), to deal
  11. // in the Software without restriction, including without limitation the rights
  12. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. // copies of the Software, and to permit persons to whom the Software is
  14. // furnished to do so, subject to the following conditions:
  15. //
  16. // The above copyright notice and this permission notice shall be included in
  17. // all copies or substantial portions of the Software.
  18. //
  19. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. // THE SOFTWARE.
  26. import Foundation
  27. public struct LivePhotoResource: Sendable {
  28. public enum FileType: Sendable, Equatable {
  29. case heic
  30. case mov
  31. case other(String)
  32. var fileExtension: String {
  33. switch self {
  34. case .heic: return "heic"
  35. case .mov: return "mov"
  36. case .other(let ext): return ext
  37. }
  38. }
  39. }
  40. public let resource: any Resource
  41. public let referenceFileType: FileType
  42. var cacheKey: String { resource.cacheKey }
  43. var downloadURL: URL { resource.downloadURL }
  44. public init(downloadURL: URL, cacheKey: String? = nil, fileType: FileType? = nil) {
  45. resource = KF.ImageResource(downloadURL: downloadURL, cacheKey: cacheKey)
  46. referenceFileType = fileType ?? resource.guessedFileType
  47. }
  48. public init(resource: any Resource, fileType: FileType? = nil) {
  49. self.resource = resource
  50. referenceFileType = fileType ?? resource.guessedFileType
  51. }
  52. }
  53. extension LivePhotoResource.FileType {
  54. func determinedFileExtension(_ data: Data) -> String? {
  55. switch self {
  56. case .mov: return "mov"
  57. case .heic: return "heic"
  58. case .other(let ext):
  59. if !ext.isEmpty {
  60. return ext
  61. }
  62. return Self.guessedFileExtension(from: data)
  63. }
  64. }
  65. static let fytpChunk: [UInt8] = [0x66, 0x74, 0x79, 0x70]
  66. static let heicChunk: [UInt8] = [0x68, 0x65, 0x69, 0x63]
  67. static let qtChunk: [UInt8] = [0x71, 0x74, 0x20, 0x20] // quicktime
  68. static func guessedFileExtension(from data: Data) -> String? {
  69. guard data.count >= 12 else { return nil }
  70. var buffer = [UInt8](repeating: 0, count: 12)
  71. data.copyBytes(to: &buffer, count: 12)
  72. guard Array(buffer[4..<8]) == fytpChunk else {
  73. return nil
  74. }
  75. let fileTypeChunk = Array(buffer[8..<12])
  76. if fileTypeChunk == heicChunk {
  77. return "heic"
  78. }
  79. if fileTypeChunk == qtChunk {
  80. return "mov"
  81. }
  82. return nil
  83. }
  84. }
  85. extension Resource {
  86. var guessedFileType: LivePhotoResource.FileType {
  87. let pathExtension = downloadURL.pathExtension.lowercased()
  88. return switch pathExtension {
  89. case "mov": .mov
  90. case "heic": .heic
  91. default: .other(pathExtension)
  92. }
  93. }
  94. }
  95. public struct LivePhotoSource: Sendable {
  96. public let resources: [LivePhotoResource]
  97. public init(resources: [any Resource]) {
  98. let livePhotoResources = resources.map { LivePhotoResource(resource: $0) }
  99. self.init(livePhotoResources)
  100. }
  101. public init(urls: [URL]) {
  102. let resources = urls.map { KF.ImageResource(downloadURL: $0) }
  103. self.init(resources: resources)
  104. }
  105. public init(_ resources: [LivePhotoResource]) {
  106. self.resources = resources
  107. }
  108. }