]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/client.ts
Add ability to forbid user to upload video
[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 10 STATIC_MAX_AGE,
d8755eed 11 OPENGRAPH_AND_OEMBED_COMMENT
65fcc311 12} from '../initializers'
49347a0a 13import { root, readFileBufferPromise, escapeHTML } 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 21
d8755eed 22// Special route that add OpenGraph and oEmbed tags
830bcd0f 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
d8755eed 46function addOpenGraphAndOEmbedTags (htmlStringPage: string, video: VideoInstance) {
d38309c3 47 const previewUrl = CONFIG.WEBSERVER.URL + STATIC_PATHS.PREVIEWS + video.getPreviewName()
d8755eed 48 const videoUrl = CONFIG.WEBSERVER.URL + '/videos/watch/' + video.uuid
830bcd0f 49
49347a0a
C
50 const videoName = escapeHTML(video.name)
51 const videoDescription = escapeHTML(video.description)
52
d8755eed 53 const openGraphMetaTags = {
830bcd0f 54 'og:type': 'video',
49347a0a 55 'og:title': videoName,
41b5da1d 56 'og:image': previewUrl,
830bcd0f 57 'og:url': videoUrl,
49347a0a 58 'og:description': videoDescription,
830bcd0f 59
49347a0a
C
60 'name': videoName,
61 'description': videoDescription,
41b5da1d 62 'image': previewUrl,
830bcd0f
C
63
64 'twitter:card': 'summary_large_image',
65 'twitter:site': '@Chocobozzz',
49347a0a
C
66 'twitter:title': videoName,
67 'twitter:description': videoDescription,
41b5da1d 68 'twitter:image': previewUrl
830bcd0f
C
69 }
70
d8755eed
C
71 const oembedLinkTags = [
72 {
73 type: 'application/json+oembed',
74 href: CONFIG.WEBSERVER.URL + '/services/oembed?url=' + encodeURIComponent(videoUrl),
49347a0a 75 title: videoName
d8755eed
C
76 }
77 ]
78
830bcd0f 79 let tagsString = ''
d8755eed
C
80 Object.keys(openGraphMetaTags).forEach(tagName => {
81 const tagValue = openGraphMetaTags[tagName]
830bcd0f 82
d8755eed 83 tagsString += `<meta property="${tagName}" content="${tagValue}" />`
830bcd0f
C
84 })
85
d8755eed
C
86 for (const oembedLinkTag of oembedLinkTags) {
87 tagsString += `<link rel="alternate" type="${oembedLinkTag.type}" href="${oembedLinkTag.href}" title="${oembedLinkTag.title}" />`
88 }
89
90 return htmlStringPage.replace(OPENGRAPH_AND_OEMBED_COMMENT, tagsString)
830bcd0f
C
91}
92
69818c93
C
93function generateWatchHtmlPage (req: express.Request, res: express.Response, next: express.NextFunction) {
94 const videoId = '' + req.params.id
0a6658fd 95 let videoPromise: Promise<VideoInstance>
73ce7f96
C
96
97 // Let Angular application handle errors
0a6658fd
C
98 if (validator.isUUID(videoId, 4)) {
99 videoPromise = db.Video.loadByUUIDAndPopulateAuthorAndPodAndTags(videoId)
100 } else if (validator.isInt(videoId)) {
101 videoPromise = db.Video.loadAndPopulateAuthorAndPodAndTags(+videoId)
102 } else {
103 return res.sendFile(indexPath)
104 }
73ce7f96 105
6fcd19ba
C
106 Promise.all([
107 readFileBufferPromise(indexPath),
0a6658fd 108 videoPromise
6fcd19ba
C
109 ])
110 .then(([ file, video ]) => {
111 file = file as Buffer
112 video = video as VideoInstance
830bcd0f 113
6fcd19ba 114 const html = file.toString()
830bcd0f 115
73ce7f96
C
116 // Let Angular application handle errors
117 if (!video) return res.sendFile(indexPath)
118
d8755eed 119 const htmlStringPageWithTags = addOpenGraphAndOEmbedTags(html, video)
830bcd0f
C
120 res.set('Content-Type', 'text/html; charset=UTF-8').send(htmlStringPageWithTags)
121 })
6fcd19ba 122 .catch(err => next(err))
830bcd0f 123}