audiobookshelf/server/objects/PodcastEpisodeDownload.js

97 lines
2.7 KiB
JavaScript
Raw Normal View History

const Path = require('path')
const uuidv4 = require('uuid').v4
const { sanitizeFilename, filePathToPOSIX } = require('../utils/fileUtils')
const globals = require('../utils/globals')
class PodcastEpisodeDownload {
constructor() {
this.id = null
this.podcastEpisode = null
this.url = null
this.libraryItem = null
2023-02-27 03:56:07 +01:00
this.libraryId = null
this.isAutoDownload = false
this.isFinished = false
this.failed = false
this.appendEpisodeId = false
this.startedAt = null
this.createdAt = null
this.finishedAt = null
}
toJSONForClient() {
return {
id: this.id,
episodeDisplayTitle: this.podcastEpisode?.title ?? null,
url: this.url,
libraryItemId: this.libraryItem?.id || null,
2023-02-27 03:56:07 +01:00
libraryId: this.libraryId || null,
isFinished: this.isFinished,
failed: this.failed,
appendEpisodeId: this.appendEpisodeId,
startedAt: this.startedAt,
createdAt: this.createdAt,
2023-02-27 03:56:07 +01:00
finishedAt: this.finishedAt,
podcastTitle: this.libraryItem?.media.metadata.title ?? null,
podcastExplicit: !!this.libraryItem?.media.metadata.explicit,
season: this.podcastEpisode?.season ?? null,
episode: this.podcastEpisode?.episode ?? null,
episodeType: this.podcastEpisode?.episodeType ?? 'full',
publishedAt: this.podcastEpisode?.publishedAt ?? null
}
}
get urlFileExtension() {
const cleanUrl = this.url.split('?')[0] // Remove query string
return Path.extname(cleanUrl).substring(1).toLowerCase()
}
get fileExtension() {
const extname = this.urlFileExtension
if (globals.SupportedAudioTypes.includes(extname)) return extname
return 'mp3'
}
get targetFilename() {
const appendage = this.appendEpisodeId ? ` (${this.podcastEpisode.id})` : ''
const filename = `${this.podcastEpisode.title}${appendage}.${this.fileExtension}`
return sanitizeFilename(filename)
}
get targetPath() {
return filePathToPOSIX(Path.join(this.libraryItem.path, this.targetFilename))
}
get targetRelPath() {
return this.targetFilename
}
get libraryItemId() {
return this.libraryItem ? this.libraryItem.id : null
}
2023-02-27 03:56:07 +01:00
setData(podcastEpisode, libraryItem, isAutoDownload, libraryId) {
2023-07-05 01:14:44 +02:00
this.id = uuidv4()
this.podcastEpisode = podcastEpisode
const url = podcastEpisode.enclosure.url
if (decodeURIComponent(url) !== url) {
// Already encoded
this.url = url
} else {
this.url = encodeURI(url)
}
this.libraryItem = libraryItem
this.isAutoDownload = isAutoDownload
this.createdAt = Date.now()
2023-02-27 03:56:07 +01:00
this.libraryId = libraryId
}
setFinished(success) {
this.finishedAt = Date.now()
this.isFinished = true
this.failed = !success
}
}
2023-02-27 03:56:07 +01:00
module.exports = PodcastEpisodeDownload