Skip to content

Commit

Permalink
Update DownloadManager.swift
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanStepanok committed Oct 9, 2024
1 parent 004666e commit a17d934
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 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

0 comments on commit a17d934

Please sign in to comment.