Pārlūkot izejas kodu

Support using data provider to get live photo data

onevcat 1 gadu atpakaļ
vecāks
revīzija
a51a4cc721

+ 8 - 0
Sources/General/ImageSource/ImageDataProvider.swift

@@ -52,6 +52,14 @@ public protocol ImageDataProvider: Sendable {
     var contentURL: URL? { get }
     var contentURL: URL? { get }
 }
 }
 
 
+extension ImageDataProvider {
+    func data() async throws -> Data {
+        try await withCheckedThrowingContinuation { continuation in
+            data(handler: { continuation.resume(with: $0) })
+        }
+    }
+}
+
 public extension ImageDataProvider {
 public extension ImageDataProvider {
     var contentURL: URL? { return nil }
     var contentURL: URL? { return nil }
     func convertToSource() -> Source {
     func convertToSource() -> Source {

+ 6 - 5
Sources/General/ImageSource/LivePhotoSource.swift

@@ -57,19 +57,20 @@ public struct LivePhotoResource: Sendable {
         }
         }
     }
     }
     
     
-    public let resource: any Resource
+    public let dataSource: Source
     public let referenceFileType: FileType
     public let referenceFileType: FileType
     
     
-    var cacheKey: String { resource.cacheKey }
-    var downloadURL: URL { resource.downloadURL }
+    var cacheKey: String { dataSource.cacheKey }
+    var downloadURL: URL? { dataSource.url }
     
     
     public init(downloadURL: URL, cacheKey: String? = nil, fileType: FileType? = nil) {
     public init(downloadURL: URL, cacheKey: String? = nil, fileType: FileType? = nil) {
-        resource = KF.ImageResource(downloadURL: downloadURL, cacheKey: cacheKey)
+        let resource = KF.ImageResource(downloadURL: downloadURL, cacheKey: cacheKey)
+        dataSource = .network(resource)
         referenceFileType = fileType ?? resource.guessedFileType
         referenceFileType = fileType ?? resource.guessedFileType
     }
     }
     
     
     public init(resource: any Resource, fileType: FileType? = nil) {
     public init(resource: any Resource, fileType: FileType? = nil) {
-        self.resource = resource
+        self.dataSource = .network(resource)
         referenceFileType = fileType ?? resource.guessedFileType
         referenceFileType = fileType ?? resource.guessedFileType
     }
     }
 }
 }

+ 1 - 1
Sources/General/KingfisherError.swift

@@ -578,7 +578,7 @@ extension KingfisherError.CacheErrorReason {
             return "The disk storage is not ready to use yet at URL: '\(cacheURL)'. " +
             return "The disk storage is not ready to use yet at URL: '\(cacheURL)'. " +
                 "This is usually caused by extremely lack of disk space. Ask users to free up some space and restart the app."
                 "This is usually caused by extremely lack of disk space. Ask users to free up some space and restart the app."
         case .missingLivePhotoResourceOnDisk(let resource):
         case .missingLivePhotoResourceOnDisk(let resource):
-            return "The live photo resource '\(resource.downloadURL)' is missing in the cache. Usually a re-download" +
+            return "The live photo resource '\(resource)' is missing in the cache. Usually a re-download" +
             " can fix this issue."
             " can fix this issue."
         }
         }
     }
     }

+ 16 - 5
Sources/General/KingfisherManager+LivePhoto.swift

@@ -173,11 +173,22 @@ extension KingfisherManager {
             
             
             for resource in resources {
             for resource in resources {
                 group.addTask {
                 group.addTask {
-                    let downloadedResource = try await downloader.downloadLivePhotoResource(
-                        with: resource.downloadURL,
-                        options: options
-                    )
-
+                    
+                    let downloadedResource: LivePhotoResourceDownloadingResult
+                    
+                    switch resource.dataSource {
+                    case .network(let urlResource):
+                        downloadedResource = try await downloader.downloadLivePhotoResource(
+                            with: urlResource.downloadURL,
+                            options: options
+                        )
+                    case .provider(let provider):
+                        downloadedResource = try await LivePhotoResourceDownloadingResult(
+                            originalData: provider.data(),
+                            url: provider.contentURL
+                        )
+                    }
+                     
                     // We need to specify the extension so the file is saved correctly. Live photo loading requires
                     // We need to specify the extension so the file is saved correctly. Live photo loading requires
                     // the file extension to be correct. Otherwise, a 3302 error will be thrown.
                     // the file extension to be correct. Otherwise, a 3302 error will be thrown.
                     // https://developer.apple.com/documentation/photokit/phphotoserror/code/invalidresource
                     // https://developer.apple.com/documentation/photokit/phphotoserror/code/invalidresource