2017-06-05 21:53:49 +02:00
|
|
|
import * as express from 'express'
|
2017-05-15 22:22:03 +02:00
|
|
|
import { join } from 'path'
|
2017-06-05 21:53:49 +02:00
|
|
|
import * as validator from 'validator'
|
2017-07-05 13:26:25 +02:00
|
|
|
import * as Promise from 'bluebird'
|
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,
|
2017-07-07 16:57:28 +02:00
|
|
|
STATIC_MAX_AGE,
|
|
|
|
OPENGRAPH_COMMENT
|
2017-05-15 22:22:03 +02:00
|
|
|
} from '../initializers'
|
2017-07-05 13:26:25 +02:00
|
|
|
import { root, readFileBufferPromise } from '../helpers'
|
2017-06-10 22:15:25 +02:00
|
|
|
import { VideoInstance } from '../models'
|
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-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-06-10 22:15:25 +02:00
|
|
|
clientsRouter.use('/videos/embed', function (req: express.Request, res: express.Response, next: express.NextFunction) {
|
2016-11-11 10:55:07 +01:00
|
|
|
res.sendFile(embedPath)
|
|
|
|
})
|
|
|
|
|
2016-11-25 12:32:21 +01:00
|
|
|
// Static HTML/CSS/JS client files
|
2017-05-15 22:22:03 +02:00
|
|
|
clientsRouter.use('/client', express.static(distPath, { maxAge: STATIC_MAX_AGE }))
|
2016-11-25 12:32:21 +01:00
|
|
|
|
|
|
|
// 404 for static files not found
|
2017-06-10 22:15:25 +02:00
|
|
|
clientsRouter.use('/client/*', function (req: express.Request, res: express.Response, next: express.NextFunction) {
|
2016-11-25 12:32:21 +01:00
|
|
|
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
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2017-06-10 22:15:25 +02:00
|
|
|
function addOpenGraphTags (htmlStringPage: string, video: VideoInstance) {
|
2016-11-18 18:55:34 +01:00
|
|
|
let basePreviewUrlHttp
|
2016-11-11 15:44:08 +01:00
|
|
|
|
|
|
|
if (video.isOwned()) {
|
2017-05-15 22:22:03 +02:00
|
|
|
basePreviewUrlHttp = CONFIG.WEBSERVER.URL
|
2016-11-11 15:44:08 +01:00
|
|
|
} else {
|
2017-05-15 22:22:03 +02:00
|
|
|
basePreviewUrlHttp = REMOTE_SCHEME.HTTP + '://' + video.Author.Pod.host
|
2016-11-11 15:44:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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,
|
2016-11-11 15:44:08 +01:00
|
|
|
'og:image': previewUrl,
|
2016-11-11 10:55:07 +01:00
|
|
|
'og:url': videoUrl,
|
|
|
|
'og:description': video.description,
|
|
|
|
|
|
|
|
'name': video.name,
|
|
|
|
'description': video.description,
|
2016-11-11 15:44:08 +01:00
|
|
|
'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,
|
2016-11-11 15:44:08 +01:00
|
|
|
'twitter:image': previewUrl
|
2016-11-11 10:55:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
let tagsString = ''
|
2017-07-11 16:01:56 +02:00
|
|
|
Object.keys(metaTags).forEach(tagName => {
|
2016-11-11 10:55:07 +01:00
|
|
|
const tagValue = metaTags[tagName]
|
|
|
|
|
|
|
|
tagsString += '<meta property="' + tagName + '" content="' + tagValue + '" />'
|
|
|
|
})
|
|
|
|
|
2017-07-07 16:57:28 +02:00
|
|
|
return htmlStringPage.replace(OPENGRAPH_COMMENT, tagsString)
|
2016-11-11 10:55:07 +01:00
|
|
|
}
|
|
|
|
|
2017-06-10 22:15:25 +02:00
|
|
|
function generateWatchHtmlPage (req: express.Request, res: express.Response, next: express.NextFunction) {
|
|
|
|
const videoId = '' + req.params.id
|
2017-07-11 16:01:56 +02:00
|
|
|
let videoPromise: Promise<VideoInstance>
|
2016-11-14 22:56:40 +01:00
|
|
|
|
|
|
|
// Let Angular application handle errors
|
2017-07-11 16:01:56 +02:00
|
|
|
if (validator.isUUID(videoId, 4)) {
|
|
|
|
videoPromise = db.Video.loadByUUIDAndPopulateAuthorAndPodAndTags(videoId)
|
|
|
|
} else if (validator.isInt(videoId)) {
|
|
|
|
videoPromise = db.Video.loadAndPopulateAuthorAndPodAndTags(+videoId)
|
|
|
|
} else {
|
|
|
|
return res.sendFile(indexPath)
|
|
|
|
}
|
2016-11-14 22:56:40 +01:00
|
|
|
|
2017-07-05 13:26:25 +02:00
|
|
|
Promise.all([
|
|
|
|
readFileBufferPromise(indexPath),
|
2017-07-11 16:01:56 +02:00
|
|
|
videoPromise
|
2017-07-05 13:26:25 +02:00
|
|
|
])
|
|
|
|
.then(([ file, video ]) => {
|
|
|
|
file = file as Buffer
|
|
|
|
video = video as VideoInstance
|
2016-11-11 10:55:07 +01:00
|
|
|
|
2017-07-05 13:26:25 +02:00
|
|
|
const html = file.toString()
|
2016-11-11 10:55:07 +01:00
|
|
|
|
2016-11-14 22:56:40 +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)
|
|
|
|
})
|
2017-07-05 13:26:25 +02:00
|
|
|
.catch(err => next(err))
|
2016-11-11 10:55:07 +01:00
|
|
|
}
|