]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/client.ts
Upgrade scripts and embed webpack config
[github/Chocobozzz/PeerTube.git] / server / controllers / client.ts
CommitLineData
4d4e5cd4 1import * as express from 'express'
65fcc311 2import { join } from 'path'
4d4e5cd4 3import * as validator from 'validator'
eb080476 4import * as Bluebird from 'bluebird'
830bcd0f 5
e02643f3 6import { database as db } from '../initializers/database'
65fcc311
C
7import {
8 CONFIG,
65fcc311 9 STATIC_PATHS,
709756b8 10 STATIC_MAX_AGE,
7ff7802a
C
11 OPENGRAPH_AND_OEMBED_COMMENT,
12 EMBED_SIZE
65fcc311 13} from '../initializers'
49347a0a 14import { root, readFileBufferPromise, escapeHTML } from '../helpers'
eb080476 15import { asyncMiddleware } from '../middlewares'
69818c93 16import { VideoInstance } from '../models'
830bcd0f 17
65fcc311 18const clientsRouter = express.Router()
830bcd0f 19
e02643f3 20const distPath = join(root(), 'client', 'dist')
6bafac54 21const assetsImagesPath = join(root(), 'client', 'dist', 'assets', 'images')
e02643f3 22const embedPath = join(distPath, 'standalone', 'videos', 'embed.html')
65fcc311 23const indexPath = join(distPath, 'index.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
65fcc311 36clientsRouter.use('/client', express.static(distPath, { maxAge: STATIC_MAX_AGE }))
6bafac54 37clientsRouter.use('/client/assets/images', express.static(assetsImagesPath, { maxAge: STATIC_MAX_AGE }))
79530164
C
38
39// 404 for static files not found
075f16ca 40clientsRouter.use('/client/*', (req: express.Request, res: express.Response, next: express.NextFunction) => {
79530164
C
41 res.sendStatus(404)
42})
43
830bcd0f
C
44// ---------------------------------------------------------------------------
45
65fcc311
C
46export {
47 clientsRouter
48}
830bcd0f
C
49
50// ---------------------------------------------------------------------------
51
d8755eed 52function addOpenGraphAndOEmbedTags (htmlStringPage: string, video: VideoInstance) {
d38309c3 53 const previewUrl = CONFIG.WEBSERVER.URL + STATIC_PATHS.PREVIEWS + video.getPreviewName()
d8755eed 54 const videoUrl = CONFIG.WEBSERVER.URL + '/videos/watch/' + video.uuid
830bcd0f 55
a434c465
C
56 const videoNameEscaped = escapeHTML(video.name)
57 const videoDescriptionEscaped = escapeHTML(video.description)
7ff7802a 58 const embedUrl = CONFIG.WEBSERVER.URL + video.getEmbedPath()
49347a0a 59
d8755eed 60 const openGraphMetaTags = {
830bcd0f 61 'og:type': 'video',
a434c465 62 'og:title': videoNameEscaped,
41b5da1d 63 'og:image': previewUrl,
830bcd0f 64 'og:url': videoUrl,
a434c465 65 'og:description': videoDescriptionEscaped,
830bcd0f 66
7ff7802a
C
67 'og:video:url': embedUrl,
68 'og:video:secure_url': embedUrl,
69 'og:video:type': 'text/html',
70 'og:video:width': EMBED_SIZE.width,
71 'og:video:height': EMBED_SIZE.height,
72
a434c465
C
73 'name': videoNameEscaped,
74 'description': videoDescriptionEscaped,
41b5da1d 75 'image': previewUrl,
830bcd0f
C
76
77 'twitter:card': 'summary_large_image',
78 'twitter:site': '@Chocobozzz',
a434c465
C
79 'twitter:title': videoNameEscaped,
80 'twitter:description': videoDescriptionEscaped,
7ff7802a
C
81 'twitter:image': previewUrl,
82 'twitter:player': embedUrl,
83 'twitter:player:width': EMBED_SIZE.width,
84 'twitter:player:height': EMBED_SIZE.height
830bcd0f
C
85 }
86
d8755eed
C
87 const oembedLinkTags = [
88 {
89 type: 'application/json+oembed',
90 href: CONFIG.WEBSERVER.URL + '/services/oembed?url=' + encodeURIComponent(videoUrl),
a434c465 91 title: videoNameEscaped
d8755eed
C
92 }
93 ]
94
830bcd0f 95 let tagsString = ''
d8755eed
C
96 Object.keys(openGraphMetaTags).forEach(tagName => {
97 const tagValue = openGraphMetaTags[tagName]
830bcd0f 98
d8755eed 99 tagsString += `<meta property="${tagName}" content="${tagValue}" />`
830bcd0f
C
100 })
101
d8755eed
C
102 for (const oembedLinkTag of oembedLinkTags) {
103 tagsString += `<link rel="alternate" type="${oembedLinkTag.type}" href="${oembedLinkTag.href}" title="${oembedLinkTag.title}" />`
104 }
105
106 return htmlStringPage.replace(OPENGRAPH_AND_OEMBED_COMMENT, tagsString)
830bcd0f
C
107}
108
eb080476 109async function generateWatchHtmlPage (req: express.Request, res: express.Response, next: express.NextFunction) {
69818c93 110 const videoId = '' + req.params.id
eb080476 111 let videoPromise: Bluebird<VideoInstance>
73ce7f96
C
112
113 // Let Angular application handle errors
0a6658fd 114 if (validator.isUUID(videoId, 4)) {
60862425 115 videoPromise = db.Video.loadByUUIDAndPopulateAccountAndServerAndTags(videoId)
0a6658fd 116 } else if (validator.isInt(videoId)) {
60862425 117 videoPromise = db.Video.loadAndPopulateAccountAndServerAndTags(+videoId)
0a6658fd
C
118 } else {
119 return res.sendFile(indexPath)
120 }
73ce7f96 121
eb080476 122 let [ file, video ] = await Promise.all([
6fcd19ba 123 readFileBufferPromise(indexPath),
0a6658fd 124 videoPromise
6fcd19ba 125 ])
830bcd0f 126
eb080476 127 const html = file.toString()
73ce7f96 128
eb080476
C
129 // Let Angular application handle errors
130 if (!video) return res.sendFile(indexPath)
131
132 const htmlStringPageWithTags = addOpenGraphAndOEmbedTags(html, video)
133 res.set('Content-Type', 'text/html; charset=UTF-8').send(htmlStringPageWithTags)
830bcd0f 134}