2022-05-02 21:41:59 +02:00
|
|
|
const Path = require('path')
|
2022-11-24 22:53:58 +01:00
|
|
|
|
|
|
|
const Logger = require('../Logger')
|
|
|
|
const SocketAuthority = require('../SocketAuthority')
|
|
|
|
|
2022-07-06 02:53:01 +02:00
|
|
|
const fs = require('../libs/fsExtra')
|
2022-06-08 01:29:43 +02:00
|
|
|
const Feed = require('../objects/Feed')
|
2022-05-02 21:41:59 +02:00
|
|
|
|
|
|
|
class RssFeedManager {
|
2022-11-24 22:53:58 +01:00
|
|
|
constructor(db) {
|
2022-05-02 21:41:59 +02:00
|
|
|
this.db = db
|
2022-11-24 22:53:58 +01:00
|
|
|
|
2022-05-02 21:41:59 +02:00
|
|
|
this.feeds = {}
|
|
|
|
}
|
|
|
|
|
2022-08-06 02:23:18 +02:00
|
|
|
get feedsArray() {
|
|
|
|
return Object.values(this.feeds)
|
|
|
|
}
|
|
|
|
|
2022-06-08 01:29:43 +02:00
|
|
|
async init() {
|
|
|
|
var feedObjects = await this.db.getAllEntities('feed')
|
|
|
|
if (feedObjects && feedObjects.length) {
|
|
|
|
feedObjects.forEach((feedObj) => {
|
|
|
|
var feed = new Feed(feedObj)
|
|
|
|
this.feeds[feed.id] = feed
|
|
|
|
Logger.info(`[RssFeedManager] Opened rss feed ${feed.feedUrl}`)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-02 23:42:30 +02:00
|
|
|
findFeedForItem(libraryItemId) {
|
2022-06-08 01:29:43 +02:00
|
|
|
return Object.values(this.feeds).find(feed => feed.entityId === libraryItemId)
|
2022-05-02 23:42:30 +02:00
|
|
|
}
|
|
|
|
|
2022-08-28 22:41:51 +02:00
|
|
|
async getFeed(req, res) {
|
2022-06-08 01:29:43 +02:00
|
|
|
var feed = this.feeds[req.params.id]
|
|
|
|
if (!feed) {
|
2022-11-21 01:54:25 +01:00
|
|
|
Logger.debug(`[RssFeedManager] Feed not found ${req.params.id}`)
|
2022-05-02 21:41:59 +02:00
|
|
|
res.sendStatus(404)
|
|
|
|
return
|
|
|
|
}
|
2022-06-08 02:25:14 +02:00
|
|
|
|
2022-08-28 22:41:51 +02:00
|
|
|
if (feed.entityType === 'item') {
|
|
|
|
const libraryItem = this.db.getLibraryItem(feed.entityId)
|
|
|
|
if (libraryItem && (!feed.entityUpdatedAt || libraryItem.updatedAt > feed.entityUpdatedAt)) {
|
|
|
|
Logger.debug(`[RssFeedManager] Updating RSS feed for item ${libraryItem.id} "${libraryItem.media.metadata.title}"`)
|
|
|
|
feed.updateFromItem(libraryItem)
|
|
|
|
await this.db.updateEntity('feed', feed)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-08 01:29:43 +02:00
|
|
|
var xml = feed.buildXml()
|
2022-05-02 21:41:59 +02:00
|
|
|
res.set('Content-Type', 'text/xml')
|
|
|
|
res.send(xml)
|
|
|
|
}
|
|
|
|
|
|
|
|
getFeedItem(req, res) {
|
2022-06-08 01:29:43 +02:00
|
|
|
var feed = this.feeds[req.params.id]
|
|
|
|
if (!feed) {
|
2022-11-21 13:39:32 +01:00
|
|
|
Logger.debug(`[RssFeedManager] Feed not found ${req.params.id}`)
|
2022-05-02 21:41:59 +02:00
|
|
|
res.sendStatus(404)
|
|
|
|
return
|
|
|
|
}
|
2022-06-08 01:29:43 +02:00
|
|
|
var episodePath = feed.getEpisodePath(req.params.episodeId)
|
|
|
|
if (!episodePath) {
|
|
|
|
Logger.error(`[RssFeedManager] Feed episode not found ${req.params.episodeId}`)
|
|
|
|
res.sendStatus(404)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
res.sendFile(episodePath)
|
2022-05-02 21:41:59 +02:00
|
|
|
}
|
|
|
|
|
2022-05-02 23:42:30 +02:00
|
|
|
getFeedCover(req, res) {
|
2022-06-08 01:29:43 +02:00
|
|
|
var feed = this.feeds[req.params.id]
|
|
|
|
if (!feed) {
|
2022-11-21 13:39:32 +01:00
|
|
|
Logger.debug(`[RssFeedManager] Feed not found ${req.params.id}`)
|
2022-05-02 23:42:30 +02:00
|
|
|
res.sendStatus(404)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-06-08 01:29:43 +02:00
|
|
|
if (!feed.coverPath) {
|
2022-05-02 23:42:30 +02:00
|
|
|
res.sendStatus(404)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-07-19 01:39:51 +02:00
|
|
|
const extname = Path.extname(feed.coverPath).toLowerCase().slice(1)
|
2022-05-02 23:42:30 +02:00
|
|
|
res.type(`image/${extname}`)
|
2022-07-19 01:39:51 +02:00
|
|
|
var readStream = fs.createReadStream(feed.coverPath)
|
2022-05-02 23:42:30 +02:00
|
|
|
readStream.pipe(res)
|
|
|
|
}
|
|
|
|
|
2022-06-08 01:29:43 +02:00
|
|
|
async openFeedForItem(user, libraryItem, options) {
|
2022-05-02 21:41:59 +02:00
|
|
|
const serverAddress = options.serverAddress
|
2022-05-04 01:52:34 +02:00
|
|
|
const slug = options.slug
|
|
|
|
|
|
|
|
if (this.feeds[slug]) {
|
|
|
|
Logger.error(`[RssFeedManager] Slug already in use`)
|
|
|
|
return {
|
|
|
|
error: `Slug "${slug}" already in use`
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-08 01:29:43 +02:00
|
|
|
const feed = new Feed()
|
|
|
|
feed.setFromItem(user.id, slug, libraryItem, serverAddress)
|
|
|
|
this.feeds[feed.id] = feed
|
|
|
|
|
|
|
|
Logger.debug(`[RssFeedManager] Opened RSS feed ${feed.feedUrl}`)
|
|
|
|
await this.db.insertEntity('feed', feed)
|
2022-12-22 23:26:11 +01:00
|
|
|
SocketAuthority.emitter('rss_feed_open', feed.toJSONMinified())
|
2022-06-08 01:29:43 +02:00
|
|
|
return feed
|
2022-05-02 21:41:59 +02:00
|
|
|
}
|
2022-05-02 23:42:30 +02:00
|
|
|
|
2022-05-20 01:51:58 +02:00
|
|
|
closeFeedForItem(libraryItemId) {
|
2022-05-02 23:42:30 +02:00
|
|
|
var feed = this.findFeedForItem(libraryItemId)
|
|
|
|
if (!feed) return
|
2022-06-08 01:29:43 +02:00
|
|
|
return this.closeRssFeed(feed.id)
|
2022-05-02 23:42:30 +02:00
|
|
|
}
|
|
|
|
|
2022-06-08 01:29:43 +02:00
|
|
|
async closeRssFeed(id) {
|
2022-05-02 23:42:30 +02:00
|
|
|
if (!this.feeds[id]) return
|
2022-06-08 01:29:43 +02:00
|
|
|
var feed = this.feeds[id]
|
|
|
|
await this.db.removeEntity('feed', id)
|
2022-12-22 23:26:11 +01:00
|
|
|
SocketAuthority.emitter('rss_feed_closed', feed.toJSONMinified())
|
2022-05-02 23:42:30 +02:00
|
|
|
delete this.feeds[id]
|
2022-06-08 01:29:43 +02:00
|
|
|
Logger.info(`[RssFeedManager] Closed RSS feed "${feed.feedUrl}"`)
|
2022-05-02 23:42:30 +02:00
|
|
|
}
|
2022-05-02 21:41:59 +02:00
|
|
|
}
|
|
|
|
module.exports = RssFeedManager
|