audiobookshelf/server/objects/entities/PodcastEpisode.js

187 lines
5.1 KiB
JavaScript
Raw Permalink Normal View History

const uuidv4 = require('uuid').v4
2023-10-20 00:20:12 +02:00
const { areEquivalent, copyValue } = require('../../utils/index')
2022-03-09 02:31:44 +01:00
const AudioFile = require('../files/AudioFile')
const AudioTrack = require('../files/AudioTrack')
2022-03-09 02:31:44 +01:00
class PodcastEpisode {
constructor(episode) {
this.libraryItemId = null
2023-07-05 01:14:44 +02:00
this.podcastId = null
2022-03-09 02:31:44 +01:00
this.id = null
this.oldEpisodeId = null
this.index = null
2022-03-09 02:31:44 +01:00
this.season = null
this.episode = null
this.episodeType = null
this.title = null
this.subtitle = null
this.description = null
this.enclosure = null
this.guid = null
this.pubDate = null
2023-04-09 21:32:51 +02:00
this.chapters = []
2022-03-09 02:31:44 +01:00
this.audioFile = null
this.publishedAt = null
2022-03-09 02:31:44 +01:00
this.addedAt = null
this.updatedAt = null
if (episode) {
this.construct(episode)
}
}
construct(episode) {
this.libraryItemId = episode.libraryItemId
2023-07-05 01:14:44 +02:00
this.podcastId = episode.podcastId
2022-03-09 02:31:44 +01:00
this.id = episode.id
this.oldEpisodeId = episode.oldEpisodeId
this.index = episode.index
this.season = episode.season
this.episode = episode.episode
this.episodeType = episode.episodeType
this.title = episode.title
this.subtitle = episode.subtitle
this.description = episode.description
this.enclosure = episode.enclosure ? { ...episode.enclosure } : null
this.guid = episode.guid || null
this.pubDate = episode.pubDate
this.chapters = episode.chapters?.map((ch) => ({ ...ch })) || []
this.audioFile = episode.audioFile ? new AudioFile(episode.audioFile) : null
this.publishedAt = episode.publishedAt
2022-03-09 02:31:44 +01:00
this.addedAt = episode.addedAt
this.updatedAt = episode.updatedAt
if (this.audioFile) {
this.audioFile.index = 1 // Only 1 audio file per episode
}
2022-03-09 02:31:44 +01:00
}
toJSON() {
return {
libraryItemId: this.libraryItemId,
2023-07-05 01:14:44 +02:00
podcastId: this.podcastId,
2022-03-09 02:31:44 +01:00
id: this.id,
oldEpisodeId: this.oldEpisodeId,
index: this.index,
season: this.season,
episode: this.episode,
episodeType: this.episodeType,
title: this.title,
subtitle: this.subtitle,
description: this.description,
enclosure: this.enclosure ? { ...this.enclosure } : null,
guid: this.guid,
pubDate: this.pubDate,
chapters: this.chapters.map((ch) => ({ ...ch })),
audioFile: this.audioFile?.toJSON() || null,
publishedAt: this.publishedAt,
2022-03-09 02:31:44 +01:00
addedAt: this.addedAt,
updatedAt: this.updatedAt
}
}
toJSONExpanded() {
return {
libraryItemId: this.libraryItemId,
2023-07-05 01:14:44 +02:00
podcastId: this.podcastId,
id: this.id,
oldEpisodeId: this.oldEpisodeId,
index: this.index,
season: this.season,
episode: this.episode,
episodeType: this.episodeType,
title: this.title,
subtitle: this.subtitle,
description: this.description,
enclosure: this.enclosure ? { ...this.enclosure } : null,
guid: this.guid,
pubDate: this.pubDate,
chapters: this.chapters.map((ch) => ({ ...ch })),
audioFile: this.audioFile?.toJSON() || null,
audioTrack: this.audioTrack?.toJSON() || null,
publishedAt: this.publishedAt,
addedAt: this.addedAt,
updatedAt: this.updatedAt,
duration: this.duration,
size: this.size
}
}
get audioTrack() {
if (!this.audioFile) return null
2022-12-26 23:08:53 +01:00
const audioTrack = new AudioTrack()
audioTrack.setData(this.libraryItemId, this.audioFile, 0)
return audioTrack
}
get tracks() {
return [this.audioTrack]
}
2022-03-18 18:37:47 +01:00
get duration() {
return this.audioFile?.duration || 0
2022-03-18 18:37:47 +01:00
}
get size() {
return this.audioFile?.metadata.size || 0
}
get enclosureUrl() {
return this.enclosure?.url || null
}
get pubYear() {
if (!this.publishedAt) return null
return new Date(this.publishedAt).getFullYear()
}
setData(data, index = 1) {
2023-07-05 01:14:44 +02:00
this.id = uuidv4()
this.index = index
this.title = data.title
this.subtitle = data.subtitle || ''
this.pubDate = data.pubDate || ''
this.description = data.description || ''
this.enclosure = data.enclosure ? { ...data.enclosure } : null
this.guid = data.guid || null
this.season = data.season || ''
this.episode = data.episode || ''
2023-02-22 19:48:36 +01:00
this.episodeType = data.episodeType || 'full'
this.publishedAt = data.publishedAt || 0
this.addedAt = Date.now()
this.updatedAt = Date.now()
}
update(payload) {
2022-12-26 23:08:53 +01:00
let hasUpdates = false
for (const key in this.toJSON()) {
2023-07-06 01:18:37 +02:00
let newValue = payload[key]
if (newValue === '') newValue = null
2023-07-06 01:18:37 +02:00
let existingValue = this[key]
if (existingValue === '') existingValue = null
2023-07-06 01:18:37 +02:00
if (newValue != undefined && !areEquivalent(newValue, existingValue)) {
this[key] = copyValue(newValue)
hasUpdates = true
}
}
if (hasUpdates) {
this.updatedAt = Date.now()
}
return hasUpdates
}
// Only checks container format
checkCanDirectPlay(payload) {
const supportedMimeTypes = payload.supportedMimeTypes || []
return supportedMimeTypes.includes(this.audioFile.mimeType)
}
getDirectPlayTracklist() {
return this.tracks
}
checkEqualsEnclosureUrl(url) {
if (!this.enclosure?.url) return false
return this.enclosure.url == url
}
2022-03-09 02:31:44 +01:00
}
2023-02-22 19:48:36 +01:00
module.exports = PodcastEpisode