LivePhotoSource.swift 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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 {
  29. case heic
  30. case mov
  31. }
  32. public let resource: any Resource
  33. public let referenceFileType: FileType
  34. var cacheKey: String { resource.cacheKey }
  35. var downloadURL: URL { resource.downloadURL }
  36. public init(downloadURL: URL, cacheKey: String? = nil, fileType: FileType? = nil) {
  37. resource = KF.ImageResource(downloadURL: downloadURL, cacheKey: cacheKey)
  38. referenceFileType = fileType ?? resource.guessedFileType
  39. }
  40. public init(resource: any Resource, fileType: FileType? = nil) {
  41. self.resource = resource
  42. referenceFileType = fileType ?? resource.guessedFileType
  43. }
  44. }
  45. extension Resource {
  46. var guessedFileType: LivePhotoResource.FileType {
  47. let pathExtension = downloadURL.pathExtension.lowercased()
  48. switch pathExtension {
  49. case "mov": return .mov
  50. case "heic": return .heic
  51. default:
  52. assertionFailure("Explicit file type is necessary in the download URL as its extension. Otherwise, set the file type of the LivePhoto resource manually with `LivePhotoSource.init(resources:)`.")
  53. return .heic
  54. }
  55. }
  56. }
  57. public struct LivePhotoSource: Sendable {
  58. public let resources: [LivePhotoResource]
  59. public init(resources: [any Resource]) {
  60. let livePhotoResources = resources.map { LivePhotoResource(resource: $0) }
  61. self.init(livePhotoResources)
  62. }
  63. public init(urls: [URL]) {
  64. let resources = urls.map { KF.ImageResource(downloadURL: $0) }
  65. self.init(resources: resources)
  66. }
  67. public init(_ resources: [LivePhotoResource]) {
  68. self.resources = resources
  69. }
  70. }