Support downloading new podcast episodes

This commit is contained in:
ronaldheft 2022-08-11 12:30:45 -04:00
parent e52a5fd585
commit 5f4ff38035
5 changed files with 41 additions and 16 deletions

View file

@ -157,14 +157,19 @@ public class AbsDownloader: CAPPlugin, URLSessionDownloadDelegate {
} }
return LocalFile(libraryItem.id, part.filename!, part.mimeType()!, part.destinationUri!, fileSize: Int(part.destinationURL!.fileSize)) return LocalFile(libraryItem.id, part.filename!, part.mimeType()!, part.destinationUri!, fileSize: Int(part.destinationURL!.fileSize))
} }
let localLibraryItem = LocalLibraryItem(libraryItem, localUrl: localDirectory, server: Store.serverConfig!, files: files, coverPath: coverFile) var localLibraryItem = Database.shared.getLocalLibraryItemByLLId(libraryItem: libraryItem.id)
if (localLibraryItem != nil && localLibraryItem!.isPodcast) {
try! localLibraryItem?.addFiles(files, item: libraryItem)
} else {
localLibraryItem = LocalLibraryItem(libraryItem, localUrl: localDirectory, server: Store.serverConfig!, files: files, coverPath: coverFile)
}
Database.shared.saveLocalLibraryItem(localLibraryItem: localLibraryItem) Database.shared.saveLocalLibraryItem(localLibraryItem: localLibraryItem!)
statusNotification["localLibraryItem"] = try? localLibraryItem.asDictionary() statusNotification["localLibraryItem"] = try? localLibraryItem.asDictionary()
if let progress = libraryItem.userMediaProgress { if let progress = libraryItem.userMediaProgress {
let episode = downloadItem.media?.episodes?.first(where: { $0.id == downloadItem.episodeId }) let episode = downloadItem.media?.episodes?.first(where: { $0.id == downloadItem.episodeId })
let localMediaProgress = LocalMediaProgress(localLibraryItem: localLibraryItem, episode: episode, progress: progress) let localMediaProgress = LocalMediaProgress(localLibraryItem: localLibraryItem!, episode: episode, progress: progress)
Database.shared.saveLocalMediaProgress(localMediaProgress) Database.shared.saveLocalMediaProgress(localMediaProgress)
statusNotification["localMediaProgress"] = try? localMediaProgress.asDictionary() statusNotification["localMediaProgress"] = try? localMediaProgress.asDictionary()
} }
@ -319,6 +324,7 @@ enum LibraryItemDownloadError: String, Error {
case noMetadata = "No metadata for track, unable to download" case noMetadata = "No metadata for track, unable to download"
case libraryItemNotPodcast = "Library item is not a podcast but episode was requested" case libraryItemNotPodcast = "Library item is not a podcast but episode was requested"
case podcastEpisodeNotFound = "Invalid podcast episode not found" case podcastEpisodeNotFound = "Invalid podcast episode not found"
case podcastOnlySupported = "Only podcasts are supported for this function"
case unknownMediaType = "Unknown media type" case unknownMediaType = "Unknown media type"
case failedDirectory = "Failed to create directory" case failedDirectory = "Failed to create directory"
case failedDownload = "Failed to download item" case failedDownload = "Failed to download item"

View file

@ -207,11 +207,13 @@ struct AudioTrack: Realmable, Codable {
mimeType = "" mimeType = ""
} }
mutating func setLocalInfo(filenameIdMap: [String: String], serverIndex: Int) { mutating func setLocalInfo(filenameIdMap: [String: String], serverIndex: Int) -> Bool {
if let localFileId = filenameIdMap[self.metadata?.filename ?? ""] { if let localFileId = filenameIdMap[self.metadata?.filename ?? ""] {
self.localFileId = localFileId self.localFileId = localFileId
self.serverIndex = serverIndex self.serverIndex = serverIndex
return true
} }
return false
} }
} }

View file

@ -9,7 +9,7 @@ import Foundation
import Unrealm import Unrealm
struct LocalLibraryItem: Realmable, Codable { struct LocalLibraryItem: Realmable, Codable {
var id: String = "local_\(UUID().uuidString)" var id: String = ""
var basePath: String = "" var basePath: String = ""
var _contentUrl: String? var _contentUrl: String?
var isInvalid: Bool = false var isInvalid: Bool = false
@ -39,6 +39,8 @@ struct LocalLibraryItem: Realmable, Codable {
} }
} }
var isPodcast: Bool { self.mediaType == "podcast" }
static func primaryKey() -> String? { static func primaryKey() -> String? {
return "id" return "id"
} }

View file

@ -10,6 +10,7 @@ import Foundation
extension LocalLibraryItem { extension LocalLibraryItem {
init(_ item: LibraryItem, localUrl: String, server: ServerConnectionConfig, files: [LocalFile], coverPath: String?) { init(_ item: LibraryItem, localUrl: String, server: ServerConnectionConfig, files: [LocalFile], coverPath: String?) {
self.init() self.init()
self.id = "local_\(item.id)"
self._contentUrl = localUrl self._contentUrl = localUrl
self.mediaType = item.mediaType self.mediaType = item.mediaType
self.localFiles = files self.localFiles = files
@ -20,22 +21,36 @@ extension LocalLibraryItem {
self.serverUserId = server.userId self.serverUserId = server.userId
// Link the audio tracks and files // Link the audio tracks and files
var media = item.media linkLocalFiles(files, fromMedia: item.media)
let fileIdByFilename = Dictionary(uniqueKeysWithValues: files.map { ($0.filename ?? "", $0.id) } ) }
if ( item.mediaType == "book" ) {
if let tracks = media.tracks { mutating func addFiles(_ files: [LocalFile], item: LibraryItem) throws {
guard self.isPodcast else { throw LibraryItemDownloadError.podcastOnlySupported }
self.localFiles.append(contentsOf: files.filter({ $0.isAudioFile() }))
linkLocalFiles(self.localFiles, fromMedia: item.media)
}
mutating private func linkLocalFiles(_ files: [LocalFile], fromMedia: MediaType) {
var fromMedia = fromMedia
let fileMap = files.map { ($0.filename ?? "", $0.id) }
let fileIdByFilename = Dictionary(fileMap, uniquingKeysWith: { (_, last) in last })
if ( self.mediaType == "book" ) {
if let tracks = fromMedia.tracks {
for i in tracks.indices { for i in tracks.indices {
media.tracks?[i].setLocalInfo(filenameIdMap: fileIdByFilename, serverIndex: i) _ = fromMedia.tracks?[i].setLocalInfo(filenameIdMap: fileIdByFilename, serverIndex: i)
} }
} }
} else if ( item.mediaType == "podcast" ) { } else if ( self.mediaType == "podcast" ) {
if let episodes = media.episodes { if let episodes = fromMedia.episodes {
for i in episodes.indices { fromMedia.episodes = episodes.compactMap { episode in
media.episodes?[i].audioTrack?.setLocalInfo(filenameIdMap: fileIdByFilename, serverIndex: 0) // Filter out episodes not downloaded
var episode = episode
let episodeIsDownloaded = episode.audioTrack?.setLocalInfo(filenameIdMap: fileIdByFilename, serverIndex: 0) ?? false
return episodeIsDownloaded ? episode : nil
} }
} }
} }
self.media = media self.media = fromMedia
} }
func getDuration() -> Double { func getDuration() -> Double {

View file

@ -168,7 +168,7 @@ class Database {
public func saveLocalMediaProgress(_ mediaProgress: LocalMediaProgress) { public func saveLocalMediaProgress(_ mediaProgress: LocalMediaProgress) {
let realm = try! Realm() let realm = try! Realm()
try! realm.write { realm.add(mediaProgress) } try! realm.write { realm.add(mediaProgress, update: .modified) }
} }
// For books this will just be the localLibraryItemId for podcast episodes this will be "{localLibraryItemId}-{episodeId}" // For books this will just be the localLibraryItemId for podcast episodes this will be "{localLibraryItemId}-{episodeId}"