Skip to content

Commit

Permalink
fix: fix the temp data deletion issue
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanStepanok committed Oct 9, 2024
1 parent 808bc4f commit 0293878
Showing 1 changed file with 34 additions and 25 deletions.
59 changes: 34 additions & 25 deletions Core/Core/Network/DownloadManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -471,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 @@ -526,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 @@ -539,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 @@ -553,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

0 comments on commit 0293878

Please sign in to comment.