LivePhotoSource.swift 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 resource type representing a component of a Live Photo, which consists of a still image and a video.
  28. ///
  29. /// ``LivePhotoResource`` encapsulates the necessary information to download and cache a single components of a Live
  30. /// Photo: it is either a still image (typically in HEIC format) or a video (typically in MOV format). Multiple
  31. /// ``LivePhotoResource`` values (typically two, one for the image and one for the video) can form a ``LivePhotoSource``,
  32. /// which is expected by Kingfisher in its live photo loading high level APIs.
  33. ///
  34. /// The Live Photo data can be retrieved by `PHAssetResourceManager.requestData` method and uploaded to your server.
  35. /// You should not modify the metadata or other information of the data, otherwise, it is possible that the
  36. /// `PHLivePhoto` class cannot read and recognize it anymore. For more information, please refer to Apple's
  37. /// documentation of Photos framework.
  38. public struct LivePhotoResource: Sendable {
  39. /// The file type of a ``LivePhotoResource``.
  40. public enum FileType: Sendable, Equatable {
  41. /// File type HEIC. Usually it represents the still image in a Live Photo.
  42. case heic
  43. /// File type MOV. Usually it represents the video in a Live Photo.
  44. case mov
  45. /// Other file types with the file extension.
  46. case other(String)
  47. var fileExtension: String {
  48. switch self {
  49. case .heic: return "heic"
  50. case .mov: return "mov"
  51. case .other(let ext): return ext
  52. }
  53. }
  54. }
  55. public let dataSource: Source
  56. public let referenceFileType: FileType
  57. var cacheKey: String { dataSource.cacheKey }
  58. var downloadURL: URL? { dataSource.url }
  59. public init(downloadURL: URL, cacheKey: String? = nil, fileType: FileType? = nil) {
  60. let resource = KF.ImageResource(downloadURL: downloadURL, cacheKey: cacheKey)
  61. dataSource = .network(resource)
  62. referenceFileType = fileType ?? resource.guessedFileType
  63. }
  64. public init(resource: any Resource, fileType: FileType? = nil) {
  65. self.dataSource = .network(resource)
  66. referenceFileType = fileType ?? resource.guessedFileType
  67. }
  68. }
  69. extension LivePhotoResource.FileType {
  70. func determinedFileExtension(_ data: Data) -> String? {
  71. switch self {
  72. case .mov: return "mov"
  73. case .heic: return "heic"
  74. case .other(let ext):
  75. if !ext.isEmpty {
  76. return ext
  77. }
  78. return Self.guessedFileExtension(from: data)
  79. }
  80. }
  81. static let fytpChunk: [UInt8] = [0x66, 0x74, 0x79, 0x70]
  82. static let heicChunk: [UInt8] = [0x68, 0x65, 0x69, 0x63]
  83. static let qtChunk: [UInt8] = [0x71, 0x74, 0x20, 0x20] // quicktime
  84. static func guessedFileExtension(from data: Data) -> String? {
  85. guard data.count >= 12 else { return nil }
  86. var buffer = [UInt8](repeating: 0, count: 12)
  87. data.copyBytes(to: &buffer, count: 12)
  88. guard Array(buffer[4..<8]) == fytpChunk else {
  89. return nil
  90. }
  91. let fileTypeChunk = Array(buffer[8..<12])
  92. if fileTypeChunk == heicChunk {
  93. return "heic"
  94. }
  95. if fileTypeChunk == qtChunk {
  96. return "mov"
  97. }
  98. return nil
  99. }
  100. }
  101. extension Resource {
  102. var guessedFileType: LivePhotoResource.FileType {
  103. let pathExtension = downloadURL.pathExtension.lowercased()
  104. return switch pathExtension {
  105. case "mov": .mov
  106. case "heic": .heic
  107. default: .other(pathExtension)
  108. }
  109. }
  110. }
  111. public struct LivePhotoSource: Sendable {
  112. public let resources: [LivePhotoResource]
  113. public init(resources: [any Resource]) {
  114. let livePhotoResources = resources.map { LivePhotoResource(resource: $0) }
  115. self.init(livePhotoResources)
  116. }
  117. public init(urls: [URL]) {
  118. let resources = urls.map { KF.ImageResource(downloadURL: $0) }
  119. self.init(resources: resources)
  120. }
  121. public init(_ resources: [LivePhotoResource]) {
  122. self.resources = resources
  123. }
  124. }