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,
13 } from '../initializers'
14 import { root, readFileBufferPromise, escapeHTML } from '../helpers'
15 import { asyncMiddleware } from '../middlewares'
16 import { VideoInstance } from '../models'
18 const clientsRouter = express.Router()
20 const distPath = join(root(), 'client', 'dist')
21 const assetsImagesPath = join(root(), 'client', 'dist', 'client', 'assets', 'images')
22 const embedPath = join(distPath, 'standalone', 'videos', 'embed.html')
23 const indexPath = join(distPath, 'index.html')
25 // Special route that add OpenGraph and oEmbed tags
26 // Do not use a template engine for a so little thing
27 clientsRouter.use('/videos/watch/:id',
28 asyncMiddleware(generateWatchHtmlPage)
31 clientsRouter.use('/videos/embed', (req: express.Request, res: express.Response, next: express.NextFunction) => {
32 res.sendFile(embedPath)
35 // Static HTML/CSS/JS client files
36 clientsRouter.use('/client', express.static(distPath, { maxAge: STATIC_MAX_AGE }))
37 clientsRouter.use('/client/assets/images', express.static(assetsImagesPath, { maxAge: STATIC_MAX_AGE }))
39 // 404 for static files not found
40 clientsRouter.use('/client/*', (req: express.Request, res: express.Response, next: express.NextFunction) => {
44 // ---------------------------------------------------------------------------
50 // ---------------------------------------------------------------------------
52 function addOpenGraphAndOEmbedTags (htmlStringPage: string, video: VideoInstance) {
53 const previewUrl = CONFIG.WEBSERVER.URL + STATIC_PATHS.PREVIEWS + video.getPreviewName()
54 const videoUrl = CONFIG.WEBSERVER.URL + '/videos/watch/' + video.uuid
56 const videoNameEscaped = escapeHTML(video.name)
57 const videoDescriptionEscaped = escapeHTML(video.description)
58 const embedUrl = CONFIG.WEBSERVER.URL + video.getEmbedPath()
60 const openGraphMetaTags = {
62 'og:title': videoNameEscaped,
63 'og:image': previewUrl,
65 'og:description': videoDescriptionEscaped,
67 'og:video:url': embedUrl,
68 'og:video:secure_url': embedUrl,
69 'og:video:type': 'text/html',
70 'og:video:width': EMBED_SIZE.width,
71 'og:video:height': EMBED_SIZE.height,
73 'name': videoNameEscaped,
74 'description': videoDescriptionEscaped,
77 'twitter:card': 'summary_large_image',
78 'twitter:site': '@Chocobozzz',
79 'twitter:title': videoNameEscaped,
80 'twitter:description': videoDescriptionEscaped,
81 'twitter:image': previewUrl,
82 'twitter:player': embedUrl,
83 'twitter:player:width': EMBED_SIZE.width,
84 'twitter:player:height': EMBED_SIZE.height
87 const oembedLinkTags = [
89 type: 'application/json+oembed',
90 href: CONFIG.WEBSERVER.URL + '/services/oembed?url=' + encodeURIComponent(videoUrl),
91 title: videoNameEscaped
96 Object.keys(openGraphMetaTags).forEach(tagName => {
97 const tagValue = openGraphMetaTags[tagName]
99 tagsString += `<meta property="${tagName}" content="${tagValue}" />`
102 for (const oembedLinkTag of oembedLinkTags) {
103 tagsString += `<link rel="alternate" type="${oembedLinkTag.type}" href="${oembedLinkTag.href}" title="${oembedLinkTag.title}" />`
106 return htmlStringPage.replace(OPENGRAPH_AND_OEMBED_COMMENT, tagsString)
109 async function generateWatchHtmlPage (req: express.Request, res: express.Response, next: express.NextFunction) {
110 const videoId = '' + req.params.id
111 let videoPromise: Bluebird<VideoInstance>
113 // Let Angular application handle errors
114 if (validator.isUUID(videoId, 4)) {
115 videoPromise = db.Video.loadByUUIDAndPopulateAccountAndServerAndTags(videoId)
116 } else if (validator.isInt(videoId)) {
117 videoPromise = db.Video.loadAndPopulateAccountAndServerAndTags(+videoId)
119 return res.sendFile(indexPath)
122 let [ file, video ] = await Promise.all([
123 readFileBufferPromise(indexPath),
127 const html = file.toString()
129 // Let Angular application handle errors
130 if (!video) return res.sendFile(indexPath)
132 const htmlStringPageWithTags = addOpenGraphAndOEmbedTags(html, video)
133 res.set('Content-Type', 'text/html; charset=UTF-8').send(htmlStringPageWithTags)