Skip to content

Commit

Permalink
Implement getting and deleting cached thumbnails
Browse files Browse the repository at this point in the history
  • Loading branch information
skjiisa committed Apr 16, 2021
1 parent a3a59c5 commit 7ac0be6
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 20 deletions.
13 changes: 13 additions & 0 deletions Sources/WebDAV/WebDAV+Images.swift
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,14 @@ public extension WebDAV {

//MARK: Thumbnail Cache

func getAllCachedThumbnails<A: WebDAVAccount>(forItemAtPath path: String, account: A) -> [ThumbnailProperties: UIImage]? {
getCachedValue(cache: thumbnailCache, forItemAtPath: path, account: account)
}

func getCachedThumbnail<A: WebDAVAccount>(forItemAtPath path: String, account: A, with properties: ThumbnailProperties) -> UIImage? {
getAllCachedThumbnails(forItemAtPath: path, account: account)?[properties]
}

func deleteCachedThumbnail<A: WebDAVAccount>(forItemAtPath path: String, account: A, with properties: ThumbnailProperties) throws {
let accountPath = AccountPath(account: account, path: path)
if var cachedThumbnails = thumbnailCache[accountPath] {
Expand All @@ -199,6 +207,11 @@ public extension WebDAV {
}
}

func deleteAllCachedThumbnails<A: WebDAVAccount>(forItemAtPath path: String, account: A) throws {
let accountPath = AccountPath(account: account, path: path)
thumbnailCache.removeValue(forKey: accountPath)
}

}

//MARK: Internal
Expand Down
46 changes: 26 additions & 20 deletions Tests/WebDAVTests/WebDAVTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,9 @@ final class WebDAVTests: XCTestCase {

//MARK: Image Cache

// Commented out lines are lines that existed to test image cache in v2.x versions.
// They will be added again when disk cache is reimplemented in v3.0.

func testDownloadImage() {
guard let (account, password) = getAccount() else { return XCTFail() }
guard let imagePath = ProcessInfo.processInfo.environment["image_path"] else {
Expand All @@ -375,7 +378,9 @@ final class WebDAVTests: XCTestCase {
// let cachedImageURL = try webDAV.getCachedDataURL(forItemAtPath: imagePath, account: account)!
// XCTAssert(FileManager.default.fileExists(atPath: cachedImageURL.path))
XCTAssertNotNil(webDAV.getCachedImage(forItemAtPath: imagePath, account: account))

try webDAV.deleteCachedData(forItemAtPath: imagePath, account: account)
XCTAssertNil(webDAV.getCachedImage(forItemAtPath: imagePath, account: account))
// XCTAssertFalse(FileManager.default.fileExists(atPath: cachedImageURL.path))
}

Expand Down Expand Up @@ -410,20 +415,21 @@ final class WebDAVTests: XCTestCase {
XCTAssertNoThrow(try webDAV.deleteCachedThumbnail(forItemAtPath: imagePath, account: account, with: .fill))
}

/*
func testSpecificThumbnailCache() throws {
guard let (account, password) = getAccount() else { return XCTFail() }
guard let imagePath = ProcessInfo.processInfo.environment["image_path"] else {
return XCTFail("You need to set the image_path in the environment.")
}

downloadThumbnail(imagePath: imagePath, account: account, password: password)
downloadThumbnail(imagePath: imagePath, account: account, password: password, with: .fill)

let cachedThumbnailURL = try webDAV.getCachedThumbnailURL(forItemAtPath: imagePath, account: account, with: nil, aspectFill: false)!
XCTAssertTrue(FileManager.default.fileExists(atPath: cachedThumbnailURL.path))
XCTAssertNotNil(webDAV.getCachedThumbnail(forItemAtPath: imagePath, account: account, with: nil, aspectFill: false))
try webDAV.deleteCachedThumbnail(forItemAtPath: imagePath, account: account, with: nil, aspectFill: false)
XCTAssertFalse(FileManager.default.fileExists(atPath: cachedThumbnailURL.path))
// let cachedThumbnailURL = try webDAV.getCachedThumbnailURL(forItemAtPath: imagePath, account: account, with: nil, aspectFill: false)!
// XCTAssertTrue(FileManager.default.fileExists(atPath: cachedThumbnailURL.path))
XCTAssertNotNil(webDAV.getCachedThumbnail(forItemAtPath: imagePath, account: account, with: .fill))

try webDAV.deleteCachedThumbnail(forItemAtPath: imagePath, account: account, with: .fill)
XCTAssertNil(webDAV.getCachedThumbnail(forItemAtPath: imagePath, account: account, with: .fill))
// XCTAssertFalse(FileManager.default.fileExists(atPath: cachedThumbnailURL.path))
}

func testGeneralThumbnailCache() throws {
Expand All @@ -432,22 +438,22 @@ final class WebDAVTests: XCTestCase {
return XCTFail("You need to set the image_path in the environment.")
}

downloadThumbnail(imagePath: imagePath, account: account, password: password, with: nil, aspectFill: true)
downloadThumbnail(imagePath: imagePath, account: account, password: password, with: nil, aspectFill: false)
downloadThumbnail(imagePath: imagePath, account: account, password: password, with: .fill)
downloadThumbnail(imagePath: imagePath, account: account, password: password, with: .fit)

let cachedThumbnailFillURL = try webDAV.getCachedThumbnailURL(forItemAtPath: imagePath, account: account, with: nil, aspectFill: true)!
let cachedThumbnailFitURL = try webDAV.getCachedThumbnailURL(forItemAtPath: imagePath, account: account, with: nil, aspectFill: false)!
// let cachedThumbnailFillURL = try webDAV.getCachedThumbnailURL(forItemAtPath: imagePath, account: account, with: nil, aspectFill: true)!
// let cachedThumbnailFitURL = try webDAV.getCachedThumbnailURL(forItemAtPath: imagePath, account: account, with: nil, aspectFill: false)!

XCTAssert(FileManager.default.fileExists(atPath: cachedThumbnailFillURL.path))
XCTAssert(FileManager.default.fileExists(atPath: cachedThumbnailFitURL.path))
XCTAssertEqual(try webDAV.getAllCachedThumbnails(forItemAtPath: imagePath, account: account).count, 2)
// XCTAssert(FileManager.default.fileExists(atPath: cachedThumbnailFillURL.path))
// XCTAssert(FileManager.default.fileExists(atPath: cachedThumbnailFitURL.path))
XCTAssertEqual(webDAV.getAllCachedThumbnails(forItemAtPath: imagePath, account: account)?.count, 2)

// Delete all cached thumbnails and check that they're both gone
try webDAV.deleteAllCachedThumbnails(forItemAtPath: imagePath, account: account)
XCTAssertFalse(FileManager.default.fileExists(atPath: cachedThumbnailFillURL.path))
XCTAssertFalse(FileManager.default.fileExists(atPath: cachedThumbnailFitURL.path))
XCTAssertNil(webDAV.getAllCachedThumbnails(forItemAtPath: imagePath, account: account))
// XCTAssertFalse(FileManager.default.fileExists(atPath: cachedThumbnailFillURL.path))
// XCTAssertFalse(FileManager.default.fileExists(atPath: cachedThumbnailFitURL.path))
}
*/

//MARK: OCS

Expand Down Expand Up @@ -571,12 +577,12 @@ final class WebDAVTests: XCTestCase {
wait(for: [expectation], timeout: 10.0)
}

private func downloadThumbnail(imagePath: String, account: SimpleAccount, password: String, with dimensions: CGSize? = nil, aspectFill: Bool = false) {
private func downloadThumbnail(imagePath: String, account: SimpleAccount, password: String, with properties: ThumbnailProperties = .fill) {
let expectation = XCTestExpectation(description: "Download thumbnail from WebDAV")

try? webDAV.deleteCachedThumbnail(forItemAtPath: imagePath, account: account, with: .fill)
try? webDAV.deleteCachedThumbnail(forItemAtPath: imagePath, account: account, with: properties)

webDAV.downloadThumbnail(path: imagePath, account: account, password: password, with: .fill) { image, error in
webDAV.downloadThumbnail(path: imagePath, account: account, password: password, with: properties) { image, error in
XCTAssertNil(error)
XCTAssertNotNil(image)

Expand Down

0 comments on commit 7ac0be6

Please sign in to comment.