Federated video streaming platform using ActivityPub and P2P in the web browser with Angular. https://joinpeertube.org/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

593 lines
19 KiB

  1. import express from 'express'
  2. import { readFile } from 'fs-extra'
  3. import { join } from 'path'
  4. import validator from 'validator'
  5. import { toCompleteUUID } from '@server/helpers/custom-validators/misc'
  6. import { root } from '@shared/core-utils'
  7. import { escapeHTML } from '@shared/core-utils/renderer'
  8. import { sha256 } from '@shared/extra-utils'
  9. import { HTMLServerConfig } from '@shared/models'
  10. import { buildFileLocale, getDefaultLocale, is18nLocale, POSSIBLE_LOCALES } from '../../shared/core-utils/i18n/i18n'
  11. import { HttpStatusCode } from '../../shared/models/http/http-error-codes'
  12. import { VideoPlaylistPrivacy, VideoPrivacy } from '../../shared/models/videos'
  13. import { isTestInstance } from '../helpers/core-utils'
  14. import { logger } from '../helpers/logger'
  15. import { mdToPlainText } from '../helpers/markdown'
  16. import { CONFIG } from '../initializers/config'
  17. import {
  18. ACCEPT_HEADERS,
  19. ACTOR_IMAGES_SIZE,
  20. CUSTOM_HTML_TAG_COMMENTS,
  21. EMBED_SIZE,
  22. FILES_CONTENT_HASH,
  23. PLUGIN_GLOBAL_CSS_PATH,
  24. WEBSERVER
  25. } from '../initializers/constants'
  26. import { AccountModel } from '../models/account/account'
  27. import { getActivityStreamDuration } from '../models/video/formatter/video-format-utils'
  28. import { VideoModel } from '../models/video/video'
  29. import { VideoChannelModel } from '../models/video/video-channel'
  30. import { VideoPlaylistModel } from '../models/video/video-playlist'
  31. import { MAccountActor, MChannelActor } from '../types/models'
  32. import { ServerConfigManager } from './server-config-manager'
  33. type Tags = {
  34. ogType: string
  35. twitterCard: 'player' | 'summary' | 'summary_large_image'
  36. schemaType: string
  37. list?: {
  38. numberOfItems: number
  39. }
  40. escapedSiteName: string
  41. escapedTitle: string
  42. escapedDescription: string
  43. url: string
  44. originUrl: string
  45. disallowIndexation?: boolean
  46. embed?: {
  47. url: string
  48. createdAt: string
  49. duration?: string
  50. views?: number
  51. }
  52. image: {
  53. url: string
  54. width?: number
  55. height?: number
  56. }
  57. }
  58. class ClientHtml {
  59. private static htmlCache: { [path: string]: string } = {}
  60. static invalidCache () {
  61. logger.info('Cleaning HTML cache.')
  62. ClientHtml.htmlCache = {}
  63. }
  64. static async getDefaultHTMLPage (req: express.Request, res: express.Response, paramLang?: string) {
  65. const html = paramLang
  66. ? await ClientHtml.getIndexHTML(req, res, paramLang)
  67. : await ClientHtml.getIndexHTML(req, res)
  68. let customHtml = ClientHtml.addTitleTag(html)
  69. customHtml = ClientHtml.addDescriptionTag(customHtml)
  70. return customHtml
  71. }
  72. static async getWatchHTMLPage (videoIdArg: string, req: express.Request, res: express.Response) {
  73. const videoId = toCompleteUUID(videoIdArg)
  74. // Let Angular application handle errors
  75. if (!validator.isInt(videoId) && !validator.isUUID(videoId, 4)) {
  76. res.status(HttpStatusCode.NOT_FOUND_404)
  77. return ClientHtml.getIndexHTML(req, res)
  78. }
  79. const [ html, video ] = await Promise.all([
  80. ClientHtml.getIndexHTML(req, res),
  81. VideoModel.loadWithBlacklist(videoId)
  82. ])
  83. // Let Angular application handle errors
  84. if (!video || video.privacy === VideoPrivacy.PRIVATE || video.privacy === VideoPrivacy.INTERNAL || video.VideoBlacklist) {
  85. res.status(HttpStatusCode.NOT_FOUND_404)
  86. return html
  87. }
  88. const description = mdToPlainText(video.description)
  89. let customHtml = ClientHtml.addTitleTag(html, video.name)
  90. customHtml = ClientHtml.addDescriptionTag(customHtml, description)
  91. const url = WEBSERVER.URL + video.getWatchStaticPath()
  92. const originUrl = video.url
  93. const title = video.name
  94. const siteName = CONFIG.INSTANCE.NAME
  95. const image = {
  96. url: WEBSERVER.URL + video.getPreviewStaticPath()
  97. }
  98. const embed = {
  99. url: WEBSERVER.URL + video.getEmbedStaticPath(),
  100. createdAt: video.createdAt.toISOString(),
  101. duration: getActivityStreamDuration(video.duration),
  102. views: video.views
  103. }
  104. const ogType = 'video'
  105. const twitterCard = CONFIG.SERVICES.TWITTER.WHITELISTED ? 'player' : 'summary_large_image'
  106. const schemaType = 'VideoObject'
  107. customHtml = ClientHtml.addTags(customHtml, {
  108. url,
  109. originUrl,
  110. escapedSiteName: escapeHTML(siteName),
  111. escapedTitle: escapeHTML(title),
  112. escapedDescription: escapeHTML(description),
  113. image,
  114. embed,
  115. ogType,
  116. twitterCard,
  117. schemaType
  118. })
  119. return customHtml
  120. }
  121. static async getWatchPlaylistHTMLPage (videoPlaylistIdArg: string, req: express.Request, res: express.Response) {
  122. const videoPlaylistId = toCompleteUUID(videoPlaylistIdArg)
  123. // Let Angular application handle errors
  124. if (!validator.isInt(videoPlaylistId) && !validator.isUUID(videoPlaylistId, 4)) {
  125. res.status(HttpStatusCode.NOT_FOUND_404)
  126. return ClientHtml.getIndexHTML(req, res)
  127. }
  128. const [ html, videoPlaylist ] = await Promise.all([
  129. ClientHtml.getIndexHTML(req, res),
  130. VideoPlaylistModel.loadWithAccountAndChannel(videoPlaylistId, null)
  131. ])
  132. // Let Angular application handle errors
  133. if (!videoPlaylist || videoPlaylist.privacy === VideoPlaylistPrivacy.PRIVATE) {
  134. res.status(HttpStatusCode.NOT_FOUND_404)
  135. return html
  136. }
  137. const description = mdToPlainText(videoPlaylist.description)
  138. let customHtml = ClientHtml.addTitleTag(html, videoPlaylist.name)
  139. customHtml = ClientHtml.addDescriptionTag(customHtml, description)
  140. const url = WEBSERVER.URL + videoPlaylist.getWatchStaticPath()
  141. const originUrl = videoPlaylist.url
  142. const title = videoPlaylist.name
  143. const siteName = CONFIG.INSTANCE.NAME
  144. const image = {
  145. url: videoPlaylist.getThumbnailUrl()
  146. }
  147. const embed = {
  148. url: WEBSERVER.URL + videoPlaylist.getEmbedStaticPath(),
  149. createdAt: videoPlaylist.createdAt.toISOString()
  150. }
  151. const list = {
  152. numberOfItems: videoPlaylist.get('videosLength') as number
  153. }
  154. const ogType = 'video'
  155. const twitterCard = CONFIG.SERVICES.TWITTER.WHITELISTED ? 'player' : 'summary'
  156. const schemaType = 'ItemList'
  157. customHtml = ClientHtml.addTags(customHtml, {
  158. url,
  159. originUrl,
  160. escapedSiteName: escapeHTML(siteName),
  161. escapedTitle: escapeHTML(title),
  162. escapedDescription: escapeHTML(description),
  163. embed,
  164. image,
  165. list,
  166. ogType,
  167. twitterCard,
  168. schemaType
  169. })
  170. return customHtml
  171. }
  172. static async getAccountHTMLPage (nameWithHost: string, req: express.Request, res: express.Response) {
  173. const accountModelPromise = AccountModel.loadByNameWithHost(nameWithHost)
  174. return this.getAccountOrChannelHTMLPage(() => accountModelPromise, req, res)
  175. }
  176. static async getVideoChannelHTMLPage (nameWithHost: string, req: express.Request, res: express.Response) {
  177. const videoChannelModelPromise = VideoChannelModel.loadByNameWithHostAndPopulateAccount(nameWithHost)
  178. return this.getAccountOrChannelHTMLPage(() => videoChannelModelPromise, req, res)
  179. }
  180. static async getActorHTMLPage (nameWithHost: string, req: express.Request, res: express.Response) {
  181. const [ account, channel ] = await Promise.all([
  182. AccountModel.loadByNameWithHost(nameWithHost),
  183. VideoChannelModel.loadByNameWithHostAndPopulateAccount(nameWithHost)
  184. ])
  185. return this.getAccountOrChannelHTMLPage(() => Promise.resolve(account || channel), req, res)
  186. }
  187. static async getEmbedHTML () {
  188. const path = ClientHtml.getEmbedPath()
  189. if (!isTestInstance() && ClientHtml.htmlCache[path]) return ClientHtml.htmlCache[path]
  190. const buffer = await readFile(path)
  191. const serverConfig = await ServerConfigManager.Instance.getHTMLServerConfig()
  192. let html = buffer.toString()
  193. html = await ClientHtml.addAsyncPluginCSS(html)
  194. html = ClientHtml.addCustomCSS(html)
  195. html = ClientHtml.addTitleTag(html)
  196. html = ClientHtml.addDescriptionTag(html)
  197. html = ClientHtml.addServerConfig(html, serverConfig)
  198. ClientHtml.htmlCache[path] = html
  199. return html
  200. }
  201. private static async getAccountOrChannelHTMLPage (
  202. loader: () => Promise<MAccountActor | MChannelActor>,
  203. req: express.Request,
  204. res: express.Response
  205. ) {
  206. const [ html, entity ] = await Promise.all([
  207. ClientHtml.getIndexHTML(req, res),
  208. loader()
  209. ])
  210. // Let Angular application handle errors
  211. if (!entity) {
  212. res.status(HttpStatusCode.NOT_FOUND_404)
  213. return ClientHtml.getIndexHTML(req, res)
  214. }
  215. const description = mdToPlainText(entity.description)
  216. let customHtml = ClientHtml.addTitleTag(html, entity.getDisplayName())
  217. customHtml = ClientHtml.addDescriptionTag(customHtml, description)
  218. const url = entity.getLocalUrl()
  219. const originUrl = entity.Actor.url
  220. const siteName = CONFIG.INSTANCE.NAME
  221. const title = entity.getDisplayName()
  222. const image = {
  223. url: entity.Actor.getAvatarUrl(),
  224. width: ACTOR_IMAGES_SIZE.AVATARS.width,
  225. height: ACTOR_IMAGES_SIZE.AVATARS.height
  226. }
  227. const ogType = 'website'
  228. const twitterCard = 'summary'
  229. const schemaType = 'ProfilePage'
  230. customHtml = ClientHtml.addTags(customHtml, {
  231. url,
  232. originUrl,
  233. escapedTitle: escapeHTML(title),
  234. escapedSiteName: escapeHTML(siteName),
  235. escapedDescription: escapeHTML(description),
  236. image,
  237. ogType,
  238. twitterCard,
  239. schemaType,
  240. disallowIndexation: !entity.Actor.isOwned()
  241. })
  242. return customHtml
  243. }
  244. private static async getIndexHTML (req: express.Request, res: express.Response, paramLang?: string) {
  245. const path = ClientHtml.getIndexPath(req, res, paramLang)
  246. if (!isTestInstance() && ClientHtml.htmlCache[path]) return ClientHtml.htmlCache[path]
  247. const buffer = await readFile(path)
  248. const serverConfig = await ServerConfigManager.Instance.getHTMLServerConfig()
  249. let html = buffer.toString()
  250. html = ClientHtml.addManifestContentHash(html)
  251. html = ClientHtml.addFaviconContentHash(html)
  252. html = ClientHtml.addLogoContentHash(html)
  253. html = ClientHtml.addCustomCSS(html)
  254. html = ClientHtml.addServerConfig(html, serverConfig)
  255. html = await ClientHtml.addAsyncPluginCSS(html)
  256. ClientHtml.htmlCache[path] = html
  257. return html
  258. }
  259. private static getIndexPath (req: express.Request, res: express.Response, paramLang: string) {
  260. let lang: string
  261. // Check param lang validity
  262. if (paramLang && is18nLocale(paramLang)) {
  263. lang = paramLang
  264. // Save locale in cookies
  265. res.cookie('clientLanguage', lang, {
  266. secure: WEBSERVER.SCHEME === 'https',
  267. sameSite: 'none',
  268. maxAge: 1000 * 3600 * 24 * 90 // 3 months
  269. })
  270. } else if (req.cookies.clientLanguage && is18nLocale(req.cookies.clientLanguage)) {
  271. lang = req.cookies.clientLanguage
  272. } else {
  273. lang = req.acceptsLanguages(POSSIBLE_LOCALES) || getDefaultLocale()
  274. }
  275. logger.debug(
  276. 'Serving %s HTML language', buildFileLocale(lang),
  277. { cookie: req.cookies?.clientLanguage, paramLang, acceptLanguage: req.headers['accept-language'] }
  278. )
  279. return join(root(), 'client', 'dist', buildFileLocale(lang), 'index.html')
  280. }
  281. private static getEmbedPath () {
  282. return join(root(), 'client', 'dist', 'standalone', 'videos', 'embed.html')
  283. }
  284. private static addManifestContentHash (htmlStringPage: string) {
  285. return htmlStringPage.replace('[manifestContentHash]', FILES_CONTENT_HASH.MANIFEST)
  286. }
  287. private static addFaviconContentHash (htmlStringPage: string) {
  288. return htmlStringPage.replace('[faviconContentHash]', FILES_CONTENT_HASH.FAVICON)
  289. }
  290. private static addLogoContentHash (htmlStringPage: string) {
  291. return htmlStringPage.replace('[logoContentHash]', FILES_CONTENT_HASH.LOGO)
  292. }
  293. private static addTitleTag (htmlStringPage: string, title?: string) {
  294. let text = title || CONFIG.INSTANCE.NAME
  295. if (title) text += ` - ${CONFIG.INSTANCE.NAME}`
  296. const titleTag = `<title>${escapeHTML(text)}</title>`
  297. return htmlStringPage.replace(CUSTOM_HTML_TAG_COMMENTS.TITLE, titleTag)
  298. }
  299. private static addDescriptionTag (htmlStringPage: string, description?: string) {
  300. const content = description || CONFIG.INSTANCE.SHORT_DESCRIPTION
  301. const descriptionTag = `<meta name="description" content="${escapeHTML(content)}" />`
  302. return htmlStringPage.replace(CUSTOM_HTML_TAG_COMMENTS.DESCRIPTION, descriptionTag)
  303. }
  304. private static addCustomCSS (htmlStringPage: string) {
  305. const styleTag = `<style class="custom-css-style">${CONFIG.INSTANCE.CUSTOMIZATIONS.CSS}</style>`
  306. return htmlStringPage.replace(CUSTOM_HTML_TAG_COMMENTS.CUSTOM_CSS, styleTag)
  307. }
  308. private static addServerConfig (htmlStringPage: string, serverConfig: HTMLServerConfig) {
  309. // Stringify the JSON object, and then stringify the string object so we can inject it into the HTML
  310. const serverConfigString = JSON.stringify(JSON.stringify(serverConfig))
  311. const configScriptTag = `<script type="application/javascript">window.PeerTubeServerConfig = ${serverConfigString}</script>`
  312. return htmlStringPage.replace(CUSTOM_HTML_TAG_COMMENTS.SERVER_CONFIG, configScriptTag)
  313. }
  314. private static async addAsyncPluginCSS (htmlStringPage: string) {
  315. const globalCSSContent = await readFile(PLUGIN_GLOBAL_CSS_PATH)
  316. if (globalCSSContent.byteLength === 0) return htmlStringPage
  317. const fileHash = sha256(globalCSSContent)
  318. const linkTag = `<link rel="stylesheet" href="/plugins/global.css?hash=${fileHash}" />`
  319. return htmlStringPage.replace('</head>', linkTag + '</head>')
  320. }
  321. private static generateOpenGraphMetaTags (tags: Tags) {
  322. const metaTags = {
  323. 'og:type': tags.ogType,
  324. 'og:site_name': tags.escapedSiteName,
  325. 'og:title': tags.escapedTitle,
  326. 'og:image': tags.image.url
  327. }
  328. if (tags.image.width && tags.image.height) {
  329. metaTags['og:image:width'] = tags.image.width
  330. metaTags['og:image:height'] = tags.image.height
  331. }
  332. metaTags['og:url'] = tags.url
  333. metaTags['og:description'] = tags.escapedDescription
  334. if (tags.embed) {
  335. metaTags['og:video:url'] = tags.embed.url
  336. metaTags['og:video:secure_url'] = tags.embed.url
  337. metaTags['og:video:type'] = 'text/html'
  338. metaTags['og:video:width'] = EMBED_SIZE.width
  339. metaTags['og:video:height'] = EMBED_SIZE.height
  340. }
  341. return metaTags
  342. }
  343. private static generateStandardMetaTags (tags: Tags) {
  344. return {
  345. name: tags.escapedTitle,
  346. description: tags.escapedDescription,
  347. image: tags.image.url
  348. }
  349. }
  350. private static generateTwitterCardMetaTags (tags: Tags) {
  351. const metaTags = {
  352. 'twitter:card': tags.twitterCard,
  353. 'twitter:site': CONFIG.SERVICES.TWITTER.USERNAME,
  354. 'twitter:title': tags.escapedTitle,
  355. 'twitter:description': tags.escapedDescription,
  356. 'twitter:image': tags.image.url
  357. }
  358. if (tags.image.width && tags.image.height) {
  359. metaTags['twitter:image:width'] = tags.image.width
  360. metaTags['twitter:image:height'] = tags.image.height
  361. }
  362. if (tags.twitterCard === 'player') {
  363. metaTags['twitter:player'] = tags.embed.url
  364. metaTags['twitter:player:width'] = EMBED_SIZE.width
  365. metaTags['twitter:player:height'] = EMBED_SIZE.height
  366. }
  367. return metaTags
  368. }
  369. private static generateSchemaTags (tags: Tags) {
  370. const schema = {
  371. '@context': 'http://schema.org',
  372. '@type': tags.schemaType,
  373. 'name': tags.escapedTitle,
  374. 'description': tags.escapedDescription,
  375. 'image': tags.image.url,
  376. 'url': tags.url
  377. }
  378. if (tags.list) {
  379. schema['numberOfItems'] = tags.list.numberOfItems
  380. schema['thumbnailUrl'] = tags.image.url
  381. }
  382. if (tags.embed) {
  383. schema['embedUrl'] = tags.embed.url
  384. schema['uploadDate'] = tags.embed.createdAt
  385. if (tags.embed.duration) schema['duration'] = tags.embed.duration
  386. if (tags.embed.views) schema['iterationCount'] = tags.embed.views
  387. schema['thumbnailUrl'] = tags.image.url
  388. schema['contentUrl'] = tags.url
  389. }
  390. return schema
  391. }
  392. private static addTags (htmlStringPage: string, tagsValues: Tags) {
  393. const openGraphMetaTags = this.generateOpenGraphMetaTags(tagsValues)
  394. const standardMetaTags = this.generateStandardMetaTags(tagsValues)
  395. const twitterCardMetaTags = this.generateTwitterCardMetaTags(tagsValues)
  396. const schemaTags = this.generateSchemaTags(tagsValues)
  397. const { url, escapedTitle, embed, originUrl, disallowIndexation } = tagsValues
  398. const oembedLinkTags: { type: string, href: string, escapedTitle: string }[] = []
  399. if (embed) {
  400. oembedLinkTags.push({
  401. type: 'application/json+oembed',
  402. href: WEBSERVER.URL + '/services/oembed?url=' + encodeURIComponent(url),
  403. escapedTitle
  404. })
  405. }
  406. let tagsStr = ''
  407. // Opengraph
  408. Object.keys(openGraphMetaTags).forEach(tagName => {
  409. const tagValue = openGraphMetaTags[tagName]
  410. tagsStr += `<meta property="${tagName}" content="${tagValue}" />`
  411. })
  412. // Standard
  413. Object.keys(standardMetaTags).forEach(tagName => {
  414. const tagValue = standardMetaTags[tagName]
  415. tagsStr += `<meta property="${tagName}" content="${tagValue}" />`
  416. })
  417. // Twitter card
  418. Object.keys(twitterCardMetaTags).forEach(tagName => {
  419. const tagValue = twitterCardMetaTags[tagName]
  420. tagsStr += `<meta property="${tagName}" content="${tagValue}" />`
  421. })
  422. // OEmbed
  423. for (const oembedLinkTag of oembedLinkTags) {
  424. tagsStr += `<link rel="alternate" type="${oembedLinkTag.type}" href="${oembedLinkTag.href}" title="${oembedLinkTag.escapedTitle}" />`
  425. }
  426. // Schema.org
  427. if (schemaTags) {
  428. tagsStr += `<script type="application/ld+json">${JSON.stringify(schemaTags)}</script>`
  429. }
  430. // SEO, use origin URL
  431. tagsStr += `<link rel="canonical" href="${originUrl}" />`
  432. if (disallowIndexation) {
  433. tagsStr += `<meta name="robots" content="noindex" />`
  434. }
  435. return htmlStringPage.replace(CUSTOM_HTML_TAG_COMMENTS.META_TAGS, tagsStr)
  436. }
  437. }
  438. function sendHTML (html: string, res: express.Response, localizedHTML: boolean = false) {
  439. res.set('Content-Type', 'text/html; charset=UTF-8')
  440. if (localizedHTML) {
  441. res.set('Vary', 'Accept-Language')
  442. }
  443. return res.send(html)
  444. }
  445. async function serveIndexHTML (req: express.Request, res: express.Response) {
  446. if (req.accepts(ACCEPT_HEADERS) === 'html' || !req.headers.accept) {
  447. try {
  448. await generateHTMLPage(req, res, req.params.language)
  449. return
  450. } catch (err) {
  451. logger.error('Cannot generate HTML page.', err)
  452. return res.status(HttpStatusCode.INTERNAL_SERVER_ERROR_500).end()
  453. }
  454. }
  455. return res.status(HttpStatusCode.NOT_ACCEPTABLE_406).end()
  456. }
  457. // ---------------------------------------------------------------------------
  458. export {
  459. ClientHtml,
  460. sendHTML,
  461. serveIndexHTML
  462. }
  463. async function generateHTMLPage (req: express.Request, res: express.Response, paramLang?: string) {
  464. const html = await ClientHtml.getDefaultHTMLPage(req, res, paramLang)
  465. return sendHTML(html, res, true)
  466. }