PeerTube/server/controllers/client.ts

125 lines
3.8 KiB
TypeScript
Raw Normal View History

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-10-25 11:55:06 +02:00
import * as Bluebird 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,
STATIC_PATHS,
2017-07-07 16:57:28 +02:00
STATIC_MAX_AGE,
2017-10-16 10:05:49 +02:00
OPENGRAPH_AND_OEMBED_COMMENT
2017-05-15 22:22:03 +02:00
} from '../initializers'
2017-10-17 16:53:10 +02:00
import { root, readFileBufferPromise, escapeHTML } from '../helpers'
2017-10-25 11:55:06 +02:00
import { asyncMiddleware } from '../middlewares'
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
2017-10-16 10:05:49 +02:00
// Special route that add OpenGraph and oEmbed tags
2016-11-11 10:55:07 +01:00
// Do not use a template engine for a so little thing
2017-10-25 11:55:06 +02:00
clientsRouter.use('/videos/watch/:id',
asyncMiddleware(generateWatchHtmlPage)
)
2016-11-11 10:55:07 +01:00
2017-07-11 17:04:57 +02:00
clientsRouter.use('/videos/embed', (req: express.Request, res: express.Response, next: express.NextFunction) => {
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-07-11 17:04:57 +02:00
clientsRouter.use('/client/*', (req: express.Request, res: express.Response, next: express.NextFunction) => {
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-10-16 10:05:49 +02:00
function addOpenGraphAndOEmbedTags (htmlStringPage: string, video: VideoInstance) {
const previewUrl = CONFIG.WEBSERVER.URL + STATIC_PATHS.PREVIEWS + video.getPreviewName()
2017-10-16 10:05:49 +02:00
const videoUrl = CONFIG.WEBSERVER.URL + '/videos/watch/' + video.uuid
2016-11-11 10:55:07 +01:00
2017-10-17 16:53:10 +02:00
const videoName = escapeHTML(video.name)
const videoDescription = escapeHTML(video.description)
2017-10-16 10:05:49 +02:00
const openGraphMetaTags = {
2016-11-11 10:55:07 +01:00
'og:type': 'video',
2017-10-17 16:53:10 +02:00
'og:title': videoName,
'og:image': previewUrl,
2016-11-11 10:55:07 +01:00
'og:url': videoUrl,
2017-10-17 16:53:10 +02:00
'og:description': videoDescription,
2016-11-11 10:55:07 +01:00
2017-10-17 16:53:10 +02:00
'name': videoName,
'description': videoDescription,
'image': previewUrl,
2016-11-11 10:55:07 +01:00
'twitter:card': 'summary_large_image',
'twitter:site': '@Chocobozzz',
2017-10-17 16:53:10 +02:00
'twitter:title': videoName,
'twitter:description': videoDescription,
'twitter:image': previewUrl
2016-11-11 10:55:07 +01:00
}
2017-10-16 10:05:49 +02:00
const oembedLinkTags = [
{
type: 'application/json+oembed',
href: CONFIG.WEBSERVER.URL + '/services/oembed?url=' + encodeURIComponent(videoUrl),
2017-10-17 16:53:10 +02:00
title: videoName
2017-10-16 10:05:49 +02:00
}
]
2016-11-11 10:55:07 +01:00
let tagsString = ''
2017-10-16 10:05:49 +02:00
Object.keys(openGraphMetaTags).forEach(tagName => {
const tagValue = openGraphMetaTags[tagName]
2016-11-11 10:55:07 +01:00
2017-10-16 10:05:49 +02:00
tagsString += `<meta property="${tagName}" content="${tagValue}" />`
2016-11-11 10:55:07 +01:00
})
2017-10-16 10:05:49 +02:00
for (const oembedLinkTag of oembedLinkTags) {
tagsString += `<link rel="alternate" type="${oembedLinkTag.type}" href="${oembedLinkTag.href}" title="${oembedLinkTag.title}" />`
}
return htmlStringPage.replace(OPENGRAPH_AND_OEMBED_COMMENT, tagsString)
2016-11-11 10:55:07 +01:00
}
2017-10-25 11:55:06 +02:00
async function generateWatchHtmlPage (req: express.Request, res: express.Response, next: express.NextFunction) {
2017-06-10 22:15:25 +02:00
const videoId = '' + req.params.id
2017-10-25 11:55:06 +02:00
let videoPromise: Bluebird<VideoInstance>
// Let Angular application handle errors
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)
}
2017-10-25 11:55:06 +02:00
let [ file, video ] = await Promise.all([
readFileBufferPromise(indexPath),
videoPromise
])
2016-11-11 10:55:07 +01:00
2017-10-25 11:55:06 +02:00
file = file as Buffer
video = video as VideoInstance
2016-11-11 10:55:07 +01:00
2017-10-25 11:55:06 +02:00
const html = file.toString()
2017-10-25 11:55:06 +02:00
// Let Angular application handle errors
if (!video) return res.sendFile(indexPath)
const htmlStringPageWithTags = addOpenGraphAndOEmbedTags(html, video)
res.set('Content-Type', 'text/html; charset=UTF-8').send(htmlStringPageWithTags)
2016-11-11 10:55:07 +01:00
}