]>
Commit | Line | Data |
---|---|---|
4d4e5cd4 | 1 | import * as express from 'express' |
65fcc311 | 2 | import { join } from 'path' |
4d4e5cd4 | 3 | import * as validator from 'validator' |
eb080476 | 4 | import * as Bluebird from 'bluebird' |
830bcd0f | 5 | |
e02643f3 | 6 | import { database as db } from '../initializers/database' |
65fcc311 C |
7 | import { |
8 | CONFIG, | |
65fcc311 | 9 | STATIC_PATHS, |
709756b8 | 10 | STATIC_MAX_AGE, |
7ff7802a C |
11 | OPENGRAPH_AND_OEMBED_COMMENT, |
12 | EMBED_SIZE | |
65fcc311 | 13 | } from '../initializers' |
49347a0a | 14 | import { root, readFileBufferPromise, escapeHTML } from '../helpers' |
eb080476 | 15 | import { asyncMiddleware } from '../middlewares' |
69818c93 | 16 | import { VideoInstance } from '../models' |
830bcd0f | 17 | |
65fcc311 | 18 | const clientsRouter = express.Router() |
830bcd0f | 19 | |
e02643f3 C |
20 | const distPath = join(root(), 'client', 'dist') |
21 | const embedPath = join(distPath, 'standalone', 'videos', 'embed.html') | |
65fcc311 | 22 | const indexPath = join(distPath, 'index.html') |
830bcd0f | 23 | |
d8755eed | 24 | // Special route that add OpenGraph and oEmbed tags |
830bcd0f | 25 | // Do not use a template engine for a so little thing |
eb080476 C |
26 | clientsRouter.use('/videos/watch/:id', |
27 | asyncMiddleware(generateWatchHtmlPage) | |
28 | ) | |
830bcd0f | 29 | |
075f16ca | 30 | clientsRouter.use('/videos/embed', (req: express.Request, res: express.Response, next: express.NextFunction) => { |
830bcd0f C |
31 | res.sendFile(embedPath) |
32 | }) | |
33 | ||
79530164 | 34 | // Static HTML/CSS/JS client files |
65fcc311 | 35 | clientsRouter.use('/client', express.static(distPath, { maxAge: STATIC_MAX_AGE })) |
79530164 C |
36 | |
37 | // 404 for static files not found | |
075f16ca | 38 | clientsRouter.use('/client/*', (req: express.Request, res: express.Response, next: express.NextFunction) => { |
79530164 C |
39 | res.sendStatus(404) |
40 | }) | |
41 | ||
830bcd0f C |
42 | // --------------------------------------------------------------------------- |
43 | ||
65fcc311 C |
44 | export { |
45 | clientsRouter | |
46 | } | |
830bcd0f C |
47 | |
48 | // --------------------------------------------------------------------------- | |
49 | ||
d8755eed | 50 | function addOpenGraphAndOEmbedTags (htmlStringPage: string, video: VideoInstance) { |
d38309c3 | 51 | const previewUrl = CONFIG.WEBSERVER.URL + STATIC_PATHS.PREVIEWS + video.getPreviewName() |
d8755eed | 52 | const videoUrl = CONFIG.WEBSERVER.URL + '/videos/watch/' + video.uuid |
830bcd0f | 53 | |
49347a0a C |
54 | const videoName = escapeHTML(video.name) |
55 | const videoDescription = escapeHTML(video.description) | |
7ff7802a | 56 | const embedUrl = CONFIG.WEBSERVER.URL + video.getEmbedPath() |
49347a0a | 57 | |
d8755eed | 58 | const openGraphMetaTags = { |
830bcd0f | 59 | 'og:type': 'video', |
49347a0a | 60 | 'og:title': videoName, |
41b5da1d | 61 | 'og:image': previewUrl, |
830bcd0f | 62 | 'og:url': videoUrl, |
49347a0a | 63 | 'og:description': videoDescription, |
830bcd0f | 64 | |
7ff7802a C |
65 | 'og:video:url': embedUrl, |
66 | 'og:video:secure_url': embedUrl, | |
67 | 'og:video:type': 'text/html', | |
68 | 'og:video:width': EMBED_SIZE.width, | |
69 | 'og:video:height': EMBED_SIZE.height, | |
70 | ||
49347a0a C |
71 | 'name': videoName, |
72 | 'description': videoDescription, | |
41b5da1d | 73 | 'image': previewUrl, |
830bcd0f C |
74 | |
75 | 'twitter:card': 'summary_large_image', | |
76 | 'twitter:site': '@Chocobozzz', | |
49347a0a C |
77 | 'twitter:title': videoName, |
78 | 'twitter:description': videoDescription, | |
7ff7802a C |
79 | 'twitter:image': previewUrl, |
80 | 'twitter:player': embedUrl, | |
81 | 'twitter:player:width': EMBED_SIZE.width, | |
82 | 'twitter:player:height': EMBED_SIZE.height | |
830bcd0f C |
83 | } |
84 | ||
d8755eed C |
85 | const oembedLinkTags = [ |
86 | { | |
87 | type: 'application/json+oembed', | |
88 | href: CONFIG.WEBSERVER.URL + '/services/oembed?url=' + encodeURIComponent(videoUrl), | |
49347a0a | 89 | title: videoName |
d8755eed C |
90 | } |
91 | ] | |
92 | ||
830bcd0f | 93 | let tagsString = '' |
d8755eed C |
94 | Object.keys(openGraphMetaTags).forEach(tagName => { |
95 | const tagValue = openGraphMetaTags[tagName] | |
830bcd0f | 96 | |
d8755eed | 97 | tagsString += `<meta property="${tagName}" content="${tagValue}" />` |
830bcd0f C |
98 | }) |
99 | ||
d8755eed C |
100 | for (const oembedLinkTag of oembedLinkTags) { |
101 | tagsString += `<link rel="alternate" type="${oembedLinkTag.type}" href="${oembedLinkTag.href}" title="${oembedLinkTag.title}" />` | |
102 | } | |
103 | ||
104 | return htmlStringPage.replace(OPENGRAPH_AND_OEMBED_COMMENT, tagsString) | |
830bcd0f C |
105 | } |
106 | ||
eb080476 | 107 | async function generateWatchHtmlPage (req: express.Request, res: express.Response, next: express.NextFunction) { |
69818c93 | 108 | const videoId = '' + req.params.id |
eb080476 | 109 | let videoPromise: Bluebird<VideoInstance> |
73ce7f96 C |
110 | |
111 | // Let Angular application handle errors | |
0a6658fd C |
112 | if (validator.isUUID(videoId, 4)) { |
113 | videoPromise = db.Video.loadByUUIDAndPopulateAuthorAndPodAndTags(videoId) | |
114 | } else if (validator.isInt(videoId)) { | |
115 | videoPromise = db.Video.loadAndPopulateAuthorAndPodAndTags(+videoId) | |
116 | } else { | |
117 | return res.sendFile(indexPath) | |
118 | } | |
73ce7f96 | 119 | |
eb080476 | 120 | let [ file, video ] = await Promise.all([ |
6fcd19ba | 121 | readFileBufferPromise(indexPath), |
0a6658fd | 122 | videoPromise |
6fcd19ba | 123 | ]) |
830bcd0f | 124 | |
eb080476 | 125 | const html = file.toString() |
73ce7f96 | 126 | |
eb080476 C |
127 | // Let Angular application handle errors |
128 | if (!video) return res.sendFile(indexPath) | |
129 | ||
130 | const htmlStringPageWithTags = addOpenGraphAndOEmbedTags(html, video) | |
131 | res.set('Content-Type', 'text/html; charset=UTF-8').send(htmlStringPageWithTags) | |
830bcd0f | 132 | } |