aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/middlewares/validators/oembed.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/middlewares/validators/oembed.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/middlewares/validators/oembed.ts')
-rw-r--r--server/middlewares/validators/oembed.ts158
1 files changed, 0 insertions, 158 deletions
diff --git a/server/middlewares/validators/oembed.ts b/server/middlewares/validators/oembed.ts
deleted file mode 100644
index ef9a227a0..000000000
--- a/server/middlewares/validators/oembed.ts
+++ /dev/null
@@ -1,158 +0,0 @@
1import express from 'express'
2import { query } from 'express-validator'
3import { join } from 'path'
4import { loadVideo } from '@server/lib/model-loaders'
5import { VideoPlaylistModel } from '@server/models/video/video-playlist'
6import { VideoPlaylistPrivacy, VideoPrivacy } from '@shared/models'
7import { HttpStatusCode } from '../../../shared/models/http/http-error-codes'
8import { isTestOrDevInstance } from '../../helpers/core-utils'
9import { isIdOrUUIDValid, isUUIDValid, toCompleteUUID } from '../../helpers/custom-validators/misc'
10import { WEBSERVER } from '../../initializers/constants'
11import { areValidationErrors } from './shared'
12
13const playlistPaths = [
14 join('videos', 'watch', 'playlist'),
15 join('w', 'p')
16]
17
18const videoPaths = [
19 join('videos', 'watch'),
20 'w'
21]
22
23function buildUrls (paths: string[]) {
24 return paths.map(p => WEBSERVER.SCHEME + '://' + join(WEBSERVER.HOST, p) + '/')
25}
26
27const startPlaylistURLs = buildUrls(playlistPaths)
28const startVideoURLs = buildUrls(videoPaths)
29
30const isURLOptions = {
31 require_host: true,
32 require_tld: true
33}
34
35// We validate 'localhost', so we don't have the top level domain
36if (isTestOrDevInstance()) {
37 isURLOptions.require_tld = false
38}
39
40const oembedValidator = [
41 query('url')
42 .isURL(isURLOptions),
43 query('maxwidth')
44 .optional()
45 .isInt(),
46 query('maxheight')
47 .optional()
48 .isInt(),
49 query('format')
50 .optional()
51 .isIn([ 'xml', 'json' ]),
52
53 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
54 if (areValidationErrors(req, res)) return
55
56 if (req.query.format !== undefined && req.query.format !== 'json') {
57 return res.fail({
58 status: HttpStatusCode.NOT_IMPLEMENTED_501,
59 message: 'Requested format is not implemented on server.',
60 data: {
61 format: req.query.format
62 }
63 })
64 }
65
66 const url = req.query.url as string
67
68 let urlPath: string
69
70 try {
71 urlPath = new URL(url).pathname
72 } catch (err) {
73 return res.fail({
74 status: HttpStatusCode.BAD_REQUEST_400,
75 message: err.message,
76 data: {
77 url
78 }
79 })
80 }
81
82 const isPlaylist = startPlaylistURLs.some(u => url.startsWith(u))
83 const isVideo = isPlaylist ? false : startVideoURLs.some(u => url.startsWith(u))
84
85 const startIsOk = isVideo || isPlaylist
86
87 const parts = urlPath.split('/')
88
89 if (startIsOk === false || parts.length === 0) {
90 return res.fail({
91 status: HttpStatusCode.BAD_REQUEST_400,
92 message: 'Invalid url.',
93 data: {
94 url
95 }
96 })
97 }
98
99 const elementId = toCompleteUUID(parts.pop())
100 if (isIdOrUUIDValid(elementId) === false) {
101 return res.fail({ message: 'Invalid video or playlist id.' })
102 }
103
104 if (isVideo) {
105 const video = await loadVideo(elementId, 'all')
106
107 if (!video) {
108 return res.fail({
109 status: HttpStatusCode.NOT_FOUND_404,
110 message: 'Video not found'
111 })
112 }
113
114 if (
115 video.privacy === VideoPrivacy.PUBLIC ||
116 (video.privacy === VideoPrivacy.UNLISTED && isUUIDValid(elementId) === true)
117 ) {
118 res.locals.videoAll = video
119 return next()
120 }
121
122 return res.fail({
123 status: HttpStatusCode.FORBIDDEN_403,
124 message: 'Video is not publicly available'
125 })
126 }
127
128 // Is playlist
129
130 const videoPlaylist = await VideoPlaylistModel.loadWithAccountAndChannelSummary(elementId, undefined)
131 if (!videoPlaylist) {
132 return res.fail({
133 status: HttpStatusCode.NOT_FOUND_404,
134 message: 'Video playlist not found'
135 })
136 }
137
138 if (
139 videoPlaylist.privacy === VideoPlaylistPrivacy.PUBLIC ||
140 (videoPlaylist.privacy === VideoPlaylistPrivacy.UNLISTED && isUUIDValid(elementId))
141 ) {
142 res.locals.videoPlaylistSummary = videoPlaylist
143 return next()
144 }
145
146 return res.fail({
147 status: HttpStatusCode.FORBIDDEN_403,
148 message: 'Playlist is not public'
149 })
150 }
151
152]
153
154// ---------------------------------------------------------------------------
155
156export {
157 oembedValidator
158}