]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/client.ts
Fix tests and user quota
[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'
6fcd19ba 4import * as Promise from 'bluebird'
830bcd0f 5
e02643f3 6import { database as db } from '../initializers/database'
65fcc311
C
7import {
8 CONFIG,
65fcc311 9 STATIC_PATHS,
709756b8
C
10 STATIC_MAX_AGE,
11 OPENGRAPH_COMMENT
65fcc311 12} from '../initializers'
6fcd19ba 13import { root, readFileBufferPromise } from '../helpers'
69818c93 14import { VideoInstance } from '../models'
830bcd0f 15
65fcc311 16const clientsRouter = express.Router()
830bcd0f 17
e02643f3
C
18const distPath = join(root(), 'client', 'dist')
19const embedPath = join(distPath, 'standalone', 'videos', 'embed.html')
65fcc311 20const indexPath = join(distPath, 'index.html')
830bcd0f
C
21
22// Special route that add OpenGraph tags
23// Do not use a template engine for a so little thing
65fcc311 24clientsRouter.use('/videos/watch/:id', generateWatchHtmlPage)
830bcd0f 25
075f16ca 26clientsRouter.use('/videos/embed', (req: express.Request, res: express.Response, next: express.NextFunction) => {
830bcd0f
C
27 res.sendFile(embedPath)
28})
29
79530164 30// Static HTML/CSS/JS client files
65fcc311 31clientsRouter.use('/client', express.static(distPath, { maxAge: STATIC_MAX_AGE }))
79530164
C
32
33// 404 for static files not found
075f16ca 34clientsRouter.use('/client/*', (req: express.Request, res: express.Response, next: express.NextFunction) => {
79530164
C
35 res.sendStatus(404)
36})
37
830bcd0f
C
38// ---------------------------------------------------------------------------
39
65fcc311
C
40export {
41 clientsRouter
42}
830bcd0f
C
43
44// ---------------------------------------------------------------------------
45
69818c93 46function addOpenGraphTags (htmlStringPage: string, video: VideoInstance) {
d38309c3 47 const previewUrl = CONFIG.WEBSERVER.URL + STATIC_PATHS.PREVIEWS + video.getPreviewName()
65fcc311 48 const videoUrl = CONFIG.WEBSERVER.URL + '/videos/watch/' + video.id
830bcd0f
C
49
50 const metaTags = {
51 'og:type': 'video',
52 'og:title': video.name,
41b5da1d 53 'og:image': previewUrl,
830bcd0f
C
54 'og:url': videoUrl,
55 'og:description': video.description,
56
57 'name': video.name,
58 'description': video.description,
41b5da1d 59 'image': previewUrl,
830bcd0f
C
60
61 'twitter:card': 'summary_large_image',
62 'twitter:site': '@Chocobozzz',
63 'twitter:title': video.name,
64 'twitter:description': video.description,
41b5da1d 65 'twitter:image': previewUrl
830bcd0f
C
66 }
67
68 let tagsString = ''
0a6658fd 69 Object.keys(metaTags).forEach(tagName => {
830bcd0f
C
70 const tagValue = metaTags[tagName]
71
72 tagsString += '<meta property="' + tagName + '" content="' + tagValue + '" />'
73 })
74
709756b8 75 return htmlStringPage.replace(OPENGRAPH_COMMENT, tagsString)
830bcd0f
C
76}
77
69818c93
C
78function generateWatchHtmlPage (req: express.Request, res: express.Response, next: express.NextFunction) {
79 const videoId = '' + req.params.id
0a6658fd 80 let videoPromise: Promise<VideoInstance>
73ce7f96
C
81
82 // Let Angular application handle errors
0a6658fd
C
83 if (validator.isUUID(videoId, 4)) {
84 videoPromise = db.Video.loadByUUIDAndPopulateAuthorAndPodAndTags(videoId)
85 } else if (validator.isInt(videoId)) {
86 videoPromise = db.Video.loadAndPopulateAuthorAndPodAndTags(+videoId)
87 } else {
88 return res.sendFile(indexPath)
89 }
73ce7f96 90
6fcd19ba
C
91 Promise.all([
92 readFileBufferPromise(indexPath),
0a6658fd 93 videoPromise
6fcd19ba
C
94 ])
95 .then(([ file, video ]) => {
96 file = file as Buffer
97 video = video as VideoInstance
830bcd0f 98
6fcd19ba 99 const html = file.toString()
830bcd0f 100
73ce7f96
C
101 // Let Angular application handle errors
102 if (!video) return res.sendFile(indexPath)
103
830bcd0f
C
104 const htmlStringPageWithTags = addOpenGraphTags(html, video)
105 res.set('Content-Type', 'text/html; charset=UTF-8').send(htmlStringPageWithTags)
106 })
6fcd19ba 107 .catch(err => next(err))
830bcd0f 108}