PeerTube/server/controllers/client.ts

120 lines
3.6 KiB
TypeScript
Raw Normal View History

2017-05-15 22:22:03 +02:00
import { parallel } from 'async'
import express = require('express')
import fs = require('fs')
import { join } from 'path'
import expressValidator = require('express-validator')
// TODO: use .validator when express-validator typing will have validator field
const validator = expressValidator['validator']
2016-11-11 10:55:07 +01:00
2017-05-22 20:58:25 +02:00
import { database as db } from '../initializers/database'
2017-05-15 22:22:03 +02:00
import {
CONFIG,
REMOTE_SCHEME,
STATIC_PATHS,
STATIC_MAX_AGE
} from '../initializers'
2017-05-22 20:58:25 +02:00
import { root } from '../helpers'
2016-11-11 10:55:07 +01:00
2017-05-15 22:22:03 +02:00
const clientsRouter = express.Router()
2016-11-11 10:55:07 +01:00
2017-05-15 22:22:03 +02:00
// TODO: move to constants
2016-11-11 10:55:07 +01:00
const opengraphComment = '<!-- opengraph tags -->'
2017-05-22 20:58:25 +02:00
const distPath = join(root(), 'client', 'dist')
const embedPath = join(distPath, 'standalone', 'videos', 'embed.html')
2017-05-15 22:22:03 +02:00
const indexPath = join(distPath, 'index.html')
2016-11-11 10:55:07 +01:00
// Special route that add OpenGraph tags
// Do not use a template engine for a so little thing
2017-05-15 22:22:03 +02:00
clientsRouter.use('/videos/watch/:id', generateWatchHtmlPage)
2016-11-11 10:55:07 +01:00
2017-05-15 22:22:03 +02:00
clientsRouter.use('/videos/embed', function (req, res, next) {
2016-11-11 10:55:07 +01:00
res.sendFile(embedPath)
})
// Static HTML/CSS/JS client files
2017-05-15 22:22:03 +02:00
clientsRouter.use('/client', express.static(distPath, { maxAge: STATIC_MAX_AGE }))
// 404 for static files not found
2017-05-15 22:22:03 +02:00
clientsRouter.use('/client/*', function (req, res, next) {
res.sendStatus(404)
})
2016-11-11 10:55:07 +01:00
// ---------------------------------------------------------------------------
2017-05-15 22:22:03 +02:00
export {
clientsRouter
}
2016-11-11 10:55:07 +01:00
// ---------------------------------------------------------------------------
function addOpenGraphTags (htmlStringPage, video) {
2016-11-18 18:55:34 +01:00
let basePreviewUrlHttp
if (video.isOwned()) {
2017-05-15 22:22:03 +02:00
basePreviewUrlHttp = CONFIG.WEBSERVER.URL
} else {
2017-05-15 22:22:03 +02:00
basePreviewUrlHttp = REMOTE_SCHEME.HTTP + '://' + video.Author.Pod.host
}
// We fetch the remote preview (bigger than the thumbnail)
// This should not overhead the remote server since social websites put in a cache the OpenGraph tags
// We can't use the thumbnail because these social websites want bigger images (> 200x200 for Facebook for example)
2017-05-15 22:22:03 +02:00
const previewUrl = basePreviewUrlHttp + STATIC_PATHS.PREVIEWS + video.getPreviewName()
const videoUrl = CONFIG.WEBSERVER.URL + '/videos/watch/' + video.id
2016-11-11 10:55:07 +01:00
const metaTags = {
'og:type': 'video',
'og:title': video.name,
'og:image': previewUrl,
2016-11-11 10:55:07 +01:00
'og:url': videoUrl,
'og:description': video.description,
'name': video.name,
'description': video.description,
'image': previewUrl,
2016-11-11 10:55:07 +01:00
'twitter:card': 'summary_large_image',
'twitter:site': '@Chocobozzz',
'twitter:title': video.name,
'twitter:description': video.description,
'twitter:image': previewUrl
2016-11-11 10:55:07 +01:00
}
let tagsString = ''
Object.keys(metaTags).forEach(function (tagName) {
const tagValue = metaTags[tagName]
tagsString += '<meta property="' + tagName + '" content="' + tagValue + '" />'
})
return htmlStringPage.replace(opengraphComment, tagsString)
}
function generateWatchHtmlPage (req, res, next) {
const videoId = req.params.id
// Let Angular application handle errors
2016-12-11 21:50:51 +01:00
if (!validator.isUUID(videoId, 4)) return res.sendFile(indexPath)
2016-11-11 10:55:07 +01:00
parallel({
file: function (callback) {
fs.readFile(indexPath, callback)
},
video: function (callback) {
2016-12-24 16:59:17 +01:00
db.Video.loadAndPopulateAuthorAndPodAndTags(videoId, callback)
2016-11-11 10:55:07 +01:00
}
2017-05-15 22:22:03 +02:00
}, function (err, result: any) {
2016-11-11 10:55:07 +01:00
if (err) return next(err)
2017-05-15 22:22:03 +02:00
const html = result.file.toString()
const video = result.video
2016-11-11 10:55:07 +01:00
// Let Angular application handle errors
if (!video) return res.sendFile(indexPath)
2016-11-11 10:55:07 +01:00
const htmlStringPageWithTags = addOpenGraphTags(html, video)
res.set('Content-Type', 'text/html; charset=UTF-8').send(htmlStringPageWithTags)
})
}