2017-12-28 05:16:08 -05:00
|
|
|
import * as Bluebird from 'bluebird'
|
2017-06-05 15:53:49 -04:00
|
|
|
import * as express from 'express'
|
2017-05-15 16:22:03 -04:00
|
|
|
import { join } from 'path'
|
2017-06-05 15:53:49 -04:00
|
|
|
import * as validator from 'validator'
|
2017-12-28 05:16:08 -05:00
|
|
|
import { escapeHTML, readFileBufferPromise, root } from '../helpers/core-utils'
|
2018-06-06 11:37:13 -04:00
|
|
|
import { ACCEPT_HEADERS, CONFIG, EMBED_SIZE, OPENGRAPH_AND_OEMBED_COMMENT, STATIC_MAX_AGE, STATIC_PATHS } from '../initializers'
|
2017-10-25 05:55:06 -04:00
|
|
|
import { asyncMiddleware } from '../middlewares'
|
2017-12-12 11:53:50 -05:00
|
|
|
import { VideoModel } from '../models/video/video'
|
2018-05-24 09:34:16 -04:00
|
|
|
import { VideoPrivacy } from '../../shared/models/videos'
|
2018-06-06 11:37:13 -04:00
|
|
|
import { buildFileLocale, getCompleteLocale, getDefaultLocale, is18nLocale } from '../../shared/models'
|
|
|
|
import { LOCALE_FILES } from '../../shared/models/i18n/i18n'
|
2016-11-11 04:55:07 -05:00
|
|
|
|
2017-05-15 16:22:03 -04:00
|
|
|
const clientsRouter = express.Router()
|
2016-11-11 04:55:07 -05:00
|
|
|
|
2017-05-22 14:58:25 -04:00
|
|
|
const distPath = join(root(), 'client', 'dist')
|
2018-02-22 09:29:32 -05:00
|
|
|
const assetsImagesPath = join(root(), 'client', 'dist', 'assets', 'images')
|
2017-05-22 14:58:25 -04:00
|
|
|
const embedPath = join(distPath, 'standalone', 'videos', 'embed.html')
|
2016-11-11 04:55:07 -05:00
|
|
|
|
2017-10-16 04:05:49 -04:00
|
|
|
// Special route that add OpenGraph and oEmbed tags
|
2016-11-11 04:55:07 -05:00
|
|
|
// Do not use a template engine for a so little thing
|
2017-10-25 05:55:06 -04:00
|
|
|
clientsRouter.use('/videos/watch/:id',
|
|
|
|
asyncMiddleware(generateWatchHtmlPage)
|
|
|
|
)
|
2016-11-11 04:55:07 -05:00
|
|
|
|
2017-07-11 11:04:57 -04:00
|
|
|
clientsRouter.use('/videos/embed', (req: express.Request, res: express.Response, next: express.NextFunction) => {
|
2016-11-11 04:55:07 -05:00
|
|
|
res.sendFile(embedPath)
|
|
|
|
})
|
|
|
|
|
2016-11-25 06:32:21 -05:00
|
|
|
// Static HTML/CSS/JS client files
|
2018-02-22 08:15:23 -05:00
|
|
|
|
|
|
|
const staticClientFiles = [
|
|
|
|
'manifest.json',
|
|
|
|
'ngsw-worker.js',
|
|
|
|
'ngsw.json'
|
|
|
|
]
|
|
|
|
for (const staticClientFile of staticClientFiles) {
|
|
|
|
const path = join(root(), 'client', 'dist', staticClientFile)
|
|
|
|
clientsRouter.use('/' + staticClientFile, express.static(path, { maxAge: STATIC_MAX_AGE }))
|
|
|
|
}
|
|
|
|
|
2017-05-15 16:22:03 -04:00
|
|
|
clientsRouter.use('/client', express.static(distPath, { maxAge: STATIC_MAX_AGE }))
|
2017-12-05 09:01:47 -05:00
|
|
|
clientsRouter.use('/client/assets/images', express.static(assetsImagesPath, { maxAge: STATIC_MAX_AGE }))
|
2016-11-25 06:32:21 -05:00
|
|
|
|
2018-06-06 08:23:40 -04:00
|
|
|
clientsRouter.use('/client/locales/:locale/:file.json', function (req, res) {
|
2018-06-06 10:46:42 -04:00
|
|
|
const locale = req.params.locale
|
|
|
|
const file = req.params.file
|
|
|
|
|
2018-06-06 11:37:13 -04:00
|
|
|
if (is18nLocale(locale) && LOCALE_FILES.indexOf(file) !== -1) {
|
|
|
|
const completeLocale = getCompleteLocale(locale)
|
|
|
|
const completeFileLocale = buildFileLocale(completeLocale)
|
|
|
|
return res.sendFile(join(__dirname, `../../../client/dist/locale/${file}_${completeFileLocale}.json`))
|
2018-06-06 08:23:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return res.sendStatus(404)
|
|
|
|
})
|
|
|
|
|
2016-11-25 06:32:21 -05:00
|
|
|
// 404 for static files not found
|
2017-07-11 11:04:57 -04:00
|
|
|
clientsRouter.use('/client/*', (req: express.Request, res: express.Response, next: express.NextFunction) => {
|
2016-11-25 06:32:21 -05:00
|
|
|
res.sendStatus(404)
|
|
|
|
})
|
|
|
|
|
2018-05-31 12:12:15 -04:00
|
|
|
// Always serve index client page (the client is a single page application, let it handle routing)
|
|
|
|
// Try to provide the right language index.html
|
|
|
|
clientsRouter.use('/(:language)?', function (req, res) {
|
|
|
|
if (req.accepts(ACCEPT_HEADERS) === 'html') {
|
|
|
|
return res.sendFile(getIndexPath(req, req.params.language))
|
|
|
|
}
|
|
|
|
|
|
|
|
return res.status(404).end()
|
|
|
|
})
|
|
|
|
|
2016-11-11 04:55:07 -05:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2017-05-15 16:22:03 -04:00
|
|
|
export {
|
|
|
|
clientsRouter
|
|
|
|
}
|
2016-11-11 04:55:07 -05:00
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2018-05-31 12:12:15 -04:00
|
|
|
function getIndexPath (req: express.Request, paramLang?: string) {
|
|
|
|
let lang: string
|
|
|
|
|
|
|
|
// Check param lang validity
|
|
|
|
if (paramLang && is18nLocale(paramLang)) {
|
|
|
|
lang = paramLang
|
|
|
|
} else {
|
2018-06-06 11:37:13 -04:00
|
|
|
// lang = req.acceptsLanguages(POSSIBLE_LOCALES) || getDefaultLocale()
|
2018-06-05 02:42:22 -04:00
|
|
|
// Disable auto language for now
|
|
|
|
lang = getDefaultLocale()
|
2018-05-31 12:12:15 -04:00
|
|
|
}
|
|
|
|
|
2018-06-06 11:37:13 -04:00
|
|
|
return join(__dirname, '../../../client/dist/' + buildFileLocale(lang) + '/index.html')
|
2018-05-31 12:12:15 -04:00
|
|
|
}
|
|
|
|
|
2017-12-12 11:53:50 -05:00
|
|
|
function addOpenGraphAndOEmbedTags (htmlStringPage: string, video: VideoModel) {
|
2017-07-12 06:19:39 -04:00
|
|
|
const previewUrl = CONFIG.WEBSERVER.URL + STATIC_PATHS.PREVIEWS + video.getPreviewName()
|
2017-10-16 04:05:49 -04:00
|
|
|
const videoUrl = CONFIG.WEBSERVER.URL + '/videos/watch/' + video.uuid
|
2016-11-11 04:55:07 -05:00
|
|
|
|
2017-11-28 11:11:07 -05:00
|
|
|
const videoNameEscaped = escapeHTML(video.name)
|
|
|
|
const videoDescriptionEscaped = escapeHTML(video.description)
|
2017-10-26 09:16:05 -04:00
|
|
|
const embedUrl = CONFIG.WEBSERVER.URL + video.getEmbedPath()
|
2017-10-17 10:53:10 -04:00
|
|
|
|
2017-10-16 04:05:49 -04:00
|
|
|
const openGraphMetaTags = {
|
2016-11-11 04:55:07 -05:00
|
|
|
'og:type': 'video',
|
2017-11-28 11:11:07 -05:00
|
|
|
'og:title': videoNameEscaped,
|
2016-11-11 09:44:08 -05:00
|
|
|
'og:image': previewUrl,
|
2016-11-11 04:55:07 -05:00
|
|
|
'og:url': videoUrl,
|
2017-11-28 11:11:07 -05:00
|
|
|
'og:description': videoDescriptionEscaped,
|
2016-11-11 04:55:07 -05:00
|
|
|
|
2017-10-26 09:16:05 -04:00
|
|
|
'og:video:url': embedUrl,
|
|
|
|
'og:video:secure_url': embedUrl,
|
|
|
|
'og:video:type': 'text/html',
|
|
|
|
'og:video:width': EMBED_SIZE.width,
|
|
|
|
'og:video:height': EMBED_SIZE.height,
|
|
|
|
|
2017-11-28 11:11:07 -05:00
|
|
|
'name': videoNameEscaped,
|
|
|
|
'description': videoDescriptionEscaped,
|
2016-11-11 09:44:08 -05:00
|
|
|
'image': previewUrl,
|
2016-11-11 04:55:07 -05:00
|
|
|
|
2018-05-10 06:26:47 -04:00
|
|
|
'twitter:card': CONFIG.SERVICES.TWITTER.WHITELISTED ? 'player' : 'summary_large_image',
|
|
|
|
'twitter:site': CONFIG.SERVICES.TWITTER.USERNAME,
|
2017-11-28 11:11:07 -05:00
|
|
|
'twitter:title': videoNameEscaped,
|
|
|
|
'twitter:description': videoDescriptionEscaped,
|
2017-10-26 09:16:05 -04:00
|
|
|
'twitter:image': previewUrl,
|
|
|
|
'twitter:player': embedUrl,
|
|
|
|
'twitter:player:width': EMBED_SIZE.width,
|
|
|
|
'twitter:player:height': EMBED_SIZE.height
|
2016-11-11 04:55:07 -05:00
|
|
|
}
|
|
|
|
|
2017-10-16 04:05:49 -04:00
|
|
|
const oembedLinkTags = [
|
|
|
|
{
|
|
|
|
type: 'application/json+oembed',
|
|
|
|
href: CONFIG.WEBSERVER.URL + '/services/oembed?url=' + encodeURIComponent(videoUrl),
|
2017-11-28 11:11:07 -05:00
|
|
|
title: videoNameEscaped
|
2017-10-16 04:05:49 -04:00
|
|
|
}
|
|
|
|
]
|
|
|
|
|
2018-01-23 11:09:06 -05:00
|
|
|
const schemaTags = {
|
2018-01-24 03:21:45 -05:00
|
|
|
'@context': 'http://schema.org',
|
|
|
|
'@type': 'VideoObject',
|
2018-01-23 11:09:06 -05:00
|
|
|
name: videoNameEscaped,
|
|
|
|
description: videoDescriptionEscaped,
|
2018-02-28 03:39:08 -05:00
|
|
|
thumbnailUrl: previewUrl,
|
|
|
|
uploadDate: video.createdAt.toISOString(),
|
2018-01-23 11:09:06 -05:00
|
|
|
duration: video.getActivityStreamDuration(),
|
2018-02-28 03:39:08 -05:00
|
|
|
contentUrl: videoUrl,
|
|
|
|
embedUrl: embedUrl,
|
|
|
|
interactionCount: video.views
|
2018-01-23 11:09:06 -05:00
|
|
|
}
|
|
|
|
|
2016-11-11 04:55:07 -05:00
|
|
|
let tagsString = ''
|
2018-01-24 03:21:45 -05:00
|
|
|
|
|
|
|
// Opengraph
|
2017-10-16 04:05:49 -04:00
|
|
|
Object.keys(openGraphMetaTags).forEach(tagName => {
|
|
|
|
const tagValue = openGraphMetaTags[tagName]
|
2016-11-11 04:55:07 -05:00
|
|
|
|
2017-10-16 04:05:49 -04:00
|
|
|
tagsString += `<meta property="${tagName}" content="${tagValue}" />`
|
2016-11-11 04:55:07 -05:00
|
|
|
})
|
|
|
|
|
2018-01-24 03:21:45 -05:00
|
|
|
// OEmbed
|
2017-10-16 04:05:49 -04:00
|
|
|
for (const oembedLinkTag of oembedLinkTags) {
|
|
|
|
tagsString += `<link rel="alternate" type="${oembedLinkTag.type}" href="${oembedLinkTag.href}" title="${oembedLinkTag.title}" />`
|
|
|
|
}
|
|
|
|
|
2018-01-24 03:21:45 -05:00
|
|
|
// Schema.org
|
|
|
|
tagsString += `<script type="application/ld+json">${JSON.stringify(schemaTags)}</script>`
|
2018-01-23 11:09:06 -05:00
|
|
|
|
2018-02-28 03:39:08 -05:00
|
|
|
// SEO
|
|
|
|
tagsString += `<link rel="canonical" href="${videoUrl}" />`
|
|
|
|
|
2017-10-16 04:05:49 -04:00
|
|
|
return htmlStringPage.replace(OPENGRAPH_AND_OEMBED_COMMENT, tagsString)
|
2016-11-11 04:55:07 -05:00
|
|
|
}
|
|
|
|
|
2017-10-25 05:55:06 -04:00
|
|
|
async function generateWatchHtmlPage (req: express.Request, res: express.Response, next: express.NextFunction) {
|
2017-06-10 16:15:25 -04:00
|
|
|
const videoId = '' + req.params.id
|
2017-12-12 11:53:50 -05:00
|
|
|
let videoPromise: Bluebird<VideoModel>
|
2016-11-14 16:56:40 -05:00
|
|
|
|
|
|
|
// Let Angular application handle errors
|
2017-07-11 10:01:56 -04:00
|
|
|
if (validator.isUUID(videoId, 4)) {
|
2017-12-12 11:53:50 -05:00
|
|
|
videoPromise = VideoModel.loadByUUIDAndPopulateAccountAndServerAndTags(videoId)
|
2017-07-11 10:01:56 -04:00
|
|
|
} else if (validator.isInt(videoId)) {
|
2017-12-12 11:53:50 -05:00
|
|
|
videoPromise = VideoModel.loadAndPopulateAccountAndServerAndTags(+videoId)
|
2017-07-11 10:01:56 -04:00
|
|
|
} else {
|
2018-05-31 12:12:15 -04:00
|
|
|
return res.sendFile(getIndexPath(req))
|
2017-07-11 10:01:56 -04:00
|
|
|
}
|
2016-11-14 16:56:40 -05:00
|
|
|
|
2017-10-25 05:55:06 -04:00
|
|
|
let [ file, video ] = await Promise.all([
|
2018-05-31 12:12:15 -04:00
|
|
|
readFileBufferPromise(getIndexPath(req)),
|
2017-07-11 10:01:56 -04:00
|
|
|
videoPromise
|
2017-07-05 07:26:25 -04:00
|
|
|
])
|
2016-11-11 04:55:07 -05:00
|
|
|
|
2017-10-25 05:55:06 -04:00
|
|
|
const html = file.toString()
|
2016-11-14 16:56:40 -05:00
|
|
|
|
2017-10-25 05:55:06 -04:00
|
|
|
// Let Angular application handle errors
|
2018-05-31 12:12:15 -04:00
|
|
|
if (!video || video.privacy === VideoPrivacy.PRIVATE) return res.sendFile(getIndexPath(req))
|
2017-10-25 05:55:06 -04:00
|
|
|
|
|
|
|
const htmlStringPageWithTags = addOpenGraphAndOEmbedTags(html, video)
|
|
|
|
res.set('Content-Type', 'text/html; charset=UTF-8').send(htmlStringPageWithTags)
|
2016-11-11 04:55:07 -05:00
|
|
|
}
|