1 import * as Bluebird from 'bluebird'
2 import * as express from 'express'
3 import { join } from 'path'
4 import * as validator from 'validator'
5 import { escapeHTML, readFileBufferPromise, root } from '../helpers/core-utils'
6 import { CONFIG, EMBED_SIZE, OPENGRAPH_AND_OEMBED_COMMENT, STATIC_MAX_AGE, STATIC_PATHS } from '../initializers'
7 import { asyncMiddleware } from '../middlewares'
8 import { VideoModel } from '../models/video/video'
9 import { VideoPrivacy } from '../../shared/models/videos'
11 const clientsRouter = express.Router()
13 const distPath = join(root(), 'client', 'dist')
14 const assetsImagesPath = join(root(), 'client', 'dist', 'assets', 'images')
15 const embedPath = join(distPath, 'standalone', 'videos', 'embed.html')
16 const indexPath = join(distPath, 'index.html')
18 // Special route that add OpenGraph and oEmbed tags
19 // Do not use a template engine for a so little thing
20 clientsRouter.use('/videos/watch/:id',
21 asyncMiddleware(generateWatchHtmlPage)
24 clientsRouter.use('/videos/embed', (req: express.Request, res: express.Response, next: express.NextFunction) => {
25 res.sendFile(embedPath)
28 // Static HTML/CSS/JS client files
30 const staticClientFiles = [
35 for (const staticClientFile of staticClientFiles) {
36 const path = join(root(), 'client', 'dist', staticClientFile)
37 clientsRouter.use('/' + staticClientFile, express.static(path, { maxAge: STATIC_MAX_AGE }))
40 clientsRouter.use('/client', express.static(distPath, { maxAge: STATIC_MAX_AGE }))
41 clientsRouter.use('/client/assets/images', express.static(assetsImagesPath, { maxAge: STATIC_MAX_AGE }))
43 // 404 for static files not found
44 clientsRouter.use('/client/*', (req: express.Request, res: express.Response, next: express.NextFunction) => {
48 // ---------------------------------------------------------------------------
54 // ---------------------------------------------------------------------------
56 function addOpenGraphAndOEmbedTags (htmlStringPage: string, video: VideoModel) {
57 const previewUrl = CONFIG.WEBSERVER.URL + STATIC_PATHS.PREVIEWS + video.getPreviewName()
58 const videoUrl = CONFIG.WEBSERVER.URL + '/videos/watch/' + video.uuid
60 const videoNameEscaped = escapeHTML(video.name)
61 const videoDescriptionEscaped = escapeHTML(video.description)
62 const embedUrl = CONFIG.WEBSERVER.URL + video.getEmbedPath()
64 const openGraphMetaTags = {
66 'og:title': videoNameEscaped,
67 'og:image': previewUrl,
69 'og:description': videoDescriptionEscaped,
71 'og:video:url': embedUrl,
72 'og:video:secure_url': embedUrl,
73 'og:video:type': 'text/html',
74 'og:video:width': EMBED_SIZE.width,
75 'og:video:height': EMBED_SIZE.height,
77 'name': videoNameEscaped,
78 'description': videoDescriptionEscaped,
81 'twitter:card': CONFIG.SERVICES.TWITTER.WHITELISTED ? 'player' : 'summary_large_image',
82 'twitter:site': CONFIG.SERVICES.TWITTER.USERNAME,
83 'twitter:title': videoNameEscaped,
84 'twitter:description': videoDescriptionEscaped,
85 'twitter:image': previewUrl,
86 'twitter:player': embedUrl,
87 'twitter:player:width': EMBED_SIZE.width,
88 'twitter:player:height': EMBED_SIZE.height
91 const oembedLinkTags = [
93 type: 'application/json+oembed',
94 href: CONFIG.WEBSERVER.URL + '/services/oembed?url=' + encodeURIComponent(videoUrl),
95 title: videoNameEscaped
100 '@context': 'http://schema.org',
101 '@type': 'VideoObject',
102 name: videoNameEscaped,
103 description: videoDescriptionEscaped,
104 thumbnailUrl: previewUrl,
105 uploadDate: video.createdAt.toISOString(),
106 duration: video.getActivityStreamDuration(),
107 contentUrl: videoUrl,
109 interactionCount: video.views
115 Object.keys(openGraphMetaTags).forEach(tagName => {
116 const tagValue = openGraphMetaTags[tagName]
118 tagsString += `<meta property="${tagName}" content="${tagValue}" />`
122 for (const oembedLinkTag of oembedLinkTags) {
123 tagsString += `<link rel="alternate" type="${oembedLinkTag.type}" href="${oembedLinkTag.href}" title="${oembedLinkTag.title}" />`
127 tagsString += `<script type="application/ld+json">${JSON.stringify(schemaTags)}</script>`
130 tagsString += `<link rel="canonical" href="${videoUrl}" />`
132 return htmlStringPage.replace(OPENGRAPH_AND_OEMBED_COMMENT, tagsString)
135 async function generateWatchHtmlPage (req: express.Request, res: express.Response, next: express.NextFunction) {
136 const videoId = '' + req.params.id
137 let videoPromise: Bluebird<VideoModel>
139 // Let Angular application handle errors
140 if (validator.isUUID(videoId, 4)) {
141 videoPromise = VideoModel.loadByUUIDAndPopulateAccountAndServerAndTags(videoId)
142 } else if (validator.isInt(videoId)) {
143 videoPromise = VideoModel.loadAndPopulateAccountAndServerAndTags(+videoId)
145 return res.sendFile(indexPath)
148 let [ file, video ] = await Promise.all([
149 readFileBufferPromise(indexPath),
153 const html = file.toString()
155 // Let Angular application handle errors
156 if (!video || video.privacy === VideoPrivacy.PRIVATE) return res.sendFile(indexPath)
158 const htmlStringPageWithTags = addOpenGraphAndOEmbedTags(html, video)
159 res.set('Content-Type', 'text/html; charset=UTF-8').send(htmlStringPageWithTags)