LivePhotoSource.swift 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. /// A type represents a loadable resource for a Live Photo, which consists of a still image and a video.
  28. ///
  29. /// Kingfisher expects a ``LivePhotoSource`` value to load a Live Photo with its high-level APIs.
  30. /// A ``LivePhotoSource`` is typically a collection of two ``LivePhotoResource`` values, one for the still image and
  31. /// one for the video.
  32. public struct LivePhotoSource: Sendable {
  33. /// The resources of a Live Photo.
  34. public let resources: [LivePhotoResource]
  35. /// Creates a Live Photo source with given resources.
  36. /// - Parameter resources: The downloadable resource for a Live Photo. It should contain two resources, one for the
  37. /// still image and one for the video.
  38. public init(resources: [any Resource]) {
  39. let livePhotoResources = resources.map { LivePhotoResource(resource: $0) }
  40. self.init(livePhotoResources)
  41. }
  42. /// Creates a Live Photo source with given URLs.
  43. /// - Parameter urls: The URLs of the downloadable resources for a Live Photo. It should contain two URLs, one for
  44. /// the still image and one for the video.
  45. public init(urls: [URL]) {
  46. let resources = urls.map { KF.ImageResource(downloadURL: $0) }
  47. self.init(resources: resources)
  48. }
  49. /// Creates a Live Photo source with given resources.
  50. /// - Parameter resources: The resources for a Live Photo. It should contain two resources, one for the still image
  51. /// and one for the video.
  52. public init(_ resources: [LivePhotoResource]) {
  53. self.resources = resources
  54. }
  55. }
  56. /// A resource type representing a component of a Live Photo, which consists of a still image and a video.
  57. ///
  58. /// ``LivePhotoResource`` encapsulates the necessary information to download and cache a single components of a Live
  59. /// Photo: it is either a still image (typically in HEIC format) or a video (typically in MOV format). Multiple
  60. /// ``LivePhotoResource`` values (typically two, one for the image and one for the video) can form a ``LivePhotoSource``,
  61. /// which is expected by Kingfisher in its live photo loading high level APIs.
  62. ///
  63. /// The Live Photo data can be retrieved by `PHAssetResourceManager.requestData` method and uploaded to your server.
  64. /// You should not modify the metadata or other information of the data, otherwise, it is possible that the
  65. /// `PHLivePhoto` class cannot read and recognize it anymore. For more information, please refer to Apple's
  66. /// documentation of Photos framework.
  67. public struct LivePhotoResource: Sendable {
  68. /// The file type of a ``LivePhotoResource``.
  69. public enum FileType: Sendable, Equatable {
  70. /// File type HEIC. Usually it represents the still image in a Live Photo.
  71. case heic
  72. /// File type MOV. Usually it represents the video in a Live Photo.
  73. case mov
  74. /// Other file types with the file extension.
  75. case other(String)
  76. var fileExtension: String {
  77. switch self {
  78. case .heic: return "heic"
  79. case .mov: return "mov"
  80. case .other(let ext): return ext
  81. }
  82. }
  83. }
  84. /// The data source of a Live Photo resource.
  85. ///
  86. /// This is a general ``Source`` type, which can be either a network resource (as ``Source/network(_:)``) or a
  87. /// provided resource as ``Source/provider(_:)``.
  88. public let dataSource: Source
  89. /// The file type of the resource.
  90. public let referenceFileType: FileType
  91. var cacheKey: String { dataSource.cacheKey }
  92. var downloadURL: URL? { dataSource.url }
  93. /// Creates a Live Photo resource with given download URL, cache key and file type.
  94. /// - Parameters:
  95. /// - downloadURL: The URL to download the resource.
  96. /// - cacheKey: The cache key for the resource. If `nil`, Kingfisher will use the `absoluteString` of the URL as
  97. /// the cache key.
  98. /// - fileType: The file type of the resource. If `nil`, Kingfisher will try to guess the file type from the URL.
  99. ///
  100. /// The file type is important for Kingfisher to determine how to handle the downloaded data and store them
  101. /// in the cache. Photos framework requires the still image to be in HEIC extension and the video to be in MOV
  102. /// extension. Otherwise, the `PHLivePhoto` class might not be able to recognize the data. If you are not sure about
  103. /// the file type, you can leave it as `nil` and Kingfisher will try to guess it from the URL and the downloaded
  104. /// data.
  105. public init(downloadURL: URL, cacheKey: String? = nil, fileType: FileType? = nil) {
  106. let resource = KF.ImageResource(downloadURL: downloadURL, cacheKey: cacheKey)
  107. dataSource = .network(resource)
  108. referenceFileType = fileType ?? resource.guessedFileType
  109. }
  110. /// Creates a Live Photo resource with given resource and file type.
  111. /// - Parameters:
  112. /// - resource: The resource to download the data.
  113. /// - fileType: The file type of the resource. If `nil`, Kingfisher will try to guess the file type from the URL.
  114. ///
  115. /// The file type is important for Kingfisher to determine how to handle the downloaded data and store them
  116. /// in the cache. Photos framework requires the still image to be in HEIC extension and the video to be in MOV
  117. /// extension. Otherwise, the `PHLivePhoto` class might not be able to recognize the data. If you are not sure about
  118. /// the file type, you can leave it as `nil` and Kingfisher will try to guess it from the URL and the downloaded
  119. /// data.
  120. public init(resource: any Resource, fileType: FileType? = nil) {
  121. self.dataSource = .network(resource)
  122. referenceFileType = fileType ?? resource.guessedFileType
  123. }
  124. /// Creates a Live Photo resource with given data source and file type.
  125. /// - Parameters:
  126. /// - source: The data source of the resource. It can be either a network resource or a provided resource.
  127. /// - fileType: The file type of the resource. If `nil`, Kingfisher will try to guess the file type from the URL.
  128. ///
  129. /// The file type is important for Kingfisher to determine how to handle the downloaded data and store them
  130. /// in the cache. Photos framework requires the still image to be in HEIC extension and the video to be in MOV
  131. /// extension. Otherwise, the `PHLivePhoto` class might not be able to recognize the data. If you are not sure about
  132. /// the file type, you can leave it as `nil` and Kingfisher will try to guess it from the URL and the downloaded
  133. /// data.
  134. public init(source: Source, fileType: FileType? = nil) {
  135. self.dataSource = source
  136. referenceFileType = fileType ?? source.url?.guessedFileType ?? .other("")
  137. }
  138. }
  139. extension LivePhotoResource.FileType {
  140. func determinedFileExtension(_ data: Data) -> String? {
  141. switch self {
  142. case .mov: return "mov"
  143. case .heic: return "heic"
  144. case .other(let ext):
  145. if !ext.isEmpty {
  146. return ext
  147. }
  148. return Self.guessedFileExtension(from: data)
  149. }
  150. }
  151. static let fytpChunk: [UInt8] = [0x66, 0x74, 0x79, 0x70] // fytp (file type box)
  152. static let heicChunk: [UInt8] = [0x68, 0x65, 0x69, 0x63] // .heic
  153. static let qtChunk: [UInt8] = [0x71, 0x74, 0x20, 0x20] // quicktime, .mov
  154. static func guessedFileExtension(from data: Data) -> String? {
  155. guard data.count >= 12 else { return nil }
  156. var buffer = [UInt8](repeating: 0, count: 12)
  157. data.copyBytes(to: &buffer, count: 12)
  158. guard Array(buffer[4..<8]) == fytpChunk else {
  159. return nil
  160. }
  161. let fileTypeChunk = Array(buffer[8..<12])
  162. if fileTypeChunk == heicChunk {
  163. return "heic"
  164. }
  165. if fileTypeChunk == qtChunk {
  166. return "mov"
  167. }
  168. return nil
  169. }
  170. }
  171. extension Resource {
  172. var guessedFileType: LivePhotoResource.FileType {
  173. let pathExtension = downloadURL.pathExtension.lowercased()
  174. return switch pathExtension {
  175. case "mov": .mov
  176. case "heic": .heic
  177. default: .other(pathExtension)
  178. }
  179. }
  180. }