Fix job panel sorting in administration
[oweals/peertube.git] / server / controllers / services.ts
1 import * as express from 'express'
2 import { CONFIG, EMBED_SIZE, PREVIEWS_SIZE } from '../initializers'
3 import { asyncMiddleware, oembedValidator } from '../middlewares'
4 import { accountsNameWithHostGetValidator } from '../middlewares/validators'
5 import { VideoModel } from '../models/video/video'
6
7 const servicesRouter = express.Router()
8
9 servicesRouter.use('/oembed',
10   asyncMiddleware(oembedValidator),
11   generateOEmbed
12 )
13 servicesRouter.use('/redirect/accounts/:nameWithHost',
14   asyncMiddleware(accountsNameWithHostGetValidator),
15   redirectToAccountUrl
16 )
17
18 // ---------------------------------------------------------------------------
19
20 export {
21   servicesRouter
22 }
23
24 // ---------------------------------------------------------------------------
25
26 function generateOEmbed (req: express.Request, res: express.Response, next: express.NextFunction) {
27   const video = res.locals.video as VideoModel
28   const webserverUrl = CONFIG.WEBSERVER.URL
29   const maxHeight = parseInt(req.query.maxheight, 10)
30   const maxWidth = parseInt(req.query.maxwidth, 10)
31
32   const embedUrl = webserverUrl + video.getEmbedPath()
33   let thumbnailUrl = webserverUrl + video.getPreviewPath()
34   let embedWidth = EMBED_SIZE.width
35   let embedHeight = EMBED_SIZE.height
36
37   if (maxHeight < embedHeight) embedHeight = maxHeight
38   if (maxWidth < embedWidth) embedWidth = maxWidth
39
40   // Our thumbnail is too big for the consumer
41   if (
42     (maxHeight !== undefined && maxHeight < PREVIEWS_SIZE.height) ||
43     (maxWidth !== undefined && maxWidth < PREVIEWS_SIZE.width)
44   ) {
45     thumbnailUrl = undefined
46   }
47
48   const html = `<iframe width="${embedWidth}" height="${embedHeight}" src="${embedUrl}" frameborder="0" allowfullscreen></iframe>`
49
50   const json: any = {
51     type: 'video',
52     version: '1.0',
53     html,
54     width: embedWidth,
55     height: embedHeight,
56     title: video.name,
57     author_name: video.VideoChannel.Account.name,
58     provider_name: 'PeerTube',
59     provider_url: webserverUrl
60   }
61
62   if (thumbnailUrl !== undefined) {
63     json.thumbnail_url = thumbnailUrl
64     json.thumbnail_width = PREVIEWS_SIZE.width
65     json.thumbnail_height = PREVIEWS_SIZE.height
66   }
67
68   return res.json(json)
69 }
70
71 function redirectToAccountUrl (req: express.Request, res: express.Response, next: express.NextFunction) {
72   return res.redirect(res.locals.account.Actor.url)
73 }