From f5e998946d4c23903e769e03625535952722abab Mon Sep 17 00:00:00 2001 From: IvanStepanok <128456094+IvanStepanok@users.noreply.github.com> Date: Wed, 9 Oct 2024 18:57:54 +0300 Subject: [PATCH] Fix: offline file deletion issue (#527) * fix: update DownloadManager.swift * fix: fix the temp data deletion issue * fix: upgrade fastlane --------- Co-authored-by: Volodymyr Chekyrta --- Core/Core/Network/DownloadManager.swift | 81 ++++++++++++++++--------- Gemfile.lock | 4 +- 2 files changed, 56 insertions(+), 29 deletions(-) diff --git a/Core/Core/Network/DownloadManager.swift b/Core/Core/Network/DownloadManager.swift index 6b6b30f19..dad856a1e 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 @@ -453,10 +471,12 @@ public class DownloadManager: DownloadManagerProtocol { resumeData: download.resumeData ) self.isDownloadingInProgress = true + let destination: DownloadRequest.Destination = { _, _ in let file = folderURL.appendingPathComponent(download.fileName) return (file, [.createIntermediateDirectories, .removePreviousFile]) } + if let resumeData = download.resumeData { downloadRequest = AF.download(resumingWith: resumeData, to: destination) } else { @@ -464,41 +484,43 @@ public class DownloadManager: DownloadManagerProtocol { } downloadRequest?.downloadProgress { [weak self] prog in - guard let self else { return } + guard let self = self else { return } let fractionCompleted = prog.fractionCompleted self.currentDownloadTask?.progress = fractionCompleted self.currentDownloadTask?.state = .inProgress self.currentDownloadEventPublisher.send(.progress(fractionCompleted, download)) let completed = Double(fractionCompleted * 100) - debugLog(">>>>> Downloading", download.url, completed, "%") + debugLog(">>>>> Downloading File", download.url, completed, "%") } - downloadRequest?.responseData { [weak self] data in - guard let self else { return } - if let error = data.error { + downloadRequest?.responseURL { [weak self] response in + guard let self = self else { return } + if let error = response.error { if error.asAFError?.isExplicitlyCancelledError == false { - failedDownloads.append(download) + self.failedDownloads.append(download) Task { try? await self.newDownload() } return } } - self.persistence.updateDownloadState( - id: download.id, - state: .finished, - resumeData: nil - ) - self.currentDownloadTask?.state = .finished - self.currentDownloadEventPublisher.send(.finished(download)) - Task { - try? await self.newDownload() + if response.fileURL != nil { + self.persistence.updateDownloadState( + id: download.id, + state: .finished, + resumeData: nil + ) + self.currentDownloadTask?.state = .finished + self.currentDownloadEventPublisher.send(.finished(download)) + Task { + try? await self.newDownload() + } } } } private func downloadHTMLWithProgress(_ download: DownloadDataTask) throws { - guard let url = URL(string: download.url) else { + guard let url = URL(string: download.url), let folderURL = self.filesFolderUrl else { return } @@ -508,10 +530,17 @@ public class DownloadManager: DownloadManagerProtocol { resumeData: download.resumeData ) self.isDownloadingInProgress = true + + let destination: DownloadRequest.Destination = { _, _ in + let fileName = URL(string: download.url)?.lastPathComponent ?? "file.zip" + let file = folderURL.appendingPathComponent(fileName) + return (file, [.createIntermediateDirectories, .removePreviousFile]) + } + if let resumeData = download.resumeData { - downloadRequest = AF.download(resumingWith: resumeData) + downloadRequest = AF.download(resumingWith: resumeData, to: destination) } else { - downloadRequest = AF.download(url) + downloadRequest = AF.download(url, to: destination) } downloadRequest?.downloadProgress { [weak self] prog in @@ -521,12 +550,12 @@ public class DownloadManager: DownloadManagerProtocol { self.currentDownloadTask?.state = .inProgress self.currentDownloadEventPublisher.send(.progress(fractionCompleted, download)) let completed = Double(fractionCompleted * 100) - debugLog(">>>>> Downloading", download.url, completed, "%") + debugLog(">>>>> Downloading HTML", download.url, completed, "%") } - downloadRequest?.responseData { [weak self] data in + downloadRequest?.responseURL { [weak self] response in guard let self else { return } - if let error = data.error { + if let error = response.error { if error.asAFError?.isExplicitlyCancelledError == false { failedDownloads.append(download) Task { @@ -535,10 +564,8 @@ public class DownloadManager: DownloadManagerProtocol { return } } - if let data = data.value, let url = self.filesFolderUrl, - let fileName = URL(string: download.url)?.lastPathComponent { - self.saveFile(fileName: fileName, data: data, folderURL: url) - self.unzipFile(url: url.appendingPathComponent(fileName)) + if let fileURL = response.fileURL { + self.unzipFile(url: fileURL) self.persistence.updateDownloadState( id: download.id, state: .finished, diff --git a/Gemfile.lock b/Gemfile.lock index af3a47938..f6449ff37 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -10,7 +10,7 @@ GEM artifactory (3.0.17) atomos (0.1.3) aws-eventstream (1.3.0) - aws-partitions (1.983.0) + aws-partitions (1.987.0) aws-sdk-core (3.209.1) aws-eventstream (~> 1, >= 1.3.0) aws-partitions (~> 1, >= 1.651.0) @@ -68,7 +68,7 @@ GEM faraday_middleware (1.2.1) faraday (~> 1.0) fastimage (2.3.1) - fastlane (2.223.1) + fastlane (2.224.0) CFPropertyList (>= 2.3, < 4.0.0) addressable (>= 2.8, < 3.0.0) artifactory (~> 3.0)