2022-05-02 21:41:59 +02:00
|
|
|
const Path = require('path')
|
2022-05-03 00:21:16 +02:00
|
|
|
const fs = require('fs-extra')
|
2022-06-08 01:29:43 +02:00
|
|
|
const Feed = require('../objects/Feed')
|
2022-05-02 21:41:59 +02:00
|
|
|
const Logger = require('../Logger')
|
|
|
|
|
|
|
|
// Not functional at the moment
|
|
|
|
class RssFeedManager {
|
2022-05-02 23:42:30 +02:00
|
|
|
constructor(db, emitter) {
|
2022-05-02 21:41:59 +02:00
|
|
|
this.db = db
|
2022-05-02 23:42:30 +02:00
|
|
|
this.emitter = emitter
|
2022-05-02 21:41:59 +02:00
|
|
|
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-05-02 21:41:59 +02:00
|
|
|
getFeed(req, res) {
|
2022-06-08 01:29:43 +02:00
|
|
|
var feed = this.feeds[req.params.id]
|
|
|
|
if (!feed) {
|
2022-05-02 21:41:59 +02:00
|
|
|
Logger.error(`[RssFeedManager] Feed not found ${req.params.id}`)
|
|
|
|
res.sendStatus(404)
|
|
|
|
return
|
|
|
|
}
|
2022-06-08 02:25:14 +02:00
|
|
|
|
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-05-02 21:41:59 +02:00
|
|
|
Logger.error(`[RssFeedManager] Feed not found ${req.params.id}`)
|
|
|
|
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-05-02 23:42:30 +02:00
|
|
|
Logger.error(`[RssFeedManager] Feed not found ${req.params.id}`)
|
|
|
|
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-06-08 01:29:43 +02:00
|
|
|
const extname = Path.extname(feedData.coverPath).toLowerCase().slice(1)
|
2022-05-02 23:42:30 +02:00
|
|
|
res.type(`image/${extname}`)
|
2022-06-08 01:29:43 +02:00
|
|
|
var readStream = fs.createReadStream(feedData.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)
|
|
|
|
this.emitter('rss_feed_open', { entityType: feed.entityType, entityId: feed.entityId, feedUrl: feed.feedUrl })
|
|
|
|
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)
|
|
|
|
this.emitter('rss_feed_closed', { entityType: feed.entityType, entityId: feed.entityId, feedUrl: feed.feedUrl })
|
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
|