2022-03-22 01:24:38 +01:00
|
|
|
const Path = require('path')
|
|
|
|
const { getId } = require('../utils/index')
|
|
|
|
const { sanitizeFilename } = require('../utils/fileUtils')
|
|
|
|
|
|
|
|
class PodcastEpisodeDownload {
|
|
|
|
constructor() {
|
|
|
|
this.id = null
|
|
|
|
this.podcastEpisode = null
|
|
|
|
this.url = null
|
|
|
|
this.libraryItem = null
|
|
|
|
|
2022-08-16 00:35:13 +02:00
|
|
|
this.isAutoDownload = false
|
2022-03-22 01:24:38 +01:00
|
|
|
this.isDownloading = false
|
2022-04-24 02:41:06 +02:00
|
|
|
this.isFinished = false
|
|
|
|
this.failed = false
|
|
|
|
|
2022-03-22 01:24:38 +01:00
|
|
|
this.startedAt = null
|
|
|
|
this.createdAt = null
|
|
|
|
this.finishedAt = null
|
|
|
|
}
|
|
|
|
|
2022-04-24 02:41:06 +02:00
|
|
|
toJSONForClient() {
|
|
|
|
return {
|
|
|
|
id: this.id,
|
2022-10-03 00:12:44 +02:00
|
|
|
episodeDisplayTitle: this.podcastEpisode ? this.podcastEpisode.title : null,
|
2022-04-24 02:41:06 +02:00
|
|
|
url: this.url,
|
|
|
|
libraryItemId: this.libraryItem ? this.libraryItem.id : null,
|
|
|
|
isDownloading: this.isDownloading,
|
|
|
|
isFinished: this.isFinished,
|
|
|
|
failed: this.failed,
|
|
|
|
startedAt: this.startedAt,
|
|
|
|
createdAt: this.createdAt,
|
|
|
|
finishedAt: this.finishedAt
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-22 01:24:38 +01:00
|
|
|
get targetFilename() {
|
2022-10-03 00:12:44 +02:00
|
|
|
return sanitizeFilename(`${this.podcastEpisode.title}.mp3`)
|
2022-03-22 01:24:38 +01:00
|
|
|
}
|
|
|
|
get targetPath() {
|
|
|
|
return Path.join(this.libraryItem.path, this.targetFilename)
|
|
|
|
}
|
|
|
|
get targetRelPath() {
|
2022-03-27 00:23:33 +01:00
|
|
|
return this.targetFilename
|
2022-03-22 01:24:38 +01:00
|
|
|
}
|
2022-04-24 02:41:06 +02:00
|
|
|
get libraryItemId() {
|
|
|
|
return this.libraryItem ? this.libraryItem.id : null
|
|
|
|
}
|
2022-03-22 01:24:38 +01:00
|
|
|
|
2022-08-16 00:35:13 +02:00
|
|
|
setData(podcastEpisode, libraryItem, isAutoDownload) {
|
2022-03-22 01:24:38 +01:00
|
|
|
this.id = getId('epdl')
|
|
|
|
this.podcastEpisode = podcastEpisode
|
2023-02-18 02:21:45 +01:00
|
|
|
this.url = encodeURI(podcastEpisode.enclosure.url)
|
2022-03-22 01:24:38 +01:00
|
|
|
this.libraryItem = libraryItem
|
2022-08-16 00:35:13 +02:00
|
|
|
this.isAutoDownload = isAutoDownload
|
2022-03-22 01:24:38 +01:00
|
|
|
this.createdAt = Date.now()
|
|
|
|
}
|
2022-04-24 02:41:06 +02:00
|
|
|
|
|
|
|
setFinished(success) {
|
|
|
|
this.finishedAt = Date.now()
|
|
|
|
this.isFinished = true
|
|
|
|
this.failed = !success
|
|
|
|
}
|
2022-03-22 01:24:38 +01:00
|
|
|
}
|
|
|
|
module.exports = PodcastEpisodeDownload
|