From c084c74003bd128cca24f27990fd41f83f26c397 Mon Sep 17 00:00:00 2001 From: Ahmed Shehata <4982099+ahmed-shehata@users.noreply.github.com> Date: Thu, 4 Jan 2024 16:19:08 +0000 Subject: [PATCH] Fix #8614: Playlist auto download not working (#8615) --- Sources/Data/models/PlaylistItem.swift | 17 +++++----- .../Playlist/PlaylistDownloadManager.swift | 33 ++++++++++--------- Sources/Playlist/PlaylistManager.swift | 4 +-- 3 files changed, 27 insertions(+), 27 deletions(-) diff --git a/Sources/Data/models/PlaylistItem.swift b/Sources/Data/models/PlaylistItem.swift index d0eeee326f7..db1f5f40ac9 100644 --- a/Sources/Data/models/PlaylistItem.swift +++ b/Sources/Data/models/PlaylistItem.swift @@ -336,16 +336,15 @@ final public class PlaylistItem: NSManagedObject, CRUD, Identifiable { } } } - - public static func updateCache(uuid: String, cachedData: Data?) { + public static func updateCache(uuid: String, pageSrc: String, cachedData: Data?) { DataController.perform(context: .new(inMemory: false), save: true) { context in - let item = PlaylistItem.first(where: NSPredicate(format: "uuid == %@", uuid), context: context) - - if let cachedData = cachedData, !cachedData.isEmpty { - item?.cachedData = cachedData - } else { - item?.cachedData = nil - } + if let item = PlaylistItem.first(where: NSPredicate(format: "uuid == %@ OR pageSrc == %@", uuid, pageSrc), context: context) { + if let cachedData = cachedData, !cachedData.isEmpty { + item.cachedData = cachedData + } else { + item.cachedData = nil + } + } } } diff --git a/Sources/Playlist/PlaylistDownloadManager.swift b/Sources/Playlist/PlaylistDownloadManager.swift index be87ce74900..4db83e865fd 100644 --- a/Sources/Playlist/PlaylistDownloadManager.swift +++ b/Sources/Playlist/PlaylistDownloadManager.swift @@ -26,6 +26,7 @@ struct MediaDownloadTask { let id: String let name: String let asset: AVURLAsset + let pageSrc: String } public enum PlaylistDownloadError: Error { @@ -277,7 +278,7 @@ private class PlaylistHLSDownloadManager: NSObject, AVAssetDownloadDelegate { if downloadTask.state != .completed, let item = PlaylistItem.getItem(uuid: itemId) { let info = PlaylistInfo(item: item) - let asset = MediaDownloadTask(id: info.tagId, name: info.name, asset: downloadTask.urlAsset) + let asset = MediaDownloadTask(id: info.tagId, name: info.name, asset: downloadTask.urlAsset, pageSrc: info.pageSrc) self.activeDownloadTasks[downloadTask] = asset } } @@ -305,7 +306,7 @@ private class PlaylistHLSDownloadManager: NSObject, AVAssetDownloadDelegate { else { return } task.taskDescription = item.tagId - activeDownloadTasks[task] = MediaDownloadTask(id: item.tagId, name: item.name, asset: asset) + activeDownloadTasks[task] = MediaDownloadTask(id: item.tagId, name: item.name, asset: asset, pageSrc: item.pageSrc) task.resume() DispatchQueue.main.async { @@ -400,7 +401,7 @@ private class PlaylistHLSDownloadManager: NSObject, AVAssetDownloadDelegate { } DispatchQueue.main.async { - PlaylistItem.updateCache(uuid: asset.id, cachedData: nil) + PlaylistItem.updateCache(uuid: asset.id, pageSrc: asset.pageSrc, cachedData: nil) self.delegate?.onDownloadStateChanged(streamDownloader: self, id: asset.id, state: .invalid, displayName: nil, error: error) } } @@ -431,7 +432,7 @@ private class PlaylistHLSDownloadManager: NSObject, AVAssetDownloadDelegate { pendingCancellationTasks.removeAll(where: { $0 == task }) DispatchQueue.main.async { - PlaylistItem.updateCache(uuid: asset.id, cachedData: nil) + PlaylistItem.updateCache(uuid: asset.id, pageSrc: asset.pageSrc, cachedData: nil) self.delegate?.onDownloadStateChanged(streamDownloader: self, id: asset.id, state: .invalid, displayName: nil, error: nil) } return @@ -446,7 +447,7 @@ private class PlaylistHLSDownloadManager: NSObject, AVAssetDownloadDelegate { DispatchQueue.main.async { Logger.module.debug("\(PlaylistItem.getItem(uuid: asset.id).debugDescription)") - PlaylistItem.updateCache(uuid: asset.id, cachedData: nil) + PlaylistItem.updateCache(uuid: asset.id, pageSrc: asset.pageSrc, cachedData: nil) self.delegate?.onDownloadStateChanged(streamDownloader: self, id: asset.id, state: .invalid, displayName: nil, error: error) } } else { @@ -461,7 +462,7 @@ private class PlaylistHLSDownloadManager: NSObject, AVAssetDownloadDelegate { let cachedData = try path.bookmarkData() DispatchQueue.main.async { - PlaylistItem.updateCache(uuid: asset.id, cachedData: cachedData) + PlaylistItem.updateCache(uuid: asset.id, pageSrc: asset.pageSrc, cachedData: cachedData) self.delegate?.onDownloadStateChanged(streamDownloader: self, id: asset.id, state: .downloaded, displayName: nil, error: nil) } } catch { @@ -502,7 +503,7 @@ private class PlaylistFileDownloadManager: NSObject, URLSessionDownloadDelegate let item = PlaylistItem.getItem(uuid: itemId), let assetUrl = URL(string: item.mediaSrc) { let info = PlaylistInfo(item: item) - let asset = MediaDownloadTask(id: info.tagId, name: info.name, asset: AVURLAsset(url: assetUrl, options: AVAsset.defaultOptions)) + let asset = MediaDownloadTask(id: info.tagId, name: info.name, asset: AVURLAsset(url: assetUrl, options: AVAsset.defaultOptions), pageSrc: info.pageSrc) self.activeDownloadTasks[task] = asset } } @@ -526,7 +527,7 @@ private class PlaylistFileDownloadManager: NSObject, URLSessionDownloadDelegate let task = session.downloadTask(with: request) task.taskDescription = item.tagId - activeDownloadTasks[task] = MediaDownloadTask(id: item.tagId, name: item.name, asset: asset) + activeDownloadTasks[task] = MediaDownloadTask(id: item.tagId, name: item.name, asset: asset, pageSrc: item.pageSrc) task.resume() DispatchQueue.main.async { @@ -556,7 +557,7 @@ private class PlaylistFileDownloadManager: NSObject, URLSessionDownloadDelegate if let cacheLocation = delegate?.localAsset(for: asset.id)?.url { do { try FileManager.default.removeItem(at: cacheLocation) - PlaylistItem.updateCache(uuid: asset.id, cachedData: nil) + PlaylistItem.updateCache(uuid: asset.id, pageSrc: asset.pageSrc, cachedData: nil) } catch { Logger.module.error("Could not delete asset cache \(asset.name): \(error.localizedDescription)") } @@ -615,7 +616,7 @@ private class PlaylistFileDownloadManager: NSObject, URLSessionDownloadDelegate } DispatchQueue.main.async { - PlaylistItem.updateCache(uuid: asset.id, cachedData: nil) + PlaylistItem.updateCache(uuid: asset.id, pageSrc: asset.pageSrc, cachedData: nil) self.delegate?.onDownloadStateChanged(streamDownloader: self, id: asset.id, state: .invalid, displayName: nil, error: error) } } @@ -668,7 +669,7 @@ private class PlaylistFileDownloadManager: NSObject, URLSessionDownloadDelegate let cachedData = try path.bookmarkData() DispatchQueue.main.async { - PlaylistItem.updateCache(uuid: asset.id, cachedData: cachedData) + PlaylistItem.updateCache(uuid: asset.id, pageSrc: asset.pageSrc, cachedData: cachedData) self.delegate?.onDownloadStateChanged(streamDownloader: self, id: asset.id, state: .downloaded, displayName: nil, error: nil) } } catch { @@ -711,7 +712,7 @@ private class PlaylistDataDownloadManager: NSObject, URLSessionDataDelegate { let item = PlaylistItem.getItem(uuid: itemId), let assetUrl = URL(string: item.mediaSrc) { let info = PlaylistInfo(item: item) - let asset = MediaDownloadTask(id: info.tagId, name: info.name, asset: AVURLAsset(url: assetUrl, options: AVAsset.defaultOptions)) + let asset = MediaDownloadTask(id: info.tagId, name: info.name, asset: AVURLAsset(url: assetUrl, options: AVAsset.defaultOptions), pageSrc: info.pageSrc ) self.activeDownloadTasks[task] = asset } } @@ -735,7 +736,7 @@ private class PlaylistDataDownloadManager: NSObject, URLSessionDataDelegate { let task = session.dataTask(with: request) task.taskDescription = item.tagId - activeDownloadTasks[task] = MediaDownloadTask(id: item.tagId, name: item.name, asset: asset) + activeDownloadTasks[task] = MediaDownloadTask(id: item.tagId, name: item.name, asset: asset, pageSrc: item.pageSrc) task.resume() DispatchQueue.main.async { @@ -765,7 +766,7 @@ private class PlaylistDataDownloadManager: NSObject, URLSessionDataDelegate { if let cacheLocation = delegate?.localAsset(for: asset.id)?.url { do { try FileManager.default.removeItem(at: cacheLocation) - PlaylistItem.updateCache(uuid: asset.id, cachedData: nil) + PlaylistItem.updateCache(uuid: asset.id, pageSrc: asset.pageSrc, cachedData: nil) } catch { Logger.module.error("Could not delete asset cache \(asset.name): \(error.localizedDescription)") } @@ -811,7 +812,7 @@ private class PlaylistDataDownloadManager: NSObject, URLSessionDataDelegate { } DispatchQueue.main.async { - PlaylistItem.updateCache(uuid: asset.id, cachedData: nil) + PlaylistItem.updateCache(uuid: asset.id, pageSrc: asset.pageSrc, cachedData: nil) self.delegate?.onDownloadStateChanged(streamDownloader: self, id: asset.id, state: .invalid, displayName: nil, error: error) } } @@ -841,7 +842,7 @@ private class PlaylistDataDownloadManager: NSObject, URLSessionDataDelegate { let cachedData = try path.bookmarkData() DispatchQueue.main.async { - PlaylistItem.updateCache(uuid: asset.id, cachedData: cachedData) + PlaylistItem.updateCache(uuid: asset.id, pageSrc: asset.pageSrc, cachedData: cachedData) self.delegate?.onDownloadStateChanged(streamDownloader: self, id: asset.id, state: .downloaded, displayName: nil, error: nil) } } catch { diff --git a/Sources/Playlist/PlaylistManager.swift b/Sources/Playlist/PlaylistManager.swift index d9edf9f672c..b78ae02ad0c 100644 --- a/Sources/Playlist/PlaylistManager.swift +++ b/Sources/Playlist/PlaylistManager.swift @@ -398,7 +398,7 @@ public class PlaylistManager: NSObject { let url = try URL(resolvingBookmarkData: cachedData, bookmarkDataIsStale: &isStale) if FileManager.default.fileExists(atPath: url.path) { try FileManager.default.removeItem(atPath: url.path) - PlaylistItem.updateCache(uuid: item.tagId, cachedData: nil) + PlaylistItem.updateCache(uuid: item.tagId, pageSrc: item.pageSrc, cachedData: nil) onDownloadStateChanged(id: item.tagId, state: .invalid, displayName: nil, error: nil) } return true @@ -463,7 +463,7 @@ public class PlaylistManager: NSObject { assets.forEach({ if let item = PlaylistItem.cachedItem(cacheURL: $0), let itemId = item.uuid { self.cancelDownload(itemId: itemId) - PlaylistItem.updateCache(uuid: itemId, cachedData: nil) + PlaylistItem.updateCache(uuid: itemId, pageSrc: item.pageSrc, cachedData: nil) } }) } catch {