Cache only non-empty archives (#685)

Fixes #684. But I'm not sure how it got into this state in the first place. `URLSession.shared.data` should've throw.
This commit is contained in:
Fedor Korotkov
2023-12-08 10:43:43 +04:00
committed by GitHub
parent 35377a3475
commit 60f0eac7a8
+7 -3
View File
@@ -659,7 +659,7 @@ struct DirectoryShare {
let archiveRequest = URLRequest(url: path, cachePolicy: .returnCacheDataElseLoad)
var response: CachedURLResponse? = urlCache.cachedResponse(for: archiveRequest)
if (response == nil) {
if (response == nil || response?.data.isEmpty == true) {
print("Downloading \(path)...")
// download and unarchive remote directories if needed here
// use old school API to prevent deadlocks since we are running via MainActor
@@ -667,8 +667,12 @@ struct DirectoryShare {
Task {
do {
let (archiveData, archiveResponse) = try await URLSession.shared.data(for: archiveRequest)
urlCache.storeCachedResponse(CachedURLResponse(response: archiveResponse, data: archiveData, storagePolicy: .allowed), for: archiveRequest)
print("Cached for future invocations!")
if archiveData.isEmpty {
print("Remote archive is empty!")
} else {
urlCache.storeCachedResponse(CachedURLResponse(response: archiveResponse, data: archiveData, storagePolicy: .allowed), for: archiveRequest)
print("Cached for future invocations!")
}
} catch {
print("Download failed: \(error)")
}