Skip to content

Commit

Permalink
Fix brave/brave-ios#8614: Playlist auto download not working (brave/b…
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmed-shehata authored Jan 4, 2024
1 parent 25d8536 commit ed2f955
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 27 deletions.
17 changes: 8 additions & 9 deletions Sources/Data/models/PlaylistItem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}
}

Expand Down
33 changes: 17 additions & 16 deletions Sources/Playlist/PlaylistDownloadManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ struct MediaDownloadTask {
let id: String
let name: String
let asset: AVURLAsset
let pageSrc: String
}

public enum PlaylistDownloadError: Error {
Expand Down Expand Up @@ -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
}
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
}
Expand Down Expand Up @@ -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
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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
}
}
Expand All @@ -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 {
Expand Down Expand Up @@ -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)")
}
Expand Down Expand Up @@ -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)
}
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
}
Expand All @@ -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 {
Expand Down Expand Up @@ -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)")
}
Expand Down Expand Up @@ -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)
}
}
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions Sources/Playlist/PlaylistManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit ed2f955

Please sign in to comment.