aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/controllers/services.ts
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2023-07-31 14:34:36 +0200
committerChocobozzz <me@florianbigard.com>2023-08-11 15:02:33 +0200
commit3a4992633ee62d5edfbb484d9c6bcb3cf158489d (patch)
treee4510b39bdac9c318fdb4b47018d08f15368b8f0 /server/controllers/services.ts
parent04d1da5621d25d59bd5fa1543b725c497bf5d9a8 (diff)
downloadPeerTube-3a4992633ee62d5edfbb484d9c6bcb3cf158489d.tar.gz
PeerTube-3a4992633ee62d5edfbb484d9c6bcb3cf158489d.tar.zst
PeerTube-3a4992633ee62d5edfbb484d9c6bcb3cf158489d.zip
Migrate server to ESM
Sorry for the very big commit that may lead to git log issues and merge conflicts, but it's a major step forward: * Server can be faster at startup because imports() are async and we can easily lazy import big modules * Angular doesn't seem to support ES import (with .js extension), so we had to correctly organize peertube into a monorepo: * Use yarn workspace feature * Use typescript reference projects for dependencies * Shared projects have been moved into "packages", each one is now a node module (with a dedicated package.json/tsconfig.json) * server/tools have been moved into apps/ and is now a dedicated app bundled and published on NPM so users don't have to build peertube cli tools manually * server/tests have been moved into packages/ so we don't compile them every time we want to run the server * Use isolatedModule option: * Had to move from const enum to const (https://www.typescriptlang.org/docs/handbook/enums.html#objects-vs-enums) * Had to explictely specify "type" imports when used in decorators * Prefer tsx (that uses esbuild under the hood) instead of ts-node to load typescript files (tests with mocha or scripts): * To reduce test complexity as esbuild doesn't support decorator metadata, we only test server files that do not import server models * We still build tests files into js files for a faster CI * Remove unmaintained peertube CLI import script * Removed some barrels to speed up execution (less imports)
Diffstat (limited to 'server/controllers/services.ts')
-rw-r--r--server/controllers/services.ts165
1 files changed, 0 insertions, 165 deletions
diff --git a/server/controllers/services.ts b/server/controllers/services.ts
deleted file mode 100644
index 0fd63a30f..000000000
--- a/server/controllers/services.ts
+++ /dev/null
@@ -1,165 +0,0 @@
1import express from 'express'
2import { MChannelSummary } from '@server/types/models'
3import { escapeHTML } from '@shared/core-utils/renderer'
4import { EMBED_SIZE, PREVIEWS_SIZE, THUMBNAILS_SIZE, WEBSERVER } from '../initializers/constants'
5import { apiRateLimiter, asyncMiddleware, oembedValidator } from '../middlewares'
6import { accountNameWithHostGetValidator } from '../middlewares/validators'
7import { forceNumber } from '@shared/core-utils'
8
9const servicesRouter = express.Router()
10
11servicesRouter.use('/oembed',
12 apiRateLimiter,
13 asyncMiddleware(oembedValidator),
14 generateOEmbed
15)
16servicesRouter.use('/redirect/accounts/:accountName',
17 apiRateLimiter,
18 asyncMiddleware(accountNameWithHostGetValidator),
19 redirectToAccountUrl
20)
21
22// ---------------------------------------------------------------------------
23
24export {
25 servicesRouter
26}
27
28// ---------------------------------------------------------------------------
29
30function generateOEmbed (req: express.Request, res: express.Response) {
31 if (res.locals.videoAll) return generateVideoOEmbed(req, res)
32
33 return generatePlaylistOEmbed(req, res)
34}
35
36function generatePlaylistOEmbed (req: express.Request, res: express.Response) {
37 const playlist = res.locals.videoPlaylistSummary
38
39 const json = buildOEmbed({
40 channel: playlist.VideoChannel,
41 title: playlist.name,
42 embedPath: playlist.getEmbedStaticPath() + buildPlayerURLQuery(req.query.url),
43 previewPath: playlist.getThumbnailStaticPath(),
44 previewSize: THUMBNAILS_SIZE,
45 req
46 })
47
48 return res.json(json)
49}
50
51function generateVideoOEmbed (req: express.Request, res: express.Response) {
52 const video = res.locals.videoAll
53
54 const json = buildOEmbed({
55 channel: video.VideoChannel,
56 title: video.name,
57 embedPath: video.getEmbedStaticPath() + buildPlayerURLQuery(req.query.url),
58 previewPath: video.getPreviewStaticPath(),
59 previewSize: PREVIEWS_SIZE,
60 req
61 })
62
63 return res.json(json)
64}
65
66function buildPlayerURLQuery (inputQueryUrl: string) {
67 const allowedParameters = new Set([
68 'start',
69 'stop',
70 'loop',
71 'autoplay',
72 'muted',
73 'controls',
74 'controlBar',
75 'title',
76 'api',
77 'warningTitle',
78 'peertubeLink',
79 'p2p',
80 'subtitle',
81 'bigPlayBackgroundColor',
82 'mode',
83 'foregroundColor'
84 ])
85
86 const params = new URLSearchParams()
87
88 new URL(inputQueryUrl).searchParams.forEach((v, k) => {
89 if (allowedParameters.has(k)) {
90 params.append(k, v)
91 }
92 })
93
94 const stringQuery = params.toString()
95 if (!stringQuery) return ''
96
97 return '?' + stringQuery
98}
99
100function buildOEmbed (options: {
101 req: express.Request
102 title: string
103 channel: MChannelSummary
104 previewPath: string | null
105 embedPath: string
106 previewSize: {
107 height: number
108 width: number
109 }
110}) {
111 const { req, previewSize, previewPath, title, channel, embedPath } = options
112
113 const webserverUrl = WEBSERVER.URL
114 const maxHeight = forceNumber(req.query.maxheight)
115 const maxWidth = forceNumber(req.query.maxwidth)
116
117 const embedUrl = webserverUrl + embedPath
118 const embedTitle = escapeHTML(title)
119
120 let thumbnailUrl = previewPath
121 ? webserverUrl + previewPath
122 : undefined
123
124 let embedWidth = EMBED_SIZE.width
125 if (maxWidth < embedWidth) embedWidth = maxWidth
126
127 let embedHeight = EMBED_SIZE.height
128 if (maxHeight < embedHeight) embedHeight = maxHeight
129
130 // Our thumbnail is too big for the consumer
131 if (
132 (maxHeight !== undefined && maxHeight < previewSize.height) ||
133 (maxWidth !== undefined && maxWidth < previewSize.width)
134 ) {
135 thumbnailUrl = undefined
136 }
137
138 const html = `<iframe width="${embedWidth}" height="${embedHeight}" sandbox="allow-same-origin allow-scripts allow-popups" ` +
139 `title="${embedTitle}" src="${embedUrl}" frameborder="0" allowfullscreen></iframe>`
140
141 const json: any = {
142 type: 'video',
143 version: '1.0',
144 html,
145 width: embedWidth,
146 height: embedHeight,
147 title,
148 author_name: channel.name,
149 author_url: channel.Actor.url,
150 provider_name: 'PeerTube',
151 provider_url: webserverUrl
152 }
153
154 if (thumbnailUrl !== undefined) {
155 json.thumbnail_url = thumbnailUrl
156 json.thumbnail_width = previewSize.width
157 json.thumbnail_height = previewSize.height
158 }
159
160 return json
161}
162
163function redirectToAccountUrl (req: express.Request, res: express.Response, next: express.NextFunction) {
164 return res.redirect(res.locals.account.Actor.url)
165}