mirror of
https://github.com/advplyr/audiobookshelf-app.git
synced 2026-08-25 21:04:02 +02:00
Support downloading new podcast episodes
This commit is contained in:
@@ -207,11 +207,13 @@ struct AudioTrack: Realmable, Codable {
|
||||
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 ?? ""] {
|
||||
self.localFileId = localFileId
|
||||
self.serverIndex = serverIndex
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -9,7 +9,7 @@ import Foundation
|
||||
import Unrealm
|
||||
|
||||
struct LocalLibraryItem: Realmable, Codable {
|
||||
var id: String = "local_\(UUID().uuidString)"
|
||||
var id: String = ""
|
||||
var basePath: String = ""
|
||||
var _contentUrl: String?
|
||||
var isInvalid: Bool = false
|
||||
@@ -39,6 +39,8 @@ struct LocalLibraryItem: Realmable, Codable {
|
||||
}
|
||||
}
|
||||
|
||||
var isPodcast: Bool { self.mediaType == "podcast" }
|
||||
|
||||
static func primaryKey() -> String? {
|
||||
return "id"
|
||||
}
|
||||
|
||||
@@ -10,6 +10,7 @@ import Foundation
|
||||
extension LocalLibraryItem {
|
||||
init(_ item: LibraryItem, localUrl: String, server: ServerConnectionConfig, files: [LocalFile], coverPath: String?) {
|
||||
self.init()
|
||||
self.id = "local_\(item.id)"
|
||||
self._contentUrl = localUrl
|
||||
self.mediaType = item.mediaType
|
||||
self.localFiles = files
|
||||
@@ -20,22 +21,36 @@ extension LocalLibraryItem {
|
||||
self.serverUserId = server.userId
|
||||
|
||||
// Link the audio tracks and files
|
||||
var media = item.media
|
||||
let fileIdByFilename = Dictionary(uniqueKeysWithValues: files.map { ($0.filename ?? "", $0.id) } )
|
||||
if ( item.mediaType == "book" ) {
|
||||
if let tracks = media.tracks {
|
||||
linkLocalFiles(files, fromMedia: item.media)
|
||||
}
|
||||
|
||||
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 {
|
||||
media.tracks?[i].setLocalInfo(filenameIdMap: fileIdByFilename, serverIndex: i)
|
||||
_ = fromMedia.tracks?[i].setLocalInfo(filenameIdMap: fileIdByFilename, serverIndex: i)
|
||||
}
|
||||
}
|
||||
} else if ( item.mediaType == "podcast" ) {
|
||||
if let episodes = media.episodes {
|
||||
for i in episodes.indices {
|
||||
media.episodes?[i].audioTrack?.setLocalInfo(filenameIdMap: fileIdByFilename, serverIndex: 0)
|
||||
} else if ( self.mediaType == "podcast" ) {
|
||||
if let episodes = fromMedia.episodes {
|
||||
fromMedia.episodes = episodes.compactMap { episode in
|
||||
// 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 {
|
||||
|
||||
Reference in New Issue
Block a user