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