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 embedPath = join(distPath, 'standalone', 'videos', 'embed.html')
22 const indexPath = join(distPath, 'index.html')
24 // Special route that add OpenGraph and oEmbed tags
25 // Do not use a template engine for a so little thing
26 clientsRouter.use('/videos/watch/:id',
27 asyncMiddleware(generateWatchHtmlPage)
30 clientsRouter.use('/videos/embed', (req: express.Request, res: express.Response, next: express.NextFunction) => {
31 res.sendFile(embedPath)
34 // Static HTML/CSS/JS client files
35 clientsRouter.use('/client', express.static(distPath, { maxAge: STATIC_MAX_AGE }))
37 // 404 for static files not found
38 clientsRouter.use('/client/*', (req: express.Request, res: express.Response, next: express.NextFunction) => {
42 // ---------------------------------------------------------------------------
48 // ---------------------------------------------------------------------------
50 function addOpenGraphAndOEmbedTags (htmlStringPage: string, video: VideoInstance) {
51 const previewUrl = CONFIG.WEBSERVER.URL + STATIC_PATHS.PREVIEWS + video.getPreviewName()
52 const videoUrl = CONFIG.WEBSERVER.URL + '/videos/watch/' + video.uuid
54 const videoName = escapeHTML(video.name)
55 const videoDescription = escapeHTML(video.description)
56 const embedUrl = CONFIG.WEBSERVER.URL + video.getEmbedPath()
58 const openGraphMetaTags = {
60 'og:title': videoName,
61 'og:image': previewUrl,
63 'og:description': videoDescription,
65 'og:video:url': embedUrl,
66 'og:video:secure_url': embedUrl,
67 'og:video:type': 'text/html',
68 'og:video:width': EMBED_SIZE.width,
69 'og:video:height': EMBED_SIZE.height,
72 'description': videoDescription,
75 'twitter:card': 'summary_large_image',
76 'twitter:site': '@Chocobozzz',
77 'twitter:title': videoName,
78 'twitter:description': videoDescription,
79 'twitter:image': previewUrl,
80 'twitter:player': embedUrl,
81 'twitter:player:width': EMBED_SIZE.width,
82 'twitter:player:height': EMBED_SIZE.height
85 const oembedLinkTags = [
87 type: 'application/json+oembed',
88 href: CONFIG.WEBSERVER.URL + '/services/oembed?url=' + encodeURIComponent(videoUrl),
94 Object.keys(openGraphMetaTags).forEach(tagName => {
95 const tagValue = openGraphMetaTags[tagName]
97 tagsString += `<meta property="${tagName}" content="${tagValue}" />`
100 for (const oembedLinkTag of oembedLinkTags) {
101 tagsString += `<link rel="alternate" type="${oembedLinkTag.type}" href="${oembedLinkTag.href}" title="${oembedLinkTag.title}" />`
104 return htmlStringPage.replace(OPENGRAPH_AND_OEMBED_COMMENT, tagsString)
107 async function generateWatchHtmlPage (req: express.Request, res: express.Response, next: express.NextFunction) {
108 const videoId = '' + req.params.id
109 let videoPromise: Bluebird<VideoInstance>
111 // Let Angular application handle errors
112 if (validator.isUUID(videoId, 4)) {
113 videoPromise = db.Video.loadByUUIDAndPopulateAccountAndServerAndTags(videoId)
114 } else if (validator.isInt(videoId)) {
115 videoPromise = db.Video.loadAndPopulateAccountAndServerAndTags(+videoId)
117 return res.sendFile(indexPath)
120 let [ file, video ] = await Promise.all([
121 readFileBufferPromise(indexPath),
125 const html = file.toString()
127 // Let Angular application handle errors
128 if (!video) return res.sendFile(indexPath)
130 const htmlStringPageWithTags = addOpenGraphAndOEmbedTags(html, video)
131 res.set('Content-Type', 'text/html; charset=UTF-8').send(htmlStringPageWithTags)