]>
Commit | Line | Data |
---|---|---|
65fcc311 | 1 | import { parallel } from 'async' |
4d4e5cd4 C |
2 | import * as express from 'express' |
3 | import * as fs from 'fs' | |
65fcc311 | 4 | import { join } from 'path' |
4d4e5cd4 | 5 | import * as validator from 'validator' |
830bcd0f | 6 | |
e02643f3 | 7 | import { database as db } from '../initializers/database' |
65fcc311 C |
8 | import { |
9 | CONFIG, | |
10 | REMOTE_SCHEME, | |
11 | STATIC_PATHS, | |
12 | STATIC_MAX_AGE | |
13 | } from '../initializers' | |
e02643f3 | 14 | import { root } from '../helpers' |
69818c93 | 15 | import { VideoInstance } from '../models' |
830bcd0f | 16 | |
65fcc311 | 17 | const clientsRouter = express.Router() |
830bcd0f | 18 | |
65fcc311 | 19 | // TODO: move to constants |
830bcd0f | 20 | const opengraphComment = '<!-- opengraph tags -->' |
e02643f3 C |
21 | const distPath = join(root(), 'client', 'dist') |
22 | const embedPath = join(distPath, 'standalone', 'videos', 'embed.html') | |
65fcc311 | 23 | const indexPath = join(distPath, 'index.html') |
830bcd0f C |
24 | |
25 | // Special route that add OpenGraph tags | |
26 | // Do not use a template engine for a so little thing | |
65fcc311 | 27 | clientsRouter.use('/videos/watch/:id', generateWatchHtmlPage) |
830bcd0f | 28 | |
69818c93 | 29 | clientsRouter.use('/videos/embed', function (req: express.Request, res: express.Response, next: express.NextFunction) { |
830bcd0f C |
30 | res.sendFile(embedPath) |
31 | }) | |
32 | ||
79530164 | 33 | // Static HTML/CSS/JS client files |
65fcc311 | 34 | clientsRouter.use('/client', express.static(distPath, { maxAge: STATIC_MAX_AGE })) |
79530164 C |
35 | |
36 | // 404 for static files not found | |
69818c93 | 37 | clientsRouter.use('/client/*', function (req: express.Request, res: express.Response, next: express.NextFunction) { |
79530164 C |
38 | res.sendStatus(404) |
39 | }) | |
40 | ||
830bcd0f C |
41 | // --------------------------------------------------------------------------- |
42 | ||
65fcc311 C |
43 | export { |
44 | clientsRouter | |
45 | } | |
830bcd0f C |
46 | |
47 | // --------------------------------------------------------------------------- | |
48 | ||
69818c93 | 49 | function addOpenGraphTags (htmlStringPage: string, video: VideoInstance) { |
40e3f5e1 | 50 | let basePreviewUrlHttp |
41b5da1d C |
51 | |
52 | if (video.isOwned()) { | |
65fcc311 | 53 | basePreviewUrlHttp = CONFIG.WEBSERVER.URL |
41b5da1d | 54 | } else { |
65fcc311 | 55 | basePreviewUrlHttp = REMOTE_SCHEME.HTTP + '://' + video.Author.Pod.host |
41b5da1d C |
56 | } |
57 | ||
58 | // We fetch the remote preview (bigger than the thumbnail) | |
59 | // This should not overhead the remote server since social websites put in a cache the OpenGraph tags | |
60 | // We can't use the thumbnail because these social websites want bigger images (> 200x200 for Facebook for example) | |
65fcc311 C |
61 | const previewUrl = basePreviewUrlHttp + STATIC_PATHS.PREVIEWS + video.getPreviewName() |
62 | const videoUrl = CONFIG.WEBSERVER.URL + '/videos/watch/' + video.id | |
830bcd0f C |
63 | |
64 | const metaTags = { | |
65 | 'og:type': 'video', | |
66 | 'og:title': video.name, | |
41b5da1d | 67 | 'og:image': previewUrl, |
830bcd0f C |
68 | 'og:url': videoUrl, |
69 | 'og:description': video.description, | |
70 | ||
71 | 'name': video.name, | |
72 | 'description': video.description, | |
41b5da1d | 73 | 'image': previewUrl, |
830bcd0f C |
74 | |
75 | 'twitter:card': 'summary_large_image', | |
76 | 'twitter:site': '@Chocobozzz', | |
77 | 'twitter:title': video.name, | |
78 | 'twitter:description': video.description, | |
41b5da1d | 79 | 'twitter:image': previewUrl |
830bcd0f C |
80 | } |
81 | ||
82 | let tagsString = '' | |
83 | Object.keys(metaTags).forEach(function (tagName) { | |
84 | const tagValue = metaTags[tagName] | |
85 | ||
86 | tagsString += '<meta property="' + tagName + '" content="' + tagValue + '" />' | |
87 | }) | |
88 | ||
89 | return htmlStringPage.replace(opengraphComment, tagsString) | |
90 | } | |
91 | ||
69818c93 C |
92 | function generateWatchHtmlPage (req: express.Request, res: express.Response, next: express.NextFunction) { |
93 | const videoId = '' + req.params.id | |
73ce7f96 C |
94 | |
95 | // Let Angular application handle errors | |
feb4bdfd | 96 | if (!validator.isUUID(videoId, 4)) return res.sendFile(indexPath) |
73ce7f96 | 97 | |
830bcd0f C |
98 | parallel({ |
99 | file: function (callback) { | |
100 | fs.readFile(indexPath, callback) | |
101 | }, | |
102 | ||
103 | video: function (callback) { | |
7920c273 | 104 | db.Video.loadAndPopulateAuthorAndPodAndTags(videoId, callback) |
830bcd0f | 105 | } |
69818c93 | 106 | }, function (err: Error, result: { file: Buffer, video: VideoInstance }) { |
830bcd0f C |
107 | if (err) return next(err) |
108 | ||
65fcc311 C |
109 | const html = result.file.toString() |
110 | const video = result.video | |
830bcd0f | 111 | |
73ce7f96 C |
112 | // Let Angular application handle errors |
113 | if (!video) return res.sendFile(indexPath) | |
114 | ||
830bcd0f C |
115 | const htmlStringPageWithTags = addOpenGraphTags(html, video) |
116 | res.set('Content-Type', 'text/html; charset=UTF-8').send(htmlStringPageWithTags) | |
117 | }) | |
118 | } |