import * as express from 'express'
import * as Bluebird from 'bluebird'
import { buildFileLocale, getDefaultLocale, is18nLocale, POSSIBLE_LOCALES } from '../../shared/models/i18n/i18n'
-import { CONFIG, CUSTOM_HTML_TAG_COMMENTS, EMBED_SIZE, STATIC_PATHS } from '../initializers'
+import { CONFIG, CUSTOM_HTML_TAG_COMMENTS, EMBED_SIZE } from '../initializers'
import { join } from 'path'
import { escapeHTML } from '../helpers/core-utils'
import { VideoModel } from '../models/video/video'
import * as validator from 'validator'
import { VideoPrivacy } from '../../shared/models/videos'
import { readFile } from 'fs-extra'
+import { getActivityStreamDuration } from '../models/video/video-format-utils'
export class ClientHtml {
ClientHtml.htmlCache = {}
}
- static async getIndexHTML (req: express.Request, res: express.Response, paramLang?: string) {
- const path = ClientHtml.getIndexPath(req, res, paramLang)
- if (ClientHtml.htmlCache[path]) return ClientHtml.htmlCache[path]
-
- const buffer = await readFile(path)
+ static async getDefaultHTMLPage (req: express.Request, res: express.Response, paramLang?: string) {
+ const html = await ClientHtml.getIndexHTML(req, res, paramLang)
- let html = buffer.toString()
-
- html = ClientHtml.addTitleTag(html)
- html = ClientHtml.addDescriptionTag(html)
- html = ClientHtml.addCustomCSS(html)
+ let customHtml = ClientHtml.addTitleTag(html)
+ customHtml = ClientHtml.addDescriptionTag(customHtml)
- ClientHtml.htmlCache[path] = html
-
- return html
+ return customHtml
}
static async getWatchHTMLPage (videoId: string, req: express.Request, res: express.Response) {
let videoPromise: Bluebird<VideoModel>
// Let Angular application handle errors
- if (validator.isUUID(videoId, 4)) {
- videoPromise = VideoModel.loadByUUIDAndPopulateAccountAndServerAndTags(videoId)
- } else if (validator.isInt(videoId)) {
- videoPromise = VideoModel.loadAndPopulateAccountAndServerAndTags(+videoId)
+ if (validator.isInt(videoId) || validator.isUUID(videoId, 4)) {
+ videoPromise = VideoModel.loadAndPopulateAccountAndServerAndTags(videoId)
} else {
return ClientHtml.getIndexHTML(req, res)
}
return ClientHtml.getIndexHTML(req, res)
}
- return ClientHtml.addOpenGraphAndOEmbedTags(html, video)
+ let customHtml = ClientHtml.addTitleTag(html, escapeHTML(video.name))
+ customHtml = ClientHtml.addDescriptionTag(customHtml, escapeHTML(video.description))
+ customHtml = ClientHtml.addOpenGraphAndOEmbedTags(customHtml, video)
+
+ return customHtml
+ }
+
+ private static async getIndexHTML (req: express.Request, res: express.Response, paramLang?: string) {
+ const path = ClientHtml.getIndexPath(req, res, paramLang)
+ if (ClientHtml.htmlCache[path]) return ClientHtml.htmlCache[path]
+
+ const buffer = await readFile(path)
+
+ let html = buffer.toString()
+
+ html = ClientHtml.addCustomCSS(html)
+
+ ClientHtml.htmlCache[path] = html
+
+ return html
}
private static getIndexPath (req: express.Request, res: express.Response, paramLang?: string) {
return join(__dirname, '../../../client/dist/' + buildFileLocale(lang) + '/index.html')
}
- private static addTitleTag (htmlStringPage: string) {
- const titleTag = '<title>' + CONFIG.INSTANCE.NAME + '</title>'
+ private static addTitleTag (htmlStringPage: string, title?: string) {
+ let text = title || CONFIG.INSTANCE.NAME
+ if (title) text += ` - ${CONFIG.INSTANCE.NAME}`
+
+ const titleTag = `<title>${text}</title>`
return htmlStringPage.replace(CUSTOM_HTML_TAG_COMMENTS.TITLE, titleTag)
}
- private static addDescriptionTag (htmlStringPage: string) {
- const descriptionTag = `<meta name="description" content="${CONFIG.INSTANCE.SHORT_DESCRIPTION}" />`
+ private static addDescriptionTag (htmlStringPage: string, description?: string) {
+ const content = description || CONFIG.INSTANCE.SHORT_DESCRIPTION
+ const descriptionTag = `<meta name="description" content="${content}" />`
return htmlStringPage.replace(CUSTOM_HTML_TAG_COMMENTS.DESCRIPTION, descriptionTag)
}
}
private static addOpenGraphAndOEmbedTags (htmlStringPage: string, video: VideoModel) {
- const previewUrl = CONFIG.WEBSERVER.URL + STATIC_PATHS.PREVIEWS + video.getPreviewName()
- const videoUrl = CONFIG.WEBSERVER.URL + '/videos/watch/' + video.uuid
+ const previewUrl = CONFIG.WEBSERVER.URL + video.getPreviewStaticPath()
+ const videoUrl = CONFIG.WEBSERVER.URL + video.getWatchStaticPath()
const videoNameEscaped = escapeHTML(video.name)
const videoDescriptionEscaped = escapeHTML(video.description)
description: videoDescriptionEscaped,
thumbnailUrl: previewUrl,
uploadDate: video.createdAt.toISOString(),
- duration: video.getActivityStreamDuration(),
+ duration: getActivityStreamDuration(video.duration),
contentUrl: videoUrl,
embedUrl: embedUrl,
interactionCount: video.views
// Schema.org
tagsString += `<script type="application/ld+json">${JSON.stringify(schemaTags)}</script>`
- // SEO
- tagsString += `<link rel="canonical" href="${videoUrl}" />`
+ // SEO, use origin video url so Google does not index remote videos
+ tagsString += `<link rel="canonical" href="${video.url}" />`
return htmlStringPage.replace(CUSTOM_HTML_TAG_COMMENTS.OPENGRAPH_AND_OEMBED, tagsString)
}