]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/client.ts
Add context on activitypub responses
[github/Chocobozzz/PeerTube.git] / server / controllers / client.ts
CommitLineData
da854ddd 1import * as Bluebird from 'bluebird'
4d4e5cd4 2import * as express from 'express'
65fcc311 3import { join } from 'path'
4d4e5cd4 4import * as validator from 'validator'
da854ddd
C
5import { escapeHTML, readFileBufferPromise, root } from '../helpers/core-utils'
6import { CONFIG, EMBED_SIZE, OPENGRAPH_AND_OEMBED_COMMENT, STATIC_MAX_AGE, STATIC_PATHS } from '../initializers'
eb080476 7import { asyncMiddleware } from '../middlewares'
3fd3ab2d 8import { VideoModel } from '../models/video/video'
830bcd0f 9
65fcc311 10const clientsRouter = express.Router()
830bcd0f 11
e02643f3 12const distPath = join(root(), 'client', 'dist')
b6827820 13const assetsImagesPath = join(root(), 'client', 'dist', 'client', 'assets', 'images')
e02643f3 14const embedPath = join(distPath, 'standalone', 'videos', 'embed.html')
65fcc311 15const indexPath = join(distPath, 'index.html')
830bcd0f 16
d8755eed 17// Special route that add OpenGraph and oEmbed tags
830bcd0f 18// Do not use a template engine for a so little thing
eb080476
C
19clientsRouter.use('/videos/watch/:id',
20 asyncMiddleware(generateWatchHtmlPage)
21)
830bcd0f 22
075f16ca 23clientsRouter.use('/videos/embed', (req: express.Request, res: express.Response, next: express.NextFunction) => {
830bcd0f
C
24 res.sendFile(embedPath)
25})
26
79530164 27// Static HTML/CSS/JS client files
65fcc311 28clientsRouter.use('/client', express.static(distPath, { maxAge: STATIC_MAX_AGE }))
6bafac54 29clientsRouter.use('/client/assets/images', express.static(assetsImagesPath, { maxAge: STATIC_MAX_AGE }))
79530164
C
30
31// 404 for static files not found
075f16ca 32clientsRouter.use('/client/*', (req: express.Request, res: express.Response, next: express.NextFunction) => {
79530164
C
33 res.sendStatus(404)
34})
35
830bcd0f
C
36// ---------------------------------------------------------------------------
37
65fcc311
C
38export {
39 clientsRouter
40}
830bcd0f
C
41
42// ---------------------------------------------------------------------------
43
3fd3ab2d 44function addOpenGraphAndOEmbedTags (htmlStringPage: string, video: VideoModel) {
d38309c3 45 const previewUrl = CONFIG.WEBSERVER.URL + STATIC_PATHS.PREVIEWS + video.getPreviewName()
d8755eed 46 const videoUrl = CONFIG.WEBSERVER.URL + '/videos/watch/' + video.uuid
830bcd0f 47
a434c465
C
48 const videoNameEscaped = escapeHTML(video.name)
49 const videoDescriptionEscaped = escapeHTML(video.description)
7ff7802a 50 const embedUrl = CONFIG.WEBSERVER.URL + video.getEmbedPath()
49347a0a 51
d8755eed 52 const openGraphMetaTags = {
830bcd0f 53 'og:type': 'video',
a434c465 54 'og:title': videoNameEscaped,
41b5da1d 55 'og:image': previewUrl,
830bcd0f 56 'og:url': videoUrl,
a434c465 57 'og:description': videoDescriptionEscaped,
830bcd0f 58
7ff7802a
C
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
a434c465
C
65 'name': videoNameEscaped,
66 'description': videoDescriptionEscaped,
41b5da1d 67 'image': previewUrl,
830bcd0f
C
68
69 'twitter:card': 'summary_large_image',
70 'twitter:site': '@Chocobozzz',
a434c465
C
71 'twitter:title': videoNameEscaped,
72 'twitter:description': videoDescriptionEscaped,
7ff7802a
C
73 'twitter:image': previewUrl,
74 'twitter:player': embedUrl,
75 'twitter:player:width': EMBED_SIZE.width,
76 'twitter:player:height': EMBED_SIZE.height
830bcd0f
C
77 }
78
d8755eed
C
79 const oembedLinkTags = [
80 {
81 type: 'application/json+oembed',
82 href: CONFIG.WEBSERVER.URL + '/services/oembed?url=' + encodeURIComponent(videoUrl),
a434c465 83 title: videoNameEscaped
d8755eed
C
84 }
85 ]
86
093237cf 87 const schemaTags = {
c7b1b92b
C
88 '@context': 'http://schema.org',
89 '@type': 'VideoObject',
093237cf
C
90 name: videoNameEscaped,
91 description: videoDescriptionEscaped,
92 duration: video.getActivityStreamDuration(),
93 thumbnailURL: previewUrl,
94 contentURL: videoUrl,
95 embedURL: embedUrl,
96 uploadDate: video.createdAt
97 }
98
830bcd0f 99 let tagsString = ''
c7b1b92b
C
100
101 // Opengraph
d8755eed
C
102 Object.keys(openGraphMetaTags).forEach(tagName => {
103 const tagValue = openGraphMetaTags[tagName]
830bcd0f 104
d8755eed 105 tagsString += `<meta property="${tagName}" content="${tagValue}" />`
830bcd0f
C
106 })
107
c7b1b92b 108 // OEmbed
d8755eed
C
109 for (const oembedLinkTag of oembedLinkTags) {
110 tagsString += `<link rel="alternate" type="${oembedLinkTag.type}" href="${oembedLinkTag.href}" title="${oembedLinkTag.title}" />`
111 }
112
c7b1b92b
C
113 // Schema.org
114 tagsString += `<script type="application/ld+json">${JSON.stringify(schemaTags)}</script>`
093237cf 115
d8755eed 116 return htmlStringPage.replace(OPENGRAPH_AND_OEMBED_COMMENT, tagsString)
830bcd0f
C
117}
118
eb080476 119async function generateWatchHtmlPage (req: express.Request, res: express.Response, next: express.NextFunction) {
69818c93 120 const videoId = '' + req.params.id
3fd3ab2d 121 let videoPromise: Bluebird<VideoModel>
73ce7f96
C
122
123 // Let Angular application handle errors
0a6658fd 124 if (validator.isUUID(videoId, 4)) {
3fd3ab2d 125 videoPromise = VideoModel.loadByUUIDAndPopulateAccountAndServerAndTags(videoId)
0a6658fd 126 } else if (validator.isInt(videoId)) {
3fd3ab2d 127 videoPromise = VideoModel.loadAndPopulateAccountAndServerAndTags(+videoId)
0a6658fd
C
128 } else {
129 return res.sendFile(indexPath)
130 }
73ce7f96 131
eb080476 132 let [ file, video ] = await Promise.all([
6fcd19ba 133 readFileBufferPromise(indexPath),
0a6658fd 134 videoPromise
6fcd19ba 135 ])
830bcd0f 136
eb080476 137 const html = file.toString()
73ce7f96 138
eb080476
C
139 // Let Angular application handle errors
140 if (!video) return res.sendFile(indexPath)
141
142 const htmlStringPageWithTags = addOpenGraphAndOEmbedTags(html, video)
143 res.set('Content-Type', 'text/html; charset=UTF-8').send(htmlStringPageWithTags)
830bcd0f 144}