]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/services.ts
Reorganize plugin models
[github/Chocobozzz/PeerTube.git] / server / controllers / services.ts
CommitLineData
d8755eed 1import * as express from 'express'
6fad8e51 2import { EMBED_SIZE, PREVIEWS_SIZE, WEBSERVER, THUMBNAILS_SIZE } from '../initializers/constants'
3fd3ab2d 3import { asyncMiddleware, oembedValidator } from '../middlewares'
418d092a 4import { accountNameWithHostGetValidator } from '../middlewares/validators'
6fad8e51 5import { MChannelSummary } from '@server/types/models'
4097c6d6 6import { escapeHTML } from '@shared/core-utils/renderer'
d8755eed
C
7
8const servicesRouter = express.Router()
9
a2431b7d
C
10servicesRouter.use('/oembed',
11 asyncMiddleware(oembedValidator),
12 generateOEmbed
13)
ad9e39fb 14servicesRouter.use('/redirect/accounts/:accountName',
418d092a 15 asyncMiddleware(accountNameWithHostGetValidator),
e8cb4409
C
16 redirectToAccountUrl
17)
d8755eed
C
18
19// ---------------------------------------------------------------------------
20
21export {
22 servicesRouter
23}
24
25// ---------------------------------------------------------------------------
26
dae86118 27function generateOEmbed (req: express.Request, res: express.Response) {
6fad8e51
C
28 if (res.locals.videoAll) return generateVideoOEmbed(req, res)
29
30 return generatePlaylistOEmbed(req, res)
31}
32
33function generatePlaylistOEmbed (req: express.Request, res: express.Response) {
34 const playlist = res.locals.videoPlaylistSummary
35
36 const json = buildOEmbed({
37 channel: playlist.VideoChannel,
38 title: playlist.name,
39 embedPath: playlist.getEmbedStaticPath(),
40 previewPath: playlist.getThumbnailStaticPath(),
41 previewSize: THUMBNAILS_SIZE,
42 req
43 })
44
45 return res.json(json)
46}
47
48function generateVideoOEmbed (req: express.Request, res: express.Response) {
453e83ea 49 const video = res.locals.videoAll
6fad8e51
C
50
51 const json = buildOEmbed({
52 channel: video.VideoChannel,
53 title: video.name,
54 embedPath: video.getEmbedStaticPath(),
55 previewPath: video.getPreviewStaticPath(),
56 previewSize: PREVIEWS_SIZE,
57 req
58 })
59
60 return res.json(json)
61}
62
63function buildOEmbed (options: {
64 req: express.Request
65 title: string
66 channel: MChannelSummary
67 previewPath: string | null
68 embedPath: string
69 previewSize: {
70 height: number
71 width: number
72 }
73}) {
74 const { req, previewSize, previewPath, title, channel, embedPath } = options
75
6dd9de95 76 const webserverUrl = WEBSERVER.URL
d8755eed
C
77 const maxHeight = parseInt(req.query.maxheight, 10)
78 const maxWidth = parseInt(req.query.maxwidth, 10)
79
6fad8e51 80 const embedUrl = webserverUrl + embedPath
164174a6
C
81 let embedWidth = EMBED_SIZE.width
82 let embedHeight = EMBED_SIZE.height
4097c6d6 83 const embedTitle = escapeHTML(title)
d8755eed 84
6fad8e51
C
85 let thumbnailUrl = previewPath
86 ? webserverUrl + previewPath
87 : undefined
88
d8755eed
C
89 if (maxHeight < embedHeight) embedHeight = maxHeight
90 if (maxWidth < embedWidth) embedWidth = maxWidth
91
92 // Our thumbnail is too big for the consumer
93 if (
6fad8e51
C
94 (maxHeight !== undefined && maxHeight < previewSize.height) ||
95 (maxWidth !== undefined && maxWidth < previewSize.width)
d8755eed
C
96 ) {
97 thumbnailUrl = undefined
98 }
99
6ccdf3a2 100 const html = `<iframe width="${embedWidth}" height="${embedHeight}" sandbox="allow-same-origin allow-scripts" ` +
4097c6d6 101 `title="${embedTitle}" src="${embedUrl}" frameborder="0" allowfullscreen></iframe>`
d8755eed
C
102
103 const json: any = {
104 type: 'video',
105 version: '1.0',
106 html,
107 width: embedWidth,
108 height: embedHeight,
6fad8e51
C
109 title: title,
110 author_name: channel.name,
111 author_url: channel.Actor.url,
d8755eed
C
112 provider_name: 'PeerTube',
113 provider_url: webserverUrl
114 }
115
116 if (thumbnailUrl !== undefined) {
117 json.thumbnail_url = thumbnailUrl
6fad8e51
C
118 json.thumbnail_width = previewSize.width
119 json.thumbnail_height = previewSize.height
d8755eed
C
120 }
121
6fad8e51 122 return json
d8755eed 123}
e8cb4409
C
124
125function redirectToAccountUrl (req: express.Request, res: express.Response, next: express.NextFunction) {
126 return res.redirect(res.locals.account.Actor.url)
127}