]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/client.ts
Disable auto language
[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 5import { escapeHTML, readFileBufferPromise, root } from '../helpers/core-utils'
989e526a
C
6import {
7 ACCEPT_HEADERS,
8 CONFIG,
9 EMBED_SIZE,
10 OPENGRAPH_AND_OEMBED_COMMENT,
11 STATIC_MAX_AGE,
12 STATIC_PATHS
13} from '../initializers'
eb080476 14import { asyncMiddleware } from '../middlewares'
3fd3ab2d 15import { VideoModel } from '../models/video/video'
a00a8f09 16import { VideoPrivacy } from '../../shared/models/videos'
989e526a 17import { I18N_LOCALES, is18nLocale, getDefaultLocale } from '../../shared/models'
830bcd0f 18
65fcc311 19const clientsRouter = express.Router()
830bcd0f 20
e02643f3 21const distPath = join(root(), 'client', 'dist')
1f30a185 22const assetsImagesPath = join(root(), 'client', 'dist', 'assets', 'images')
e02643f3 23const embedPath = join(distPath, 'standalone', 'videos', 'embed.html')
830bcd0f 24
d8755eed 25// Special route that add OpenGraph and oEmbed tags
830bcd0f 26// Do not use a template engine for a so little thing
eb080476
C
27clientsRouter.use('/videos/watch/:id',
28 asyncMiddleware(generateWatchHtmlPage)
29)
830bcd0f 30
075f16ca 31clientsRouter.use('/videos/embed', (req: express.Request, res: express.Response, next: express.NextFunction) => {
830bcd0f
C
32 res.sendFile(embedPath)
33})
34
79530164 35// Static HTML/CSS/JS client files
78967fca
C
36
37const staticClientFiles = [
38 'manifest.json',
39 'ngsw-worker.js',
40 'ngsw.json'
41]
42for (const staticClientFile of staticClientFiles) {
43 const path = join(root(), 'client', 'dist', staticClientFile)
44 clientsRouter.use('/' + staticClientFile, express.static(path, { maxAge: STATIC_MAX_AGE }))
45}
46
65fcc311 47clientsRouter.use('/client', express.static(distPath, { maxAge: STATIC_MAX_AGE }))
6bafac54 48clientsRouter.use('/client/assets/images', express.static(assetsImagesPath, { maxAge: STATIC_MAX_AGE }))
79530164
C
49
50// 404 for static files not found
075f16ca 51clientsRouter.use('/client/*', (req: express.Request, res: express.Response, next: express.NextFunction) => {
79530164
C
52 res.sendStatus(404)
53})
54
989e526a
C
55// Always serve index client page (the client is a single page application, let it handle routing)
56// Try to provide the right language index.html
57clientsRouter.use('/(:language)?', function (req, res) {
58 if (req.accepts(ACCEPT_HEADERS) === 'html') {
59 return res.sendFile(getIndexPath(req, req.params.language))
60 }
61
62 return res.status(404).end()
63})
64
830bcd0f
C
65// ---------------------------------------------------------------------------
66
65fcc311
C
67export {
68 clientsRouter
69}
830bcd0f
C
70
71// ---------------------------------------------------------------------------
72
989e526a
C
73function getIndexPath (req: express.Request, paramLang?: string) {
74 let lang: string
75
76 // Check param lang validity
77 if (paramLang && is18nLocale(paramLang)) {
78 lang = paramLang
79 } else {
f8b44753
C
80 // lang = req.acceptsLanguages(Object.keys(I18N_LOCALES)) || getDefaultLocale()
81 // Disable auto language for now
82 lang = getDefaultLocale()
989e526a
C
83 }
84
85 return join(__dirname, '../../../client/dist/' + lang + '/index.html')
86}
87
3fd3ab2d 88function addOpenGraphAndOEmbedTags (htmlStringPage: string, video: VideoModel) {
d38309c3 89 const previewUrl = CONFIG.WEBSERVER.URL + STATIC_PATHS.PREVIEWS + video.getPreviewName()
d8755eed 90 const videoUrl = CONFIG.WEBSERVER.URL + '/videos/watch/' + video.uuid
830bcd0f 91
a434c465
C
92 const videoNameEscaped = escapeHTML(video.name)
93 const videoDescriptionEscaped = escapeHTML(video.description)
7ff7802a 94 const embedUrl = CONFIG.WEBSERVER.URL + video.getEmbedPath()
49347a0a 95
d8755eed 96 const openGraphMetaTags = {
830bcd0f 97 'og:type': 'video',
a434c465 98 'og:title': videoNameEscaped,
41b5da1d 99 'og:image': previewUrl,
830bcd0f 100 'og:url': videoUrl,
a434c465 101 'og:description': videoDescriptionEscaped,
830bcd0f 102
7ff7802a
C
103 'og:video:url': embedUrl,
104 'og:video:secure_url': embedUrl,
105 'og:video:type': 'text/html',
106 'og:video:width': EMBED_SIZE.width,
107 'og:video:height': EMBED_SIZE.height,
108
a434c465
C
109 'name': videoNameEscaped,
110 'description': videoDescriptionEscaped,
41b5da1d 111 'image': previewUrl,
830bcd0f 112
8be1afa1
C
113 'twitter:card': CONFIG.SERVICES.TWITTER.WHITELISTED ? 'player' : 'summary_large_image',
114 'twitter:site': CONFIG.SERVICES.TWITTER.USERNAME,
a434c465
C
115 'twitter:title': videoNameEscaped,
116 'twitter:description': videoDescriptionEscaped,
7ff7802a
C
117 'twitter:image': previewUrl,
118 'twitter:player': embedUrl,
119 'twitter:player:width': EMBED_SIZE.width,
120 'twitter:player:height': EMBED_SIZE.height
830bcd0f
C
121 }
122
d8755eed
C
123 const oembedLinkTags = [
124 {
125 type: 'application/json+oembed',
126 href: CONFIG.WEBSERVER.URL + '/services/oembed?url=' + encodeURIComponent(videoUrl),
a434c465 127 title: videoNameEscaped
d8755eed
C
128 }
129 ]
130
093237cf 131 const schemaTags = {
c7b1b92b
C
132 '@context': 'http://schema.org',
133 '@type': 'VideoObject',
093237cf
C
134 name: videoNameEscaped,
135 description: videoDescriptionEscaped,
acbffe9c
C
136 thumbnailUrl: previewUrl,
137 uploadDate: video.createdAt.toISOString(),
093237cf 138 duration: video.getActivityStreamDuration(),
acbffe9c
C
139 contentUrl: videoUrl,
140 embedUrl: embedUrl,
141 interactionCount: video.views
093237cf
C
142 }
143
830bcd0f 144 let tagsString = ''
c7b1b92b
C
145
146 // Opengraph
d8755eed
C
147 Object.keys(openGraphMetaTags).forEach(tagName => {
148 const tagValue = openGraphMetaTags[tagName]
830bcd0f 149
d8755eed 150 tagsString += `<meta property="${tagName}" content="${tagValue}" />`
830bcd0f
C
151 })
152
c7b1b92b 153 // OEmbed
d8755eed
C
154 for (const oembedLinkTag of oembedLinkTags) {
155 tagsString += `<link rel="alternate" type="${oembedLinkTag.type}" href="${oembedLinkTag.href}" title="${oembedLinkTag.title}" />`
156 }
157
c7b1b92b
C
158 // Schema.org
159 tagsString += `<script type="application/ld+json">${JSON.stringify(schemaTags)}</script>`
093237cf 160
acbffe9c
C
161 // SEO
162 tagsString += `<link rel="canonical" href="${videoUrl}" />`
163
d8755eed 164 return htmlStringPage.replace(OPENGRAPH_AND_OEMBED_COMMENT, tagsString)
830bcd0f
C
165}
166
eb080476 167async function generateWatchHtmlPage (req: express.Request, res: express.Response, next: express.NextFunction) {
69818c93 168 const videoId = '' + req.params.id
3fd3ab2d 169 let videoPromise: Bluebird<VideoModel>
73ce7f96
C
170
171 // Let Angular application handle errors
0a6658fd 172 if (validator.isUUID(videoId, 4)) {
3fd3ab2d 173 videoPromise = VideoModel.loadByUUIDAndPopulateAccountAndServerAndTags(videoId)
0a6658fd 174 } else if (validator.isInt(videoId)) {
3fd3ab2d 175 videoPromise = VideoModel.loadAndPopulateAccountAndServerAndTags(+videoId)
0a6658fd 176 } else {
989e526a 177 return res.sendFile(getIndexPath(req))
0a6658fd 178 }
73ce7f96 179
eb080476 180 let [ file, video ] = await Promise.all([
989e526a 181 readFileBufferPromise(getIndexPath(req)),
0a6658fd 182 videoPromise
6fcd19ba 183 ])
830bcd0f 184
eb080476 185 const html = file.toString()
73ce7f96 186
eb080476 187 // Let Angular application handle errors
989e526a 188 if (!video || video.privacy === VideoPrivacy.PRIVATE) return res.sendFile(getIndexPath(req))
eb080476
C
189
190 const htmlStringPageWithTags = addOpenGraphAndOEmbedTags(html, video)
191 res.set('Content-Type', 'text/html; charset=UTF-8').send(htmlStringPageWithTags)
830bcd0f 192}