Fix issue where all cached files would be deleted on cleanup

This commit is contained in:
Isvvc 2021-04-19 19:32:25 -06:00
parent d4d72426b5
commit d005b997b5
2 changed files with 56 additions and 7 deletions

View File

@ -128,13 +128,14 @@ extension WebDAV {
func cleanupDiskCache<A: WebDAVAccount>(at path: String, account: A, files: [WebDAVFile]) throws {
let fm = FileManager.default
guard let url = cachedDataURL(forItemAtPath: path, account: account),fm.fileExists(atPath: url.path) else { return }
// let directory = url.deletingLastPathComponent()
let goodFilePaths = Set(files.compactMap { cachedDataURL(forItemAtPath: $0.path, account: account)?.path })
let infoPlist = filesCacheURL?.path
for item in try fm.contentsOfDirectory(atPath: url.path) where !goodFilePaths.contains(item) && item != infoPlist {
try fm.removeItem(at: url.appendingPathComponent(item))
for path in try fm.contentsOfDirectory(atPath: url.path).map({ url.appendingPathComponent($0).path })
where !goodFilePaths.contains(path)
&& path != infoPlist {
try fm.removeItem(atPath: path)
}
}

View File

@ -351,6 +351,8 @@ final class WebDAVTests: XCTestCase {
wait(for: [expectation], timeout: 10.0)
}
//MARK: Files Cache Cleanup
func testCleanupFilesCacheRoot() {
guard let (account, password) = getAccount() else { return XCTFail() }
@ -379,8 +381,6 @@ final class WebDAVTests: XCTestCase {
XCTAssertNil(webDAV.filesCache[accountPath])
}
//MARK: Cache Cleanup
func testCleanupFilesCacheSubdirectory() {
guard let (account, password) = getAccount() else { return XCTFail() }
@ -411,6 +411,8 @@ final class WebDAVTests: XCTestCase {
XCTAssertNil(webDAV.filesCache[accountPath])
}
//MARK: Disk Cache Cleanup
func testCleanupDiskCacheFile() {
guard let (account, password) = getAccount() else { return XCTFail() }
@ -418,8 +420,7 @@ final class WebDAVTests: XCTestCase {
// Add dummy file to disk cache
let name = UUID().uuidString
let path = name + ".txt"
let path = UUID().uuidString + ".txt"
let data = UUID().uuidString.data(using: .utf8)!
let tempFileURL = webDAV.cachedDataURL(forItemAtPath: path, account: account)!
XCTAssertNoThrow(try webDAV.saveDataToDiskCache(data, url: tempFileURL))
@ -468,6 +469,48 @@ final class WebDAVTests: XCTestCase {
XCTAssertFalse(FileManager.default.fileExists(atPath: tempFileURL.path))
}
func testCleanupDiskCacheWithGoodFile() {
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.")
}
let expectation = XCTestExpectation(description: "List files from WebDAV")
// Download image
let cachedImageURL = webDAV.cachedDataURL(forItemAtPath: imagePath, account: account)!
downloadImage(imagePath: imagePath, account: account, password: password)
XCTAssert(FileManager.default.fileExists(atPath: cachedImageURL.path))
// Add dummy file to same directory as image in disk cache
let imageURL = URL(fileURLWithPath: imagePath, isDirectory: false)
let directory = imageURL.deletingLastPathComponent()
let url = directory.appendingPathComponent(UUID().uuidString)
let data = UUID().uuidString.data(using: .utf8)!
let tempFileURL = webDAV.cachedDataURL(forItemAtPath: url.path, account: account)!
XCTAssertNoThrow(try webDAV.saveDataToDiskCache(data, url: tempFileURL))
XCTAssert(FileManager.default.fileExists(atPath: tempFileURL.path))
// List files
webDAV.listFiles(atPath: directory.path, account: account, password: password, foldersFirst: false, caching: .ignoreCache) { files, error in
XCTAssertNotNil(files)
XCTAssertNil(error)
expectation.fulfill()
}
wait(for: [expectation], timeout: 10.0)
// Check that the fake cached file was cleaned up and the valid cached file still exists
XCTAssert(FileManager.default.fileExists(atPath: cachedImageURL.path))
XCTAssertFalse(FileManager.default.fileExists(atPath: tempFileURL.path))
XCTAssertNoThrow(try webDAV.deleteCachedData(forItemAtPath: imagePath, account: account))
}
//MARK: Image Cache
func testDownloadImage() {
@ -732,8 +775,13 @@ final class WebDAVTests: XCTestCase {
("testFilesReadFromMemoryCache", testFilesReadFromMemoryCache),
("testDiskCache", testDiskCache),
("testFilesCacheDoubleRequest", testFilesCacheDoubleRequest),
// Files Cache Cleanup
("testCleanupFilesCacheRoot", testCleanupFilesCacheRoot),
("testCleanupFilesCacheSubdirectory", testCleanupFilesCacheSubdirectory),
// Disk Cache Cleanup
("testCleanupDiskCacheFile", testCleanupDiskCacheFile),
("testCleanupDiskCacheFolder", testCleanupDiskCacheFolder),
("testCleanupDiskCacheWithGoodFile", testCleanupDiskCacheWithGoodFile),
// Image Cache
("testDownloadImage", testDownloadImage),
("testImageCache", testImageCache),