Skip to content

Commit

Permalink
Give correct cache key for local urls
Browse files Browse the repository at this point in the history
  • Loading branch information
onevcat committed Mar 1, 2022
1 parent cbbeb66 commit 5624b15
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 30 deletions.
28 changes: 0 additions & 28 deletions Sources/General/ImageSource/ImageDataProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -109,34 +109,6 @@ public struct LocalFileImageDataProvider: ImageDataProvider {
}
}

extension URL {
static let localFileCacheKeyPrefix = "kingfisher.local.cacheKey"

// The special version of cache key for a local file on disk. Every time the app is reinstalled on the disk,
// the system assigns a new container folder to hold the .app (and the extensions, .appex) folder. So the URL for
// the same image in bundle might be different.
//
// This getter only uses the fixed part in the URL (until the bundle name folder) to provide a stable cache key
// for the image under the same path inside the bundle.
//
// See #1825 (https://github.com/onevcat/Kingfisher/issues/1825)
var localFileCacheKey: String {
var validComponents: [String] = []
for part in pathComponents.reversed() {
validComponents.append(part)
if part.hasSuffix(".app") || part.hasSuffix(".appex") {
break
}
}
let fixedPath = "\(Self.localFileCacheKeyPrefix)/\(validComponents.reversed().joined(separator: "/"))"
if let q = query {
return "\(fixedPath)?\(q)"
} else {
return fixedPath
}
}
}

/// Represents an image data provider for loading image from a given Base64 encoded string.
public struct Base64ImageDataProvider: ImageDataProvider {

Expand Down
32 changes: 30 additions & 2 deletions Sources/General/ImageSource/Resource.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public struct ImageResource: Resource {
/// Default is `nil`.
public init(downloadURL: URL, cacheKey: String? = nil) {
self.downloadURL = downloadURL
self.cacheKey = cacheKey ?? (downloadURL.isFileURL ? downloadURL.localFileCacheKey : downloadURL.absoluteString)
self.cacheKey = cacheKey ?? downloadURL.cacheKey
}

// MARK: Protocol Conforming
Expand All @@ -82,6 +82,34 @@ public struct ImageResource: Resource {
/// The `absoluteString` of this URL is used as `cacheKey`. And the URL itself will be used as `downloadURL`.
/// If you need customize the url and/or cache key, use `ImageResource` instead.
extension URL: Resource {
public var cacheKey: String { return absoluteString }
public var cacheKey: String { return isFileURL ? localFileCacheKey : absoluteString }
public var downloadURL: URL { return self }
}

extension URL {
static let localFileCacheKeyPrefix = "kingfisher.local.cacheKey"

// The special version of cache key for a local file on disk. Every time the app is reinstalled on the disk,
// the system assigns a new container folder to hold the .app (and the extensions, .appex) folder. So the URL for
// the same image in bundle might be different.
//
// This getter only uses the fixed part in the URL (until the bundle name folder) to provide a stable cache key
// for the image under the same path inside the bundle.
//
// See #1825 (https://github.com/onevcat/Kingfisher/issues/1825)
var localFileCacheKey: String {
var validComponents: [String] = []
for part in pathComponents.reversed() {
validComponents.append(part)
if part.hasSuffix(".app") || part.hasSuffix(".appex") {
break
}
}
let fixedPath = "\(Self.localFileCacheKeyPrefix)/\(validComponents.reversed().joined(separator: "/"))"
if let q = query {
return "\(fixedPath)?\(q)"
} else {
return fixedPath
}
}
}
2 changes: 2 additions & 0 deletions Tests/KingfisherTests/ImageDataProviderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ class ImageDataProviderTests: XCTestCase {

let provider = LocalFileImageDataProvider(fileURL: fileURL)
XCTAssertEqual(provider.cacheKey, fileURL.localFileCacheKey)
XCTAssertEqual(fileURL.cacheKey, fileURL.localFileCacheKey)

XCTAssertEqual(provider.fileURL, fileURL)

let exp = expectation(description: #function)
Expand Down

0 comments on commit 5624b15

Please sign in to comment.