]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/client-html.ts
Reduce video.ts file size by moving some methods in other files
[github/Chocobozzz/PeerTube.git] / server / lib / client-html.ts
1 import * as express from 'express'
2 import * as Bluebird from 'bluebird'
3 import { buildFileLocale, getDefaultLocale, is18nLocale, POSSIBLE_LOCALES } from '../../shared/models/i18n/i18n'
4 import { CONFIG, CUSTOM_HTML_TAG_COMMENTS, EMBED_SIZE, STATIC_PATHS } from '../initializers'
5 import { join } from 'path'
6 import { escapeHTML } from '../helpers/core-utils'
7 import { VideoModel } from '../models/video/video'
8 import * as validator from 'validator'
9 import { VideoPrivacy } from '../../shared/models/videos'
10 import { readFile } from 'fs-extra'
11 import { getActivityStreamDuration } from '../models/video/video-format-utils'
12
13 export class ClientHtml {
14
15 private static htmlCache: { [path: string]: string } = {}
16
17 static invalidCache () {
18 ClientHtml.htmlCache = {}
19 }
20
21 static async getIndexHTML (req: express.Request, res: express.Response, paramLang?: string) {
22 const path = ClientHtml.getIndexPath(req, res, paramLang)
23 if (ClientHtml.htmlCache[path]) return ClientHtml.htmlCache[path]
24
25 const buffer = await readFile(path)
26
27 let html = buffer.toString()
28
29 html = ClientHtml.addTitleTag(html)
30 html = ClientHtml.addDescriptionTag(html)
31 html = ClientHtml.addCustomCSS(html)
32
33 ClientHtml.htmlCache[path] = html
34
35 return html
36 }
37
38 static async getWatchHTMLPage (videoId: string, req: express.Request, res: express.Response) {
39 let videoPromise: Bluebird<VideoModel>
40
41 // Let Angular application handle errors
42 if (validator.isUUID(videoId, 4)) {
43 videoPromise = VideoModel.loadByUUIDAndPopulateAccountAndServerAndTags(videoId)
44 } else if (validator.isInt(videoId)) {
45 videoPromise = VideoModel.loadAndPopulateAccountAndServerAndTags(+videoId)
46 } else {
47 return ClientHtml.getIndexHTML(req, res)
48 }
49
50 const [ html, video ] = await Promise.all([
51 ClientHtml.getIndexHTML(req, res),
52 videoPromise
53 ])
54
55 // Let Angular application handle errors
56 if (!video || video.privacy === VideoPrivacy.PRIVATE) {
57 return ClientHtml.getIndexHTML(req, res)
58 }
59
60 return ClientHtml.addOpenGraphAndOEmbedTags(html, video)
61 }
62
63 private static getIndexPath (req: express.Request, res: express.Response, paramLang?: string) {
64 let lang: string
65
66 // Check param lang validity
67 if (paramLang && is18nLocale(paramLang)) {
68 lang = paramLang
69
70 // Save locale in cookies
71 res.cookie('clientLanguage', lang, {
72 secure: CONFIG.WEBSERVER.SCHEME === 'https',
73 sameSite: true,
74 maxAge: 1000 * 3600 * 24 * 90 // 3 months
75 })
76
77 } else if (req.cookies.clientLanguage && is18nLocale(req.cookies.clientLanguage)) {
78 lang = req.cookies.clientLanguage
79 } else {
80 lang = req.acceptsLanguages(POSSIBLE_LOCALES) || getDefaultLocale()
81 }
82
83 return join(__dirname, '../../../client/dist/' + buildFileLocale(lang) + '/index.html')
84 }
85
86 private static addTitleTag (htmlStringPage: string) {
87 const titleTag = '<title>' + CONFIG.INSTANCE.NAME + '</title>'
88
89 return htmlStringPage.replace(CUSTOM_HTML_TAG_COMMENTS.TITLE, titleTag)
90 }
91
92 private static addDescriptionTag (htmlStringPage: string) {
93 const descriptionTag = `<meta name="description" content="${CONFIG.INSTANCE.SHORT_DESCRIPTION}" />`
94
95 return htmlStringPage.replace(CUSTOM_HTML_TAG_COMMENTS.DESCRIPTION, descriptionTag)
96 }
97
98 private static addCustomCSS (htmlStringPage: string) {
99 const styleTag = '<style class="custom-css-style">' + CONFIG.INSTANCE.CUSTOMIZATIONS.CSS + '</style>'
100
101 return htmlStringPage.replace(CUSTOM_HTML_TAG_COMMENTS.CUSTOM_CSS, styleTag)
102 }
103
104 private static addOpenGraphAndOEmbedTags (htmlStringPage: string, video: VideoModel) {
105 const previewUrl = CONFIG.WEBSERVER.URL + STATIC_PATHS.PREVIEWS + video.getPreviewName()
106 const videoUrl = CONFIG.WEBSERVER.URL + '/videos/watch/' + video.uuid
107
108 const videoNameEscaped = escapeHTML(video.name)
109 const videoDescriptionEscaped = escapeHTML(video.description)
110 const embedUrl = CONFIG.WEBSERVER.URL + video.getEmbedStaticPath()
111
112 const openGraphMetaTags = {
113 'og:type': 'video',
114 'og:title': videoNameEscaped,
115 'og:image': previewUrl,
116 'og:url': videoUrl,
117 'og:description': videoDescriptionEscaped,
118
119 'og:video:url': embedUrl,
120 'og:video:secure_url': embedUrl,
121 'og:video:type': 'text/html',
122 'og:video:width': EMBED_SIZE.width,
123 'og:video:height': EMBED_SIZE.height,
124
125 'name': videoNameEscaped,
126 'description': videoDescriptionEscaped,
127 'image': previewUrl,
128
129 'twitter:card': CONFIG.SERVICES.TWITTER.WHITELISTED ? 'player' : 'summary_large_image',
130 'twitter:site': CONFIG.SERVICES.TWITTER.USERNAME,
131 'twitter:title': videoNameEscaped,
132 'twitter:description': videoDescriptionEscaped,
133 'twitter:image': previewUrl,
134 'twitter:player': embedUrl,
135 'twitter:player:width': EMBED_SIZE.width,
136 'twitter:player:height': EMBED_SIZE.height
137 }
138
139 const oembedLinkTags = [
140 {
141 type: 'application/json+oembed',
142 href: CONFIG.WEBSERVER.URL + '/services/oembed?url=' + encodeURIComponent(videoUrl),
143 title: videoNameEscaped
144 }
145 ]
146
147 const schemaTags = {
148 '@context': 'http://schema.org',
149 '@type': 'VideoObject',
150 name: videoNameEscaped,
151 description: videoDescriptionEscaped,
152 thumbnailUrl: previewUrl,
153 uploadDate: video.createdAt.toISOString(),
154 duration: getActivityStreamDuration(video.duration),
155 contentUrl: videoUrl,
156 embedUrl: embedUrl,
157 interactionCount: video.views
158 }
159
160 let tagsString = ''
161
162 // Opengraph
163 Object.keys(openGraphMetaTags).forEach(tagName => {
164 const tagValue = openGraphMetaTags[tagName]
165
166 tagsString += `<meta property="${tagName}" content="${tagValue}" />`
167 })
168
169 // OEmbed
170 for (const oembedLinkTag of oembedLinkTags) {
171 tagsString += `<link rel="alternate" type="${oembedLinkTag.type}" href="${oembedLinkTag.href}" title="${oembedLinkTag.title}" />`
172 }
173
174 // Schema.org
175 tagsString += `<script type="application/ld+json">${JSON.stringify(schemaTags)}</script>`
176
177 // SEO
178 tagsString += `<link rel="canonical" href="${videoUrl}" />`
179
180 return htmlStringPage.replace(CUSTOM_HTML_TAG_COMMENTS.OPENGRAPH_AND_OEMBED, tagsString)
181 }
182 }