2024-08-11 22:15:34 +02:00
|
|
|
const { Request, Response, NextFunction } = require('express')
|
2022-03-19 16:13:10 +01:00
|
|
|
const Logger = require('../Logger')
|
2022-11-24 22:53:58 +01:00
|
|
|
const SocketAuthority = require('../SocketAuthority')
|
2023-07-05 01:14:44 +02:00
|
|
|
const Database = require('../Database')
|
2022-11-24 22:53:58 +01:00
|
|
|
|
|
|
|
const fs = require('../libs/fsExtra')
|
|
|
|
|
2022-09-16 01:35:56 +02:00
|
|
|
const { getPodcastFeed, findMatchingEpisodes } = require('../utils/podcastUtils')
|
2023-01-06 00:45:27 +01:00
|
|
|
const { getFileTimestampsWithIno, filePathToPOSIX } = require('../utils/fileUtils')
|
2023-12-17 19:00:11 +01:00
|
|
|
const { validateUrl } = require('../utils/index')
|
2022-03-19 16:13:10 +01:00
|
|
|
|
2023-09-07 00:48:50 +02:00
|
|
|
const Scanner = require('../scanner/Scanner')
|
|
|
|
const CoverManager = require('../managers/CoverManager')
|
|
|
|
|
2022-11-24 22:53:58 +01:00
|
|
|
const LibraryItem = require('../objects/LibraryItem')
|
|
|
|
|
2024-08-11 22:15:34 +02:00
|
|
|
/**
|
2024-08-12 00:01:25 +02:00
|
|
|
* @typedef RequestUserObject
|
2024-08-11 23:07:29 +02:00
|
|
|
* @property {import('../models/User')} user
|
2024-08-11 22:15:34 +02:00
|
|
|
*
|
2024-08-12 00:01:25 +02:00
|
|
|
* @typedef {Request & RequestUserObject} RequestWithUser
|
2024-08-11 22:15:34 +02:00
|
|
|
*/
|
|
|
|
|
2022-03-19 16:13:10 +01:00
|
|
|
class PodcastController {
|
2024-07-17 00:05:52 +02:00
|
|
|
/**
|
|
|
|
* POST /api/podcasts
|
|
|
|
* Create podcast
|
|
|
|
*
|
|
|
|
* @this import('../routers/ApiRouter')
|
|
|
|
*
|
2024-08-11 22:15:34 +02:00
|
|
|
* @param {RequestWithUser} req
|
|
|
|
* @param {Response} res
|
2024-07-17 00:05:52 +02:00
|
|
|
*/
|
2022-03-19 16:13:10 +01:00
|
|
|
async create(req, res) {
|
2024-08-11 23:07:29 +02:00
|
|
|
if (!req.user.isAdminOrUp) {
|
|
|
|
Logger.error(`[PodcastController] Non-admin user "${req.user.username}" attempted to create podcast`)
|
2022-11-20 23:12:30 +01:00
|
|
|
return res.sendStatus(403)
|
2022-03-19 16:13:10 +01:00
|
|
|
}
|
|
|
|
const payload = req.body
|
|
|
|
|
2024-09-01 14:35:05 +02:00
|
|
|
const library = await Database.libraryModel.findByIdWithFolders(payload.libraryId)
|
2022-03-22 01:24:38 +01:00
|
|
|
if (!library) {
|
|
|
|
Logger.error(`[PodcastController] Create: Library not found "${payload.libraryId}"`)
|
2022-11-20 23:12:30 +01:00
|
|
|
return res.status(404).send('Library not found')
|
2022-03-22 01:24:38 +01:00
|
|
|
}
|
|
|
|
|
2024-08-24 23:09:54 +02:00
|
|
|
const folder = library.libraryFolders.find((fold) => fold.id === payload.folderId)
|
2022-03-22 01:24:38 +01:00
|
|
|
if (!folder) {
|
|
|
|
Logger.error(`[PodcastController] Create: Folder not found "${payload.folderId}"`)
|
2022-11-20 23:12:30 +01:00
|
|
|
return res.status(404).send('Folder not found')
|
2022-03-22 01:24:38 +01:00
|
|
|
}
|
|
|
|
|
2023-01-06 00:45:27 +01:00
|
|
|
const podcastPath = filePathToPOSIX(payload.path)
|
2023-02-11 22:25:54 +01:00
|
|
|
|
|
|
|
// Check if a library item with this podcast folder exists already
|
2024-06-09 20:55:53 +02:00
|
|
|
const existingLibraryItem =
|
|
|
|
(await Database.libraryItemModel.count({
|
|
|
|
where: {
|
|
|
|
path: podcastPath
|
|
|
|
}
|
|
|
|
})) > 0
|
2023-02-11 22:25:54 +01:00
|
|
|
if (existingLibraryItem) {
|
2023-08-13 00:29:08 +02:00
|
|
|
Logger.error(`[PodcastController] Podcast already exists at path "${podcastPath}"`)
|
2022-04-24 02:41:06 +02:00
|
|
|
return res.status(400).send('Podcast already exists')
|
2022-03-19 16:13:10 +01:00
|
|
|
}
|
|
|
|
|
2024-06-09 20:55:53 +02:00
|
|
|
const success = await fs
|
|
|
|
.ensureDir(podcastPath)
|
|
|
|
.then(() => true)
|
|
|
|
.catch((error) => {
|
|
|
|
Logger.error(`[PodcastController] Failed to ensure podcast dir "${podcastPath}"`, error)
|
|
|
|
return false
|
|
|
|
})
|
2022-03-19 16:13:10 +01:00
|
|
|
if (!success) return res.status(400).send('Invalid podcast path')
|
2022-03-22 01:24:38 +01:00
|
|
|
|
2023-02-11 22:25:54 +01:00
|
|
|
const libraryItemFolderStats = await getFileTimestampsWithIno(podcastPath)
|
2022-03-22 01:24:38 +01:00
|
|
|
|
2023-02-11 22:25:54 +01:00
|
|
|
let relPath = payload.path.replace(folder.fullPath, '')
|
2022-03-22 01:24:38 +01:00
|
|
|
if (relPath.startsWith('/')) relPath = relPath.slice(1)
|
2022-03-19 16:13:10 +01:00
|
|
|
|
2022-03-22 01:24:38 +01:00
|
|
|
const libraryItemPayload = {
|
|
|
|
path: podcastPath,
|
|
|
|
relPath,
|
|
|
|
folderId: payload.folderId,
|
|
|
|
libraryId: payload.libraryId,
|
|
|
|
ino: libraryItemFolderStats.ino,
|
|
|
|
mtimeMs: libraryItemFolderStats.mtimeMs || 0,
|
|
|
|
ctimeMs: libraryItemFolderStats.ctimeMs || 0,
|
|
|
|
birthtimeMs: libraryItemFolderStats.birthtimeMs || 0,
|
|
|
|
media: payload.media
|
2022-03-19 16:13:10 +01:00
|
|
|
}
|
|
|
|
|
2023-02-11 22:25:54 +01:00
|
|
|
const libraryItem = new LibraryItem()
|
2022-03-22 01:24:38 +01:00
|
|
|
libraryItem.setData('podcast', libraryItemPayload)
|
|
|
|
|
|
|
|
// Download and save cover image
|
|
|
|
if (payload.media.metadata.imageUrl) {
|
2022-03-27 00:23:33 +01:00
|
|
|
// TODO: Scan cover image to library files
|
2022-04-24 02:41:06 +02:00
|
|
|
// Podcast cover will always go into library item folder
|
2023-09-07 00:48:50 +02:00
|
|
|
const coverResponse = await CoverManager.downloadCoverFromUrl(libraryItem, payload.media.metadata.imageUrl, true)
|
2022-03-22 01:24:38 +01:00
|
|
|
if (coverResponse) {
|
|
|
|
if (coverResponse.error) {
|
|
|
|
Logger.error(`[PodcastController] Download cover error from "${payload.media.metadata.imageUrl}": ${coverResponse.error}`)
|
|
|
|
} else if (coverResponse.cover) {
|
|
|
|
libraryItem.media.coverPath = coverResponse.cover
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-03-19 16:13:10 +01:00
|
|
|
|
2023-07-05 01:14:44 +02:00
|
|
|
await Database.createLibraryItem(libraryItem)
|
2022-11-24 22:53:58 +01:00
|
|
|
SocketAuthority.emitter('item_added', libraryItem.toJSONExpanded())
|
2022-03-19 16:13:10 +01:00
|
|
|
|
|
|
|
res.json(libraryItem.toJSONExpanded())
|
2022-03-22 01:24:38 +01:00
|
|
|
|
2023-09-29 21:52:04 +02:00
|
|
|
if (payload.episodesToDownload?.length) {
|
2022-03-22 01:24:38 +01:00
|
|
|
Logger.info(`[PodcastController] Podcast created now starting ${payload.episodesToDownload.length} episode downloads`)
|
|
|
|
this.podcastManager.downloadPodcastEpisodes(libraryItem, payload.episodesToDownload)
|
|
|
|
}
|
2022-03-27 01:58:59 +01:00
|
|
|
|
|
|
|
// Turn on podcast auto download cron if not already on
|
2022-08-21 02:32:37 +02:00
|
|
|
if (libraryItem.media.autoDownloadEpisodes) {
|
|
|
|
this.cronManager.checkUpdatePodcastCron(libraryItem)
|
2022-03-27 01:58:59 +01:00
|
|
|
}
|
2022-03-19 16:13:10 +01:00
|
|
|
}
|
|
|
|
|
2023-12-17 19:00:11 +01:00
|
|
|
/**
|
|
|
|
* POST: /api/podcasts/feed
|
2024-06-09 20:55:53 +02:00
|
|
|
*
|
2023-12-17 19:00:11 +01:00
|
|
|
* @typedef getPodcastFeedReqBody
|
|
|
|
* @property {string} rssFeed
|
2024-06-09 20:55:53 +02:00
|
|
|
*
|
2024-08-12 00:01:25 +02:00
|
|
|
* @param {Request<{}, {}, getPodcastFeedReqBody, {}> & RequestUserObject} req
|
2024-08-11 22:15:34 +02:00
|
|
|
* @param {Response} res
|
2023-12-17 19:00:11 +01:00
|
|
|
*/
|
2022-09-16 01:35:56 +02:00
|
|
|
async getPodcastFeed(req, res) {
|
2024-08-11 23:07:29 +02:00
|
|
|
if (!req.user.isAdminOrUp) {
|
|
|
|
Logger.error(`[PodcastController] Non-admin user "${req.user.username}" attempted to get podcast feed`)
|
2023-12-17 18:17:35 +01:00
|
|
|
return res.sendStatus(403)
|
|
|
|
}
|
|
|
|
|
2023-12-17 19:00:11 +01:00
|
|
|
const url = validateUrl(req.body.rssFeed)
|
2022-03-19 16:13:10 +01:00
|
|
|
if (!url) {
|
2023-12-17 19:00:11 +01:00
|
|
|
return res.status(400).send('Invalid request body. "rssFeed" must be a valid URL')
|
2022-03-19 16:13:10 +01:00
|
|
|
}
|
|
|
|
|
2022-09-16 01:35:56 +02:00
|
|
|
const podcast = await getPodcastFeed(url)
|
|
|
|
if (!podcast) {
|
|
|
|
return res.status(404).send('Podcast RSS feed request failed or invalid response data')
|
|
|
|
}
|
|
|
|
res.json({ podcast })
|
2022-03-19 16:13:10 +01:00
|
|
|
}
|
2022-03-27 01:58:59 +01:00
|
|
|
|
2024-07-17 00:05:52 +02:00
|
|
|
/**
|
|
|
|
* POST: /api/podcasts/opml
|
|
|
|
*
|
|
|
|
* @this import('../routers/ApiRouter')
|
|
|
|
*
|
2024-08-11 22:15:34 +02:00
|
|
|
* @param {RequestWithUser} req
|
|
|
|
* @param {Response} res
|
2024-07-17 00:05:52 +02:00
|
|
|
*/
|
2023-05-28 22:10:34 +02:00
|
|
|
async getFeedsFromOPMLText(req, res) {
|
2024-08-11 23:07:29 +02:00
|
|
|
if (!req.user.isAdminOrUp) {
|
|
|
|
Logger.error(`[PodcastController] Non-admin user "${req.user.username}" attempted to get feeds from opml`)
|
2023-12-17 18:17:35 +01:00
|
|
|
return res.sendStatus(403)
|
|
|
|
}
|
|
|
|
|
2022-05-29 18:46:45 +02:00
|
|
|
if (!req.body.opmlText) {
|
|
|
|
return res.sendStatus(400)
|
|
|
|
}
|
|
|
|
|
2024-07-17 00:05:52 +02:00
|
|
|
res.json({
|
|
|
|
feeds: this.podcastManager.getParsedOPMLFileFeeds(req.body.opmlText)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* POST: /api/podcasts/opml/create
|
|
|
|
*
|
|
|
|
* @this import('../routers/ApiRouter')
|
|
|
|
*
|
2024-08-11 22:15:34 +02:00
|
|
|
* @param {RequestWithUser} req
|
|
|
|
* @param {Response} res
|
2024-07-17 00:05:52 +02:00
|
|
|
*/
|
|
|
|
async bulkCreatePodcastsFromOpmlFeedUrls(req, res) {
|
2024-08-11 23:07:29 +02:00
|
|
|
if (!req.user.isAdminOrUp) {
|
|
|
|
Logger.error(`[PodcastController] Non-admin user "${req.user.username}" attempted to bulk create podcasts`)
|
2024-07-17 00:05:52 +02:00
|
|
|
return res.sendStatus(403)
|
|
|
|
}
|
|
|
|
|
|
|
|
const rssFeeds = req.body.feeds
|
|
|
|
if (!Array.isArray(rssFeeds) || !rssFeeds.length || rssFeeds.some((feed) => !validateUrl(feed))) {
|
|
|
|
return res.status(400).send('Invalid request body. "feeds" must be an array of RSS feed URLs')
|
|
|
|
}
|
|
|
|
|
|
|
|
const libraryId = req.body.libraryId
|
|
|
|
const folderId = req.body.folderId
|
|
|
|
if (!libraryId || !folderId) {
|
|
|
|
return res.status(400).send('Invalid request body. "libraryId" and "folderId" are required')
|
|
|
|
}
|
|
|
|
|
|
|
|
const folder = await Database.libraryFolderModel.findByPk(folderId)
|
|
|
|
if (!folder || folder.libraryId !== libraryId) {
|
|
|
|
return res.status(404).send('Folder not found')
|
|
|
|
}
|
|
|
|
const autoDownloadEpisodes = !!req.body.autoDownloadEpisodes
|
|
|
|
this.podcastManager.createPodcastsFromFeedUrls(rssFeeds, folder, autoDownloadEpisodes, this.cronManager)
|
|
|
|
|
|
|
|
res.sendStatus(200)
|
2022-05-29 18:46:45 +02:00
|
|
|
}
|
|
|
|
|
2024-08-11 22:15:34 +02:00
|
|
|
/**
|
|
|
|
* GET: /api/podcasts/:id/checknew
|
|
|
|
*
|
|
|
|
* @this import('../routers/ApiRouter')
|
|
|
|
*
|
|
|
|
* @param {RequestWithUser} req
|
|
|
|
* @param {Response} res
|
|
|
|
*/
|
2022-03-27 01:58:59 +01:00
|
|
|
async checkNewEpisodes(req, res) {
|
2024-08-11 23:07:29 +02:00
|
|
|
if (!req.user.isAdminOrUp) {
|
|
|
|
Logger.error(`[PodcastController] Non-admin user "${req.user.username}" attempted to check/download episodes`)
|
2022-11-20 23:12:30 +01:00
|
|
|
return res.sendStatus(403)
|
2022-04-30 01:29:40 +02:00
|
|
|
}
|
|
|
|
|
2022-05-02 21:41:59 +02:00
|
|
|
var libraryItem = req.libraryItem
|
2022-03-27 01:58:59 +01:00
|
|
|
if (!libraryItem.media.metadata.feedUrl) {
|
|
|
|
Logger.error(`[PodcastController] checkNewEpisodes no feed url for item ${libraryItem.id}`)
|
|
|
|
return res.status(500).send('Podcast has no rss feed url')
|
|
|
|
}
|
|
|
|
|
2022-09-03 15:06:52 +02:00
|
|
|
const maxEpisodesToDownload = !isNaN(req.query.limit) ? Number(req.query.limit) : 3
|
|
|
|
|
|
|
|
var newEpisodes = await this.podcastManager.checkAndDownloadNewEpisodes(libraryItem, maxEpisodesToDownload)
|
2022-03-27 01:58:59 +01:00
|
|
|
res.json({
|
|
|
|
episodes: newEpisodes || []
|
|
|
|
})
|
|
|
|
}
|
2022-03-27 22:37:04 +02:00
|
|
|
|
2024-08-11 22:15:34 +02:00
|
|
|
/**
|
|
|
|
* GET: /api/podcasts/:id/clear-queue
|
|
|
|
*
|
|
|
|
* @this {import('../routers/ApiRouter')}
|
|
|
|
*
|
|
|
|
* @param {RequestWithUser} req
|
|
|
|
* @param {Response} res
|
|
|
|
*/
|
2022-04-24 02:41:06 +02:00
|
|
|
clearEpisodeDownloadQueue(req, res) {
|
2024-08-11 23:07:29 +02:00
|
|
|
if (!req.user.isAdminOrUp) {
|
|
|
|
Logger.error(`[PodcastController] Non-admin user "${req.user.username}" attempting to clear download queue`)
|
2022-11-20 23:12:30 +01:00
|
|
|
return res.sendStatus(403)
|
2022-04-24 02:41:06 +02:00
|
|
|
}
|
|
|
|
this.podcastManager.clearDownloadQueue(req.params.id)
|
|
|
|
res.sendStatus(200)
|
|
|
|
}
|
|
|
|
|
2024-08-11 22:15:34 +02:00
|
|
|
/**
|
|
|
|
* GET: /api/podcasts/:id/downloads
|
|
|
|
*
|
|
|
|
* @this {import('../routers/ApiRouter')}
|
|
|
|
*
|
|
|
|
* @param {RequestWithUser} req
|
|
|
|
* @param {Response} res
|
|
|
|
*/
|
2022-04-24 02:41:06 +02:00
|
|
|
getEpisodeDownloads(req, res) {
|
2022-05-02 21:41:59 +02:00
|
|
|
var libraryItem = req.libraryItem
|
|
|
|
|
2022-04-24 02:41:06 +02:00
|
|
|
var downloadsInQueue = this.podcastManager.getEpisodeDownloadsInQueue(libraryItem.id)
|
|
|
|
res.json({
|
2024-06-09 20:55:53 +02:00
|
|
|
downloads: downloadsInQueue.map((d) => d.toJSONForClient())
|
2022-04-24 02:41:06 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-07-31 20:12:37 +02:00
|
|
|
async findEpisode(req, res) {
|
|
|
|
const rssFeedUrl = req.libraryItem.media.metadata.feedUrl
|
|
|
|
if (!rssFeedUrl) {
|
|
|
|
Logger.error(`[PodcastController] findEpisode: Podcast has no feed url`)
|
|
|
|
return res.status(500).send('Podcast does not have an RSS feed URL')
|
|
|
|
}
|
|
|
|
|
2024-06-09 20:55:53 +02:00
|
|
|
const searchTitle = req.query.title
|
|
|
|
if (!searchTitle || typeof searchTitle !== 'string') {
|
2022-07-31 20:12:37 +02:00
|
|
|
return res.sendStatus(500)
|
|
|
|
}
|
2022-09-16 01:35:56 +02:00
|
|
|
const episodes = await findMatchingEpisodes(rssFeedUrl, searchTitle)
|
2022-07-31 20:12:37 +02:00
|
|
|
res.json({
|
|
|
|
episodes: episodes || []
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-08-11 22:15:34 +02:00
|
|
|
/**
|
|
|
|
* POST: /api/podcasts/:id/download-episodes
|
|
|
|
*
|
|
|
|
* @this {import('../routers/ApiRouter')}
|
|
|
|
*
|
|
|
|
* @param {RequestWithUser} req
|
|
|
|
* @param {Response} res
|
|
|
|
*/
|
2022-03-27 22:37:04 +02:00
|
|
|
async downloadEpisodes(req, res) {
|
2024-08-11 23:07:29 +02:00
|
|
|
if (!req.user.isAdminOrUp) {
|
|
|
|
Logger.error(`[PodcastController] Non-admin user "${req.user.username}" attempted to download episodes`)
|
2023-01-05 01:13:46 +01:00
|
|
|
return res.sendStatus(403)
|
2022-04-30 01:29:40 +02:00
|
|
|
}
|
2023-10-17 00:47:44 +02:00
|
|
|
const libraryItem = req.libraryItem
|
|
|
|
const episodes = req.body
|
|
|
|
if (!episodes?.length) {
|
2022-03-27 22:37:04 +02:00
|
|
|
return res.sendStatus(400)
|
|
|
|
}
|
|
|
|
|
|
|
|
this.podcastManager.downloadPodcastEpisodes(libraryItem, episodes)
|
|
|
|
res.sendStatus(200)
|
|
|
|
}
|
|
|
|
|
2024-08-11 22:15:34 +02:00
|
|
|
/**
|
|
|
|
* POST: /api/podcasts/:id/match-episodes
|
|
|
|
*
|
|
|
|
* @this {import('../routers/ApiRouter')}
|
|
|
|
*
|
|
|
|
* @param {RequestWithUser} req
|
|
|
|
* @param {Response} res
|
|
|
|
*/
|
2023-01-05 01:13:46 +01:00
|
|
|
async quickMatchEpisodes(req, res) {
|
2024-08-11 23:07:29 +02:00
|
|
|
if (!req.user.isAdminOrUp) {
|
|
|
|
Logger.error(`[PodcastController] Non-admin user "${req.user.username}" attempted to download episodes`)
|
2023-01-05 01:13:46 +01:00
|
|
|
return res.sendStatus(403)
|
|
|
|
}
|
|
|
|
|
|
|
|
const overrideDetails = req.query.override === '1'
|
2023-09-07 00:48:50 +02:00
|
|
|
const episodesUpdated = await Scanner.quickMatchPodcastEpisodes(req.libraryItem, { overrideDetails })
|
2023-01-05 01:13:46 +01:00
|
|
|
if (episodesUpdated) {
|
2023-07-05 01:14:44 +02:00
|
|
|
await Database.updateLibraryItem(req.libraryItem)
|
2023-01-05 01:13:46 +01:00
|
|
|
SocketAuthority.emitter('item_updated', req.libraryItem.toJSONExpanded())
|
|
|
|
}
|
|
|
|
|
|
|
|
res.json({
|
|
|
|
numEpisodesUpdated: episodesUpdated
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-08-11 22:15:34 +02:00
|
|
|
/**
|
|
|
|
* PATCH: /api/podcasts/:id/episode/:episodeId
|
|
|
|
*
|
|
|
|
* @param {RequestWithUser} req
|
|
|
|
* @param {Response} res
|
|
|
|
*/
|
2022-05-02 21:41:59 +02:00
|
|
|
async updateEpisode(req, res) {
|
2023-01-05 01:13:46 +01:00
|
|
|
const libraryItem = req.libraryItem
|
2022-05-02 21:41:59 +02:00
|
|
|
|
2022-03-27 22:37:04 +02:00
|
|
|
var episodeId = req.params.episodeId
|
|
|
|
if (!libraryItem.media.checkHasEpisode(episodeId)) {
|
2022-07-31 20:12:37 +02:00
|
|
|
return res.status(404).send('Episode not found')
|
2022-03-27 22:37:04 +02:00
|
|
|
}
|
|
|
|
|
2023-07-05 01:14:44 +02:00
|
|
|
if (libraryItem.media.updateEpisode(episodeId, req.body)) {
|
|
|
|
await Database.updateLibraryItem(libraryItem)
|
2022-11-24 22:53:58 +01:00
|
|
|
SocketAuthority.emitter('item_updated', libraryItem.toJSONExpanded())
|
2022-03-27 22:37:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
res.json(libraryItem.toJSONExpanded())
|
|
|
|
}
|
2022-05-02 21:41:59 +02:00
|
|
|
|
2024-08-11 22:15:34 +02:00
|
|
|
/**
|
|
|
|
* GET: /api/podcasts/:id/episode/:episodeId
|
|
|
|
*
|
|
|
|
* @param {RequestWithUser} req
|
|
|
|
* @param {Response} res
|
|
|
|
*/
|
2023-03-04 20:04:55 +01:00
|
|
|
async getEpisode(req, res) {
|
2023-03-05 19:28:20 +01:00
|
|
|
const episodeId = req.params.episodeId
|
|
|
|
const libraryItem = req.libraryItem
|
2023-03-04 20:04:55 +01:00
|
|
|
|
2024-06-09 20:55:53 +02:00
|
|
|
const episode = libraryItem.media.episodes.find((ep) => ep.id === episodeId)
|
2023-03-04 20:04:55 +01:00
|
|
|
if (!episode) {
|
2023-03-05 19:28:20 +01:00
|
|
|
Logger.error(`[PodcastController] getEpisode episode ${episodeId} not found for item ${libraryItem.id}`)
|
|
|
|
return res.sendStatus(404)
|
2023-03-04 20:04:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
res.json(episode)
|
|
|
|
}
|
|
|
|
|
2024-08-11 22:15:34 +02:00
|
|
|
/**
|
|
|
|
* DELETE: /api/podcasts/:id/episode/:episodeId
|
|
|
|
*
|
|
|
|
* @param {RequestWithUser} req
|
|
|
|
* @param {Response} res
|
|
|
|
*/
|
2022-05-25 01:38:25 +02:00
|
|
|
async removeEpisode(req, res) {
|
2023-07-23 16:42:57 +02:00
|
|
|
const episodeId = req.params.episodeId
|
|
|
|
const libraryItem = req.libraryItem
|
|
|
|
const hardDelete = req.query.hard === '1'
|
2022-05-25 01:38:25 +02:00
|
|
|
|
2024-06-09 20:55:53 +02:00
|
|
|
const episode = libraryItem.media.episodes.find((ep) => ep.id === episodeId)
|
2022-05-25 01:38:25 +02:00
|
|
|
if (!episode) {
|
|
|
|
Logger.error(`[PodcastController] removeEpisode episode ${episodeId} not found for item ${libraryItem.id}`)
|
|
|
|
return res.sendStatus(404)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hardDelete) {
|
2023-07-23 16:42:57 +02:00
|
|
|
const audioFile = episode.audioFile
|
2022-05-25 01:38:25 +02:00
|
|
|
// TODO: this will trigger the watcher. should maybe handle this gracefully
|
2024-06-09 20:55:53 +02:00
|
|
|
await fs
|
|
|
|
.remove(audioFile.metadata.path)
|
|
|
|
.then(() => {
|
|
|
|
Logger.info(`[PodcastController] Hard deleted episode file at "${audioFile.metadata.path}"`)
|
|
|
|
})
|
|
|
|
.catch((error) => {
|
|
|
|
Logger.error(`[PodcastController] Failed to hard delete episode file at "${audioFile.metadata.path}"`, error)
|
|
|
|
})
|
2022-05-25 01:38:25 +02:00
|
|
|
}
|
|
|
|
|
2022-06-02 00:45:52 +02:00
|
|
|
// Remove episode from Podcast and library file
|
|
|
|
const episodeRemoved = libraryItem.media.removeEpisode(episodeId)
|
2023-08-06 21:18:51 +02:00
|
|
|
if (episodeRemoved?.audioFile) {
|
2022-06-02 00:45:52 +02:00
|
|
|
libraryItem.removeLibraryFile(episodeRemoved.audioFile.ino)
|
|
|
|
}
|
2022-05-25 01:38:25 +02:00
|
|
|
|
2023-07-23 16:42:57 +02:00
|
|
|
// Update/remove playlists that had this podcast episode
|
2023-08-20 20:34:03 +02:00
|
|
|
const playlistMediaItems = await Database.playlistMediaItemModel.findAll({
|
2023-08-13 00:29:08 +02:00
|
|
|
where: {
|
|
|
|
mediaItemId: episodeId
|
|
|
|
},
|
|
|
|
include: {
|
2023-08-20 20:34:03 +02:00
|
|
|
model: Database.playlistModel,
|
|
|
|
include: Database.playlistMediaItemModel
|
2023-08-13 00:29:08 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
for (const pmi of playlistMediaItems) {
|
|
|
|
const numItems = pmi.playlist.playlistMediaItems.length - 1
|
2023-07-23 16:42:57 +02:00
|
|
|
|
2023-08-13 00:29:08 +02:00
|
|
|
if (!numItems) {
|
2023-10-13 20:33:15 +02:00
|
|
|
Logger.info(`[PodcastController] Playlist "${pmi.playlist.name}" has no more items - removing it`)
|
2023-08-13 00:29:08 +02:00
|
|
|
const jsonExpanded = await pmi.playlist.getOldJsonExpanded()
|
|
|
|
SocketAuthority.clientEmitter(pmi.playlist.userId, 'playlist_removed', jsonExpanded)
|
|
|
|
await pmi.playlist.destroy()
|
2023-07-23 16:42:57 +02:00
|
|
|
} else {
|
2023-08-13 00:29:08 +02:00
|
|
|
await pmi.destroy()
|
|
|
|
const jsonExpanded = await pmi.playlist.getOldJsonExpanded()
|
|
|
|
SocketAuthority.clientEmitter(pmi.playlist.userId, 'playlist_updated', jsonExpanded)
|
2023-07-23 16:42:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-06 21:18:51 +02:00
|
|
|
// Remove media progress for this episode
|
2023-08-20 20:34:03 +02:00
|
|
|
const mediaProgressRemoved = await Database.mediaProgressModel.destroy({
|
2023-08-06 21:18:51 +02:00
|
|
|
where: {
|
|
|
|
mediaItemId: episode.id
|
|
|
|
}
|
|
|
|
})
|
|
|
|
if (mediaProgressRemoved) {
|
|
|
|
Logger.info(`[PodcastController] Removed ${mediaProgressRemoved} media progress for episode ${episode.id}`)
|
|
|
|
}
|
|
|
|
|
2023-07-05 01:14:44 +02:00
|
|
|
await Database.updateLibraryItem(libraryItem)
|
2022-11-24 22:53:58 +01:00
|
|
|
SocketAuthority.emitter('item_updated', libraryItem.toJSONExpanded())
|
2022-05-25 01:38:25 +02:00
|
|
|
res.json(libraryItem.toJSON())
|
|
|
|
}
|
|
|
|
|
2024-08-11 22:15:34 +02:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param {RequestWithUser} req
|
|
|
|
* @param {Response} res
|
|
|
|
* @param {NextFunction} next
|
|
|
|
*/
|
2023-08-13 00:29:08 +02:00
|
|
|
async middleware(req, res, next) {
|
2023-08-20 20:34:03 +02:00
|
|
|
const item = await Database.libraryItemModel.getOldById(req.params.id)
|
2023-08-13 00:29:08 +02:00
|
|
|
if (!item?.media) return res.sendStatus(404)
|
2022-05-02 21:41:59 +02:00
|
|
|
|
|
|
|
if (!item.isPodcast) {
|
|
|
|
return res.sendStatus(500)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check user can access this library item
|
2024-08-11 23:07:29 +02:00
|
|
|
if (!req.user.checkCanAccessLibraryItem(item)) {
|
2022-05-02 21:41:59 +02:00
|
|
|
return res.sendStatus(403)
|
|
|
|
}
|
|
|
|
|
2024-08-11 23:07:29 +02:00
|
|
|
if (req.method == 'DELETE' && !req.user.canDelete) {
|
|
|
|
Logger.warn(`[PodcastController] User "${req.user.username}" attempted to delete without permission`)
|
2022-05-02 21:41:59 +02:00
|
|
|
return res.sendStatus(403)
|
2024-08-11 23:07:29 +02:00
|
|
|
} else if ((req.method == 'PATCH' || req.method == 'POST') && !req.user.canUpdate) {
|
|
|
|
Logger.warn(`[PodcastController] User "${req.user.username}" attempted to update without permission`)
|
2022-05-02 21:41:59 +02:00
|
|
|
return res.sendStatus(403)
|
|
|
|
}
|
|
|
|
|
|
|
|
req.libraryItem = item
|
|
|
|
next()
|
|
|
|
}
|
2022-03-19 16:13:10 +01:00
|
|
|
}
|
2023-03-04 20:04:55 +01:00
|
|
|
module.exports = new PodcastController()
|