1 import * as express from 'express'
2 import { join } from 'path'
3 import * as validator from 'validator'
4 import * as Promise 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 { VideoInstance } from '../models'
16 const clientsRouter = express.Router()
18 const distPath = join(root(), 'client', 'dist')
19 const embedPath = join(distPath, 'standalone', 'videos', 'embed.html')
20 const indexPath = join(distPath, 'index.html')
22 // Special route that add OpenGraph and oEmbed tags
23 // Do not use a template engine for a so little thing
24 clientsRouter.use('/videos/watch/:id', generateWatchHtmlPage)
26 clientsRouter.use('/videos/embed', (req: express.Request, res: express.Response, next: express.NextFunction) => {
27 res.sendFile(embedPath)
30 // Static HTML/CSS/JS client files
31 clientsRouter.use('/client', express.static(distPath, { maxAge: STATIC_MAX_AGE }))
33 // 404 for static files not found
34 clientsRouter.use('/client/*', (req: express.Request, res: express.Response, next: express.NextFunction) => {
38 // ---------------------------------------------------------------------------
44 // ---------------------------------------------------------------------------
46 function addOpenGraphAndOEmbedTags (htmlStringPage: string, video: VideoInstance) {
47 const previewUrl = CONFIG.WEBSERVER.URL + STATIC_PATHS.PREVIEWS + video.getPreviewName()
48 const videoUrl = CONFIG.WEBSERVER.URL + '/videos/watch/' + video.uuid
50 const videoName = escapeHTML(video.name)
51 const videoDescription = escapeHTML(video.description)
53 const openGraphMetaTags = {
55 'og:title': videoName,
56 'og:image': previewUrl,
58 'og:description': videoDescription,
61 'description': videoDescription,
64 'twitter:card': 'summary_large_image',
65 'twitter:site': '@Chocobozzz',
66 'twitter:title': videoName,
67 'twitter:description': videoDescription,
68 'twitter:image': previewUrl
71 const oembedLinkTags = [
73 type: 'application/json+oembed',
74 href: CONFIG.WEBSERVER.URL + '/services/oembed?url=' + encodeURIComponent(videoUrl),
80 Object.keys(openGraphMetaTags).forEach(tagName => {
81 const tagValue = openGraphMetaTags[tagName]
83 tagsString += `<meta property="${tagName}" content="${tagValue}" />`
86 for (const oembedLinkTag of oembedLinkTags) {
87 tagsString += `<link rel="alternate" type="${oembedLinkTag.type}" href="${oembedLinkTag.href}" title="${oembedLinkTag.title}" />`
90 return htmlStringPage.replace(OPENGRAPH_AND_OEMBED_COMMENT, tagsString)
93 function generateWatchHtmlPage (req: express.Request, res: express.Response, next: express.NextFunction) {
94 const videoId = '' + req.params.id
95 let videoPromise: Promise<VideoInstance>
97 // Let Angular application handle errors
98 if (validator.isUUID(videoId, 4)) {
99 videoPromise = db.Video.loadByUUIDAndPopulateAuthorAndPodAndTags(videoId)
100 } else if (validator.isInt(videoId)) {
101 videoPromise = db.Video.loadAndPopulateAuthorAndPodAndTags(+videoId)
103 return res.sendFile(indexPath)
107 readFileBufferPromise(indexPath),
110 .then(([ file, video ]) => {
111 file = file as Buffer
112 video = video as VideoInstance
114 const html = file.toString()
116 // Let Angular application handle errors
117 if (!video) return res.sendFile(indexPath)
119 const htmlStringPageWithTags = addOpenGraphAndOEmbedTags(html, video)
120 res.set('Content-Type', 'text/html; charset=UTF-8').send(htmlStringPageWithTags)
122 .catch(err => next(err))