1
0
Fork 0
peertube/server/controllers/client.ts

109 lines
3.2 KiB
TypeScript
Raw Normal View History

2017-06-05 15:53:49 -04:00
import * as express from 'express'
2017-05-15 16:22:03 -04:00
import { join } from 'path'
2017-06-05 15:53:49 -04:00
import * as validator from 'validator'
import * as Promise from 'bluebird'
2016-11-11 04:55:07 -05:00
2017-05-22 14:58:25 -04:00
import { database as db } from '../initializers/database'
2017-05-15 16:22:03 -04:00
import {
CONFIG,
STATIC_PATHS,
2017-07-07 10:57:28 -04:00
STATIC_MAX_AGE,
OPENGRAPH_COMMENT
2017-05-15 16:22:03 -04:00
} from '../initializers'
import { root, readFileBufferPromise } from '../helpers'
2017-06-10 16:15:25 -04:00
import { VideoInstance } from '../models'
2016-11-11 04:55:07 -05:00
2017-05-15 16:22:03 -04:00
const clientsRouter = express.Router()
2016-11-11 04:55:07 -05:00
2017-05-22 14:58:25 -04:00
const distPath = join(root(), 'client', 'dist')
const embedPath = join(distPath, 'standalone', 'videos', 'embed.html')
2017-05-15 16:22:03 -04:00
const indexPath = join(distPath, 'index.html')
2016-11-11 04:55:07 -05:00
// Special route that add OpenGraph tags
// Do not use a template engine for a so little thing
2017-05-15 16:22:03 -04:00
clientsRouter.use('/videos/watch/:id', generateWatchHtmlPage)
2016-11-11 04:55:07 -05:00
2017-07-11 11:04:57 -04:00
clientsRouter.use('/videos/embed', (req: express.Request, res: express.Response, next: express.NextFunction) => {
2016-11-11 04:55:07 -05:00
res.sendFile(embedPath)
})
// Static HTML/CSS/JS client files
2017-05-15 16:22:03 -04:00
clientsRouter.use('/client', express.static(distPath, { maxAge: STATIC_MAX_AGE }))
// 404 for static files not found
2017-07-11 11:04:57 -04:00
clientsRouter.use('/client/*', (req: express.Request, res: express.Response, next: express.NextFunction) => {
res.sendStatus(404)
})
2016-11-11 04:55:07 -05:00
// ---------------------------------------------------------------------------
2017-05-15 16:22:03 -04:00
export {
clientsRouter
}
2016-11-11 04:55:07 -05:00
// ---------------------------------------------------------------------------
2017-06-10 16:15:25 -04:00
function addOpenGraphTags (htmlStringPage: string, video: VideoInstance) {
const previewUrl = CONFIG.WEBSERVER.URL + STATIC_PATHS.PREVIEWS + video.getPreviewName()
2017-05-15 16:22:03 -04:00
const videoUrl = CONFIG.WEBSERVER.URL + '/videos/watch/' + video.id
2016-11-11 04:55:07 -05:00
const metaTags = {
'og:type': 'video',
'og:title': video.name,
'og:image': previewUrl,
2016-11-11 04:55:07 -05:00
'og:url': videoUrl,
'og:description': video.description,
'name': video.name,
'description': video.description,
'image': previewUrl,
2016-11-11 04:55:07 -05:00
'twitter:card': 'summary_large_image',
'twitter:site': '@Chocobozzz',
'twitter:title': video.name,
'twitter:description': video.description,
'twitter:image': previewUrl
2016-11-11 04:55:07 -05:00
}
let tagsString = ''
Object.keys(metaTags).forEach(tagName => {
2016-11-11 04:55:07 -05:00
const tagValue = metaTags[tagName]
tagsString += '<meta property="' + tagName + '" content="' + tagValue + '" />'
})
2017-07-07 10:57:28 -04:00
return htmlStringPage.replace(OPENGRAPH_COMMENT, tagsString)
2016-11-11 04:55:07 -05:00
}
2017-06-10 16:15:25 -04:00
function generateWatchHtmlPage (req: express.Request, res: express.Response, next: express.NextFunction) {
const videoId = '' + req.params.id
let videoPromise: Promise<VideoInstance>
// Let Angular application handle errors
if (validator.isUUID(videoId, 4)) {
videoPromise = db.Video.loadByUUIDAndPopulateAuthorAndPodAndTags(videoId)
} else if (validator.isInt(videoId)) {
videoPromise = db.Video.loadAndPopulateAuthorAndPodAndTags(+videoId)
} else {
return res.sendFile(indexPath)
}
Promise.all([
readFileBufferPromise(indexPath),
videoPromise
])
.then(([ file, video ]) => {
file = file as Buffer
video = video as VideoInstance
2016-11-11 04:55:07 -05:00
const html = file.toString()
2016-11-11 04:55:07 -05:00
// Let Angular application handle errors
if (!video) return res.sendFile(indexPath)
2016-11-11 04:55:07 -05:00
const htmlStringPageWithTags = addOpenGraphTags(html, video)
res.set('Content-Type', 'text/html; charset=UTF-8').send(htmlStringPageWithTags)
})
.catch(err => next(err))
2016-11-11 04:55:07 -05:00
}