Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LocalFileImageDataProvider.data #1989

Merged
merged 2 commits into from
Oct 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 16 additions & 16 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,22 @@ GEM
specs:
CFPropertyList (3.0.5)
rexml
activesupport (6.1.6.1)
activesupport (6.1.7)
concurrent-ruby (~> 1.0, >= 1.0.2)
i18n (>= 1.6, < 2)
minitest (>= 5.1)
tzinfo (~> 2.0)
zeitwerk (~> 2.3)
addressable (2.8.0)
public_suffix (>= 2.0.2, < 5.0)
addressable (2.8.1)
public_suffix (>= 2.0.2, < 6.0)
algoliasearch (1.27.5)
httpclient (~> 2.8, >= 2.8.3)
json (>= 1.5.1)
artifactory (3.0.15)
atomos (0.1.3)
aws-eventstream (1.2.0)
aws-partitions (1.616.0)
aws-sdk-core (3.132.0)
aws-partitions (1.633.0)
aws-sdk-core (3.151.0)
aws-eventstream (~> 1, >= 1.0.2)
aws-partitions (~> 1, >= 1.525.0)
aws-sigv4 (~> 1.1)
Expand Down Expand Up @@ -87,7 +87,7 @@ GEM
ethon (0.15.0)
ffi (>= 1.15.0)
excon (0.92.4)
faraday (1.10.1)
faraday (1.10.2)
faraday-em_http (~> 1.0)
faraday-em_synchrony (~> 1.0)
faraday-excon (~> 1.1)
Expand Down Expand Up @@ -116,7 +116,7 @@ GEM
faraday_middleware (1.2.0)
faraday (~> 1.0)
fastimage (2.2.6)
fastlane (2.208.0)
fastlane (2.210.0)
CFPropertyList (>= 2.3, < 4.0.0)
addressable (>= 2.8, < 3.0.0)
artifactory (~> 3.0)
Expand Down Expand Up @@ -159,9 +159,9 @@ GEM
fourflusher (2.3.1)
fuzzy_match (2.0.4)
gh_inspector (1.1.3)
google-apis-androidpublisher_v3 (0.25.0)
google-apis-core (>= 0.7, < 2.a)
google-apis-core (0.7.0)
google-apis-androidpublisher_v3 (0.27.0)
google-apis-core (>= 0.7.2, < 2.a)
google-apis-core (0.9.0)
addressable (~> 2.5, >= 2.5.1)
googleauth (>= 0.16.2, < 2.a)
httpclient (>= 2.8.1, < 3.a)
Expand All @@ -170,8 +170,8 @@ GEM
retriable (>= 2.0, < 4.a)
rexml
webrick
google-apis-iamcredentials_v1 (0.13.0)
google-apis-core (>= 0.7, < 2.a)
google-apis-iamcredentials_v1 (0.14.0)
google-apis-core (>= 0.7.2, < 2.a)
google-apis-playcustomapp_v1 (0.10.0)
google-apis-core (>= 0.7, < 2.a)
google-apis-storage_v1 (0.17.0)
Expand All @@ -181,8 +181,8 @@ GEM
google-cloud-errors (~> 1.0)
google-cloud-env (1.6.0)
faraday (>= 0.17.3, < 3.0)
google-cloud-errors (1.2.0)
google-cloud-storage (1.38.0)
google-cloud-errors (1.3.0)
google-cloud-storage (1.41.0)
addressable (~> 2.8)
digest-crc (~> 0.4)
google-apis-iamcredentials_v1 (~> 0.1)
Expand All @@ -205,11 +205,11 @@ GEM
concurrent-ruby (~> 1.0)
jmespath (1.6.1)
json (2.6.2)
jwt (2.4.1)
jwt (2.5.0)
memoist (0.16.2)
mini_magick (4.11.0)
mini_mime (1.1.2)
minitest (5.16.2)
minitest (5.16.3)
molinillo (0.8.0)
multi_json (1.15.0)
multipart-post (2.0.0)
Expand Down
20 changes: 20 additions & 0 deletions Sources/General/ImageSource/ImageDataProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,26 @@ public struct LocalFileImageDataProvider: ImageDataProvider {
handler(Result(catching: { try Data(contentsOf: fileURL) }))
}
}

#if swift(>=5.5)
#if canImport(_Concurrency)
@available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.0, *)
public var data: Data {
get async throws {
try await withCheckedThrowingContinuation { continuation in
loadingQueue.execute {
do {
let data = try Data(contentsOf: fileURL)
continuation.resume(returning: data)
} catch {
continuation.resume(throwing: error)
}
}
}
}
}
#endif
#endif

/// The URL of the local file on the disk.
public var contentURL: URL? {
Expand Down
46 changes: 46 additions & 0 deletions Tests/KingfisherTests/ImageDataProviderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,28 @@ class ImageDataProviderTests: XCTestCase {
waitForExpectations(timeout: 1, handler: nil)
}

#if swift(>=5.5)
#if canImport(_Concurrency)
@available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.0, *)
func testLocalFileImageDataProviderAsync() async {
let fm = FileManager.default
let document = try! fm.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
let fileURL = document.appendingPathComponent("test")
try! testImageData.write(to: fileURL)

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

XCTAssertEqual(provider.fileURL, fileURL)

let value = try? await provider.data
XCTAssertEqual(value, testImageData)
try! fm.removeItem(at: fileURL)
}
#endif
#endif

func testLocalFileImageDataProviderMainQueue() {
let fm = FileManager.default
let document = try! fm.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
Expand All @@ -71,6 +93,30 @@ class ImageDataProviderTests: XCTestCase {
XCTAssertTrue(called)
}

#if swift(>=5.5)
#if canImport(_Concurrency)
@available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.0, *)
func testLocalFileImageDataProviderMainQueueAsync() async {
let fm = FileManager.default
let document = try! fm.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
let fileURL = document.appendingPathComponent("test")
try! testImageData.write(to: fileURL)

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

var called = false
let value = try? await provider.data
XCTAssertEqual(value, testImageData)
try! fm.removeItem(at: fileURL)
called = true

XCTAssertTrue(called)
}
#endif
#endif

func testLocalFileCacheKey() {
let url1 = URL(string: "file:///Users/onevcat/Library/Developer/CoreSimulator/Devices/ABC/data/Containers/Bundle/Application/DEF/Kingfisher-Demo.app/images/kingfisher-1.jpg")!
XCTAssertEqual(url1.localFileCacheKey, "\(URL.localFileCacheKeyPrefix)/Kingfisher-Demo.app/images/kingfisher-1.jpg")
Expand Down