LivePhotoSource.swift 9.3 KB

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