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