1
0
mirror of https://github.com/comatory/fb2iCal synced 2025-06-05 22:09:25 +02:00

refactor retrieving ICS file

This commit is contained in:
Ondrej Synacek
2019-10-23 20:37:06 +02:00
parent 52b86953aa
commit a41992d53b
7 changed files with 91 additions and 64 deletions

View File

@ -4,10 +4,12 @@ const path = require('path')
const favicon = require('serve-favicon')
const rateLimit = require('express-rate-limit')
const crawl = require('./crawler')
const parseHTML = require('./parser')
const generateICS = require('./ics')
const { genericErrorHandler, checkURLParameter, forceSecure } = require('./middlewares')
const retrieveICS = require('./services/ics-retriever')
const {
genericErrorHandler,
checkURLParameter,
forceSecure,
} = require('./middlewares')
const port = process.env.PORT
const certEndpoint = process.env.CERT_ENDPOINT || ''
@ -50,22 +52,17 @@ app.get('*', (req, res) => {
app.use('/download', checkURLParameter)
app.use('/download', rateLimit())
app.post('/download', async (req, res, next) => {
const { url } = req.body
try {
const html = await crawl(url)
const data = parseHTML(html)
const ics = await generateICS(data)
const { url } = req.body
if (ics) {
return res
.contentType('text/calendar')
.status(200)
.send(new Buffer(ics, 'utf8'))
}
const ics = await retrieveICS(url)
res
.contentType('text/calendar')
.status(200)
.send(new Buffer(ics, 'utf8'))
} catch (err) {
next(err)
return
}
})