diff --git a/Core/Core/Network/DownloadManager.swift b/Core/Core/Network/DownloadManager.swift index 6b6b30f19..8aa565c7f 100644 --- a/Core/Core/Network/DownloadManager.swift +++ b/Core/Core/Network/DownloadManager.swift @@ -297,7 +297,7 @@ public class DownloadManager: DownloadManagerProtocol { public func deleteFile(blocks: [CourseBlock]) async { for block in blocks { do { - if let fileURL = fileUrl(for: block.id), + if let fileURL = fileOrFolderUrl(for: block.id), FileManager.default.fileExists(atPath: fileURL.path) { try FileManager.default.removeItem(at: fileURL) } @@ -367,7 +367,7 @@ public class DownloadManager: DownloadManagerProtocol { public func deleteAllFiles() async { let downloadsData = await getDownloadTasks() for downloadData in downloadsData { - if let fileURL = fileUrl(for: downloadData.id) { + if let fileURL = fileOrFolderUrl(for: downloadData.id) { do { try FileManager.default.removeItem(at: fileURL) } catch { @@ -395,6 +395,24 @@ public class DownloadManager: DownloadManagerProtocol { return path?.appendingPathComponent(data.fileName) } } + + public func fileOrFolderUrl(for blockId: String) -> URL? { + guard let data = persistence.downloadDataTask(for: blockId), + data.url.count > 0, + data.state == .finished else { return nil } + let path = filesFolderUrl + switch data.type { + case .html, .problem: + if let folderUrl = URL(string: data.url) { + let folder = folderUrl.deletingPathExtension().lastPathComponent + return path?.appendingPathComponent(folder) + } else { + return nil + } + case .video: + return path?.appendingPathComponent(data.fileName) + } + } // MARK: - Private Intents