1 import * as express from 'express'
2 import { join } from 'path'
3 import * as validator from 'validator'
4 import * as Bluebird from 'bluebird'
6 import { database as db } from '../initializers/database'
11 OPENGRAPH_AND_OEMBED_COMMENT
12 } from '../initializers'
13 import { root, readFileBufferPromise, escapeHTML } from '../helpers'
14 import { asyncMiddleware } from '../middlewares'
15 import { VideoInstance } from '../models'
17 const clientsRouter = express.Router()
19 const distPath = join(root(), 'client', 'dist')
20 const embedPath = join(distPath, 'standalone', 'videos', 'embed.html')
21 const indexPath = join(distPath, 'index.html')
23 // Special route that add OpenGraph and oEmbed tags
24 // Do not use a template engine for a so little thing
25 clientsRouter.use('/videos/watch/:id',
26 asyncMiddleware(generateWatchHtmlPage)
29 clientsRouter.use('/videos/embed', (req: express.Request, res: express.Response, next: express.NextFunction) => {
30 res.sendFile(embedPath)
33 // Static HTML/CSS/JS client files
34 clientsRouter.use('/client', express.static(distPath, { maxAge: STATIC_MAX_AGE }))
36 // 404 for static files not found
37 clientsRouter.use('/client/*', (req: express.Request, res: express.Response, next: express.NextFunction) => {
41 // ---------------------------------------------------------------------------
47 // ---------------------------------------------------------------------------
49 function addOpenGraphAndOEmbedTags (htmlStringPage: string, video: VideoInstance) {
50 const previewUrl = CONFIG.WEBSERVER.URL + STATIC_PATHS.PREVIEWS + video.getPreviewName()
51 const videoUrl = CONFIG.WEBSERVER.URL + '/videos/watch/' + video.uuid
53 const videoName = escapeHTML(video.name)
54 const videoDescription = escapeHTML(video.description)
56 const openGraphMetaTags = {
58 'og:title': videoName,
59 'og:image': previewUrl,
61 'og:description': videoDescription,
64 'description': videoDescription,
67 'twitter:card': 'summary_large_image',
68 'twitter:site': '@Chocobozzz',
69 'twitter:title': videoName,
70 'twitter:description': videoDescription,
71 'twitter:image': previewUrl
74 const oembedLinkTags = [
76 type: 'application/json+oembed',
77 href: CONFIG.WEBSERVER.URL + '/services/oembed?url=' + encodeURIComponent(videoUrl),
83 Object.keys(openGraphMetaTags).forEach(tagName => {
84 const tagValue = openGraphMetaTags[tagName]
86 tagsString += `<meta property="${tagName}" content="${tagValue}" />`
89 for (const oembedLinkTag of oembedLinkTags) {
90 tagsString += `<link rel="alternate" type="${oembedLinkTag.type}" href="${oembedLinkTag.href}" title="${oembedLinkTag.title}" />`
93 return htmlStringPage.replace(OPENGRAPH_AND_OEMBED_COMMENT, tagsString)
96 async function generateWatchHtmlPage (req: express.Request, res: express.Response, next: express.NextFunction) {
97 const videoId = '' + req.params.id
98 let videoPromise: Bluebird<VideoInstance>
100 // Let Angular application handle errors
101 if (validator.isUUID(videoId, 4)) {
102 videoPromise = db.Video.loadByUUIDAndPopulateAuthorAndPodAndTags(videoId)
103 } else if (validator.isInt(videoId)) {
104 videoPromise = db.Video.loadAndPopulateAuthorAndPodAndTags(+videoId)
106 return res.sendFile(indexPath)
109 let [ file, video ] = await Promise.all([
110 readFileBufferPromise(indexPath),
114 file = file as Buffer
115 video = video as VideoInstance
117 const html = file.toString()
119 // Let Angular application handle errors
120 if (!video) return res.sendFile(indexPath)
122 const htmlStringPageWithTags = addOpenGraphAndOEmbedTags(html, video)
123 res.set('Content-Type', 'text/html; charset=UTF-8').send(htmlStringPageWithTags)