Fix issue where all cached files would be deleted on cleanup
This commit is contained in:
parent
d4d72426b5
commit
d005b997b5
|
@ -128,13 +128,14 @@ extension WebDAV {
|
||||||
func cleanupDiskCache<A: WebDAVAccount>(at path: String, account: A, files: [WebDAVFile]) throws {
|
func cleanupDiskCache<A: WebDAVAccount>(at path: String, account: A, files: [WebDAVFile]) throws {
|
||||||
let fm = FileManager.default
|
let fm = FileManager.default
|
||||||
guard let url = cachedDataURL(forItemAtPath: path, account: account),fm.fileExists(atPath: url.path) else { return }
|
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 goodFilePaths = Set(files.compactMap { cachedDataURL(forItemAtPath: $0.path, account: account)?.path })
|
||||||
|
|
||||||
let infoPlist = filesCacheURL?.path
|
let infoPlist = filesCacheURL?.path
|
||||||
for item in try fm.contentsOfDirectory(atPath: url.path) where !goodFilePaths.contains(item) && item != infoPlist {
|
for path in try fm.contentsOfDirectory(atPath: url.path).map({ url.appendingPathComponent($0).path })
|
||||||
try fm.removeItem(at: url.appendingPathComponent(item))
|
where !goodFilePaths.contains(path)
|
||||||
|
&& path != infoPlist {
|
||||||
|
try fm.removeItem(atPath: path)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -351,6 +351,8 @@ final class WebDAVTests: XCTestCase {
|
||||||
wait(for: [expectation], timeout: 10.0)
|
wait(for: [expectation], timeout: 10.0)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//MARK: Files Cache Cleanup
|
||||||
|
|
||||||
func testCleanupFilesCacheRoot() {
|
func testCleanupFilesCacheRoot() {
|
||||||
guard let (account, password) = getAccount() else { return XCTFail() }
|
guard let (account, password) = getAccount() else { return XCTFail() }
|
||||||
|
|
||||||
|
@ -379,8 +381,6 @@ final class WebDAVTests: XCTestCase {
|
||||||
XCTAssertNil(webDAV.filesCache[accountPath])
|
XCTAssertNil(webDAV.filesCache[accountPath])
|
||||||
}
|
}
|
||||||
|
|
||||||
//MARK: Cache Cleanup
|
|
||||||
|
|
||||||
func testCleanupFilesCacheSubdirectory() {
|
func testCleanupFilesCacheSubdirectory() {
|
||||||
guard let (account, password) = getAccount() else { return XCTFail() }
|
guard let (account, password) = getAccount() else { return XCTFail() }
|
||||||
|
|
||||||
|
@ -411,6 +411,8 @@ final class WebDAVTests: XCTestCase {
|
||||||
XCTAssertNil(webDAV.filesCache[accountPath])
|
XCTAssertNil(webDAV.filesCache[accountPath])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//MARK: Disk Cache Cleanup
|
||||||
|
|
||||||
func testCleanupDiskCacheFile() {
|
func testCleanupDiskCacheFile() {
|
||||||
guard let (account, password) = getAccount() else { return XCTFail() }
|
guard let (account, password) = getAccount() else { return XCTFail() }
|
||||||
|
|
||||||
|
@ -418,8 +420,7 @@ final class WebDAVTests: XCTestCase {
|
||||||
|
|
||||||
// Add dummy file to disk cache
|
// Add dummy file to disk cache
|
||||||
|
|
||||||
let name = UUID().uuidString
|
let path = UUID().uuidString + ".txt"
|
||||||
let path = name + ".txt"
|
|
||||||
let data = UUID().uuidString.data(using: .utf8)!
|
let data = UUID().uuidString.data(using: .utf8)!
|
||||||
let tempFileURL = webDAV.cachedDataURL(forItemAtPath: path, account: account)!
|
let tempFileURL = webDAV.cachedDataURL(forItemAtPath: path, account: account)!
|
||||||
XCTAssertNoThrow(try webDAV.saveDataToDiskCache(data, url: tempFileURL))
|
XCTAssertNoThrow(try webDAV.saveDataToDiskCache(data, url: tempFileURL))
|
||||||
|
@ -468,6 +469,48 @@ final class WebDAVTests: XCTestCase {
|
||||||
XCTAssertFalse(FileManager.default.fileExists(atPath: tempFileURL.path))
|
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
|
//MARK: Image Cache
|
||||||
|
|
||||||
func testDownloadImage() {
|
func testDownloadImage() {
|
||||||
|
@ -732,8 +775,13 @@ final class WebDAVTests: XCTestCase {
|
||||||
("testFilesReadFromMemoryCache", testFilesReadFromMemoryCache),
|
("testFilesReadFromMemoryCache", testFilesReadFromMemoryCache),
|
||||||
("testDiskCache", testDiskCache),
|
("testDiskCache", testDiskCache),
|
||||||
("testFilesCacheDoubleRequest", testFilesCacheDoubleRequest),
|
("testFilesCacheDoubleRequest", testFilesCacheDoubleRequest),
|
||||||
|
// Files Cache Cleanup
|
||||||
("testCleanupFilesCacheRoot", testCleanupFilesCacheRoot),
|
("testCleanupFilesCacheRoot", testCleanupFilesCacheRoot),
|
||||||
("testCleanupFilesCacheSubdirectory", testCleanupFilesCacheSubdirectory),
|
("testCleanupFilesCacheSubdirectory", testCleanupFilesCacheSubdirectory),
|
||||||
|
// Disk Cache Cleanup
|
||||||
|
("testCleanupDiskCacheFile", testCleanupDiskCacheFile),
|
||||||
|
("testCleanupDiskCacheFolder", testCleanupDiskCacheFolder),
|
||||||
|
("testCleanupDiskCacheWithGoodFile", testCleanupDiskCacheWithGoodFile),
|
||||||
// Image Cache
|
// Image Cache
|
||||||
("testDownloadImage", testDownloadImage),
|
("testDownloadImage", testDownloadImage),
|
||||||
("testImageCache", testImageCache),
|
("testImageCache", testImageCache),
|
||||||
|
|
Loading…
Reference in New Issue