This commit is contained in:
Balazs Perlaki-Horvath 2025-09-21 23:53:01 +02:00
parent 9d5dc55c2e
commit 5025508f95
4 changed files with 61 additions and 19 deletions

View File

@ -146,8 +146,11 @@ final class DownloadService: NSObject, URLSessionDelegate, URLSessionTaskDelegat
context.delete(downloadTask)
try context.save()
} catch {
let fileId = zimFileID.uuidString
let errorDesc = error.localizedDescription
Log.DownloadService.error(
"Error deleting download task for zimFile: \(zimFileID.uuidString, privacy: .public). Error: \(error.localizedDescription, privacy: .public)")
"Error deleting download task for: \(fileId, privacy: .public), \(errorDesc, privacy: .public)"
)
}
}
}
@ -187,13 +190,15 @@ final class DownloadService: NSObject, URLSessionDelegate, URLSessionTaskDelegat
// download finished successfully if there's no error
// and the status code is in the 200 < 300 range
guard let error = error as NSError? else {
let fileId = zimFileID.uuidString
if (200..<300).contains(httpResponse.statusCode) {
Log.DownloadService.info(
"Download finished successfully. File ID: \(zimFileID.uuidString, privacy: .public)",
"Download Ok, zimId: \(fileId, privacy: .public)",
)
} else {
Log.DownloadService.info(
"Download was unsuccessful. File ID: \(zimFileID.uuidString, privacy: .public). status code: \(httpResponse.statusCode, privacy: .public)")
let statusCode = httpResponse.statusCode
Log.DownloadService.error(
"Download error: \(fileId, privacy: .public). status code: \(statusCode, privacy: .public)")
self.deleteDownloadTask(zimFileID: zimFileID)
}
return
@ -218,7 +223,10 @@ final class DownloadService: NSObject, URLSessionDelegate, URLSessionTaskDelegat
try? context.save()
}
}
Log.DownloadService.error("Download finished for File ID: \(zimFileID.uuidString, privacy: .public). with: \(error.localizedDescription, privacy: .public)")
let fileId = zimFileID.uuidString
let errorDesc = error.localizedDescription
Log.DownloadService.error(
"Finished for zimId: \(fileId, privacy: .public). with: \(errorDesc, privacy: .public)")
}
// MARK: - URLSessionDownloadDelegate
@ -237,13 +245,21 @@ final class DownloadService: NSObject, URLSessionDelegate, URLSessionTaskDelegat
}
}
// swiftlint:disable:next function_body_length
func urlSession(_ session: URLSession,
downloadTask: URLSessionDownloadTask,
didFinishDownloadingTo location: URL) {
guard let httpResponse = downloadTask.response as? HTTPURLResponse else { return }
guard let httpResponse = downloadTask.response as? HTTPURLResponse else {
Log.DownloadService.fault("Response is not an HTTPURLResponse")
return
}
let taskId = downloadTask.taskDescription ?? ""
guard (200..<300).contains(httpResponse.statusCode) else {
Log.DownloadService.error("Download didFinish failed with http status for: \(downloadTask.taskIdentifier.description, privacy: .public), httpStatusCode: \(httpResponse.statusCode, privacy: .public)")
let taskId = downloadTask.taskIdentifier.description
let statusCode = httpResponse.statusCode
Log.DownloadService.error(
"didFinish failed for: \(taskId, privacy: .public), status: \(statusCode, privacy: .public)")
Task { @MainActor in
NotificationCenter.default.post(
name: .alert,
@ -262,27 +278,40 @@ final class DownloadService: NSObject, URLSessionDelegate, URLSessionTaskDelegat
#endif
// move file
guard let directory = FileManager.default.urls(for: searchPath, in: .userDomainMask).first else {
Log.DownloadService.fault("Cannot find download directory!! downloadTask: \(downloadTask.taskDescription ?? "", privacy: .public)")
Log.DownloadService.fault(
"Cannot find download directory! downloadTask: \(taskId ?? "", privacy: .public)"
)
return
}
guard let zimFileID = UUID(uuidString: downloadTask.taskDescription ?? "") else {
Log.DownloadService.fault("Cannot convert downloadTask to zimFileID: \(downloadTask.taskDescription ?? "", privacy: .public)")
guard let zimFileID = UUID(uuidString: taskId) else {
Log.DownloadService.fault(
"Cannot convert downloadTask to zimFileID: \(taskId, privacy: .public)"
)
return
}
let fileName = downloadTask.response?.suggestedFilename
?? downloadTask.originalRequest?.url?.lastPathComponent
?? zimFileID.uuidString + ".zim"
let destination = directory.appendingPathComponent(fileName)
Log.DownloadService.info("Start moving downloaded zimFile: \(fileName, privacy: .public), zimFileID: \(zimFileID.uuidString, privacy: .public)")
Log.DownloadService.info(
"Start moving zimFile: \(fileName, privacy: .public), \(zimFileID.uuidString, privacy: .public)"
)
try? FileManager.default.moveItem(at: location, to: destination)
Log.DownloadService.info("Completed moving downloaded zimFile: \(zimFileID.uuidString, privacy: .public)")
Log.DownloadService.info(
"Completed moving zimFile: \(zimFileID.uuidString, privacy: .public)"
)
// open the file
Task { @ZimActor in
Log.DownloadService.info("start opening downloaded zimFile: \(zimFileID.uuidString, privacy: .public)")
Log.DownloadService.info(
"start opening zimFile: \(zimFileID.uuidString, privacy: .public)"
)
await LibraryOperations.open(url: destination)
Log.DownloadService.info("opened downloaded zimFile: \(zimFileID.uuidString, privacy: .public)")
Log.DownloadService.info(
"opened downloaded zimFile: \(zimFileID.uuidString, privacy: .public)"
)
// schedule notification
scheduleDownloadCompleteNotification(zimFileID: zimFileID)
deleteDownloadTask(zimFileID: zimFileID)

View File

@ -91,7 +91,9 @@ struct LibraryOperations {
try? context.save()
}
}
Log.LibraryOperations.info("Reopened \(successCount, privacy: .public) out of \(zimFiles.count, privacy: .public) zim files")
Log.LibraryOperations.info(
"Reopened \(successCount, privacy: .public) out of \(zimFiles.count, privacy: .public) zim files"
)
}
/// Scan a directory and open available zim files inside it
@ -208,9 +210,12 @@ struct LibraryOperations {
try url.setResourceValues(resourceValues)
}
let status = backupDocumentDirectory ? "backing up" : "not backing up"
Log.LibraryOperations.info("Applying zim file backup setting (\(status, privacy: .public)) on \(urls.count, privacy: .public) zim file(s).")
let fileCount = urls.count
Log.LibraryOperations.info(
"Updated iCloud backup setting (\(status, privacy: .public)) on files: \(fileCount, privacy: .public)")
} catch {
Log.LibraryOperations.error("Unable to change iCloud backup settings, due to \(error.localizedDescription, privacy: .public)")
Log.LibraryOperations.error(
"Unable to change iCloud backup settings, due to \(error.localizedDescription, privacy: .public)")
}
}
}

View File

@ -445,7 +445,11 @@ final class BrowserViewModel: NSObject, ObservableObject,
return .cancel
} else if url.isZIMURL {
guard await ZimFileService.shared.getContentSize(url: url) != nil else {
Log.URLSchemeHandler.error("Missing content at url: \(url.absoluteString, privacy: .public) => \(url.contentPath, privacy: .public)")
let urlString = url.absoluteString
let path = url.contentPath
Log.URLSchemeHandler.error(
"Missing content at url: \(urlString, privacy: .public) => \(path, privacy: .public)"
)
if navigationAction.request.mainDocumentURL == url {
// only show alerts for missing main document
NotificationCenter.default.post(

View File

@ -207,7 +207,11 @@ final class LibraryViewModel: ObservableObject {
let insertedCount = insertionCount
let deletedCount = deletionCount
let totalCount = parser.zimFileIDs.count
Log.OPDS.notice("Refresh finished -- insertion: \(insertedCount, privacy: .public), deletion: \(deletedCount, privacy: .public), total: \(totalCount, privacy: .public)")
Log.OPDS.notice("""
Refresh finished -- insertion: \(insertedCount, privacy: .public), \
deletion: \(deletedCount, privacy: .public), \
total: \(totalCount, privacy: .public)
""")
} catch {
self.error = error
process.state = .error