]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/controllers/client.ts
Fix sharedInboxUrl list
[github/Chocobozzz/PeerTube.git] / server / controllers / client.ts
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
10 const clientsRouter = express.Router()
11
12 const distPath = join(root(), 'client', 'dist')
13 const assetsImagesPath = join(root(), 'client', 'dist', 'client', 'assets', 'images')
14 const embedPath = join(distPath, 'standalone', 'videos', 'embed.html')
15 const indexPath = join(distPath, 'index.html')
16
17 // Special route that add OpenGraph and oEmbed tags
18 // Do not use a template engine for a so little thing
19 clientsRouter.use('/videos/watch/:id',
20 asyncMiddleware(generateWatchHtmlPage)
21 )
22
23 clientsRouter.use('/videos/embed', (req: express.Request, res: express.Response, next: express.NextFunction) => {
24 res.sendFile(embedPath)
25 })
26
27 // Static HTML/CSS/JS client files
28 clientsRouter.use('/client', express.static(distPath, { maxAge: STATIC_MAX_AGE }))
29 clientsRouter.use('/client/assets/images', express.static(assetsImagesPath, { maxAge: STATIC_MAX_AGE }))
30
31 // 404 for static files not found
32 clientsRouter.use('/client/*', (req: express.Request, res: express.Response, next: express.NextFunction) => {
33 res.sendStatus(404)
34 })
35
36 // ---------------------------------------------------------------------------
37
38 export {
39 clientsRouter
40 }
41
42 // ---------------------------------------------------------------------------
43
44 function addOpenGraphAndOEmbedTags (htmlStringPage: string, video: VideoModel) {
45 const previewUrl = CONFIG.WEBSERVER.URL + STATIC_PATHS.PREVIEWS + video.getPreviewName()
46 const videoUrl = CONFIG.WEBSERVER.URL + '/videos/watch/' + video.uuid
47
48 const videoNameEscaped = escapeHTML(video.name)
49 const videoDescriptionEscaped = escapeHTML(video.description)
50 const embedUrl = CONFIG.WEBSERVER.URL + video.getEmbedPath()
51
52 const openGraphMetaTags = {
53 'og:type': 'video',
54 'og:title': videoNameEscaped,
55 'og:image': previewUrl,
56 'og:url': videoUrl,
57 'og:description': videoDescriptionEscaped,
58
59 'og:video:url': embedUrl,
60 'og:video:secure_url': embedUrl,
61 'og:video:type': 'text/html',
62 'og:video:width': EMBED_SIZE.width,
63 'og:video:height': EMBED_SIZE.height,
64
65 'name': videoNameEscaped,
66 'description': videoDescriptionEscaped,
67 'image': previewUrl,
68
69 'twitter:card': 'summary_large_image',
70 'twitter:site': '@Chocobozzz',
71 'twitter:title': videoNameEscaped,
72 'twitter:description': videoDescriptionEscaped,
73 'twitter:image': previewUrl,
74 'twitter:player': embedUrl,
75 'twitter:player:width': EMBED_SIZE.width,
76 'twitter:player:height': EMBED_SIZE.height
77 }
78
79 const oembedLinkTags = [
80 {
81 type: 'application/json+oembed',
82 href: CONFIG.WEBSERVER.URL + '/services/oembed?url=' + encodeURIComponent(videoUrl),
83 title: videoNameEscaped
84 }
85 ]
86
87 let tagsString = ''
88 Object.keys(openGraphMetaTags).forEach(tagName => {
89 const tagValue = openGraphMetaTags[tagName]
90
91 tagsString += `<meta property="${tagName}" content="${tagValue}" />`
92 })
93
94 for (const oembedLinkTag of oembedLinkTags) {
95 tagsString += `<link rel="alternate" type="${oembedLinkTag.type}" href="${oembedLinkTag.href}" title="${oembedLinkTag.title}" />`
96 }
97
98 return htmlStringPage.replace(OPENGRAPH_AND_OEMBED_COMMENT, tagsString)
99 }
100
101 async function generateWatchHtmlPage (req: express.Request, res: express.Response, next: express.NextFunction) {
102 const videoId = '' + req.params.id
103 let videoPromise: Bluebird<VideoModel>
104
105 // Let Angular application handle errors
106 if (validator.isUUID(videoId, 4)) {
107 videoPromise = VideoModel.loadByUUIDAndPopulateAccountAndServerAndTags(videoId)
108 } else if (validator.isInt(videoId)) {
109 videoPromise = VideoModel.loadAndPopulateAccountAndServerAndTags(+videoId)
110 } else {
111 return res.sendFile(indexPath)
112 }
113
114 let [ file, video ] = await Promise.all([
115 readFileBufferPromise(indexPath),
116 videoPromise
117 ])
118
119 const html = file.toString()
120
121 // Let Angular application handle errors
122 if (!video) return res.sendFile(indexPath)
123
124 const htmlStringPageWithTags = addOpenGraphAndOEmbedTags(html, video)
125 res.set('Content-Type', 'text/html; charset=UTF-8').send(htmlStringPageWithTags)
126 }