Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: offline file deletion issue #527

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 54 additions & 27 deletions Core/Core/Network/DownloadManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -453,52 +471,56 @@ 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 {
downloadRequest = AF.download(url, to: destination)
}

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
}

Expand All @@ -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
Expand All @@ -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 {
Expand All @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
Loading