diff options
author | Chocobozzz <me@florianbigard.com> | 2023-07-31 14:34:36 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2023-08-11 15:02:33 +0200 |
commit | 3a4992633ee62d5edfbb484d9c6bcb3cf158489d (patch) | |
tree | e4510b39bdac9c318fdb4b47018d08f15368b8f0 /server/controllers/api/videos/import.ts | |
parent | 04d1da5621d25d59bd5fa1543b725c497bf5d9a8 (diff) | |
download | PeerTube-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/api/videos/import.ts')
-rw-r--r-- | server/controllers/api/videos/import.ts | 262 |
1 files changed, 0 insertions, 262 deletions
diff --git a/server/controllers/api/videos/import.ts b/server/controllers/api/videos/import.ts deleted file mode 100644 index defe9efd4..000000000 --- a/server/controllers/api/videos/import.ts +++ /dev/null | |||
@@ -1,262 +0,0 @@ | |||
1 | import express from 'express' | ||
2 | import { move, readFile } from 'fs-extra' | ||
3 | import { decode } from 'magnet-uri' | ||
4 | import parseTorrent, { Instance } from 'parse-torrent' | ||
5 | import { join } from 'path' | ||
6 | import { buildYoutubeDLImport, buildVideoFromImport, insertFromImportIntoDB, YoutubeDlImportError } from '@server/lib/video-pre-import' | ||
7 | import { MThumbnail, MVideoThumbnail } from '@server/types/models' | ||
8 | import { HttpStatusCode, ServerErrorCode, ThumbnailType, VideoImportCreate, VideoImportPayload, VideoImportState } from '@shared/models' | ||
9 | import { auditLoggerFactory, getAuditIdFromRes, VideoImportAuditView } from '../../../helpers/audit-logger' | ||
10 | import { isArray } from '../../../helpers/custom-validators/misc' | ||
11 | import { cleanUpReqFiles, createReqFiles } from '../../../helpers/express-utils' | ||
12 | import { logger } from '../../../helpers/logger' | ||
13 | import { getSecureTorrentName } from '../../../helpers/utils' | ||
14 | import { CONFIG } from '../../../initializers/config' | ||
15 | import { MIMETYPES } from '../../../initializers/constants' | ||
16 | import { JobQueue } from '../../../lib/job-queue/job-queue' | ||
17 | import { updateLocalVideoMiniatureFromExisting } from '../../../lib/thumbnail' | ||
18 | import { | ||
19 | asyncMiddleware, | ||
20 | asyncRetryTransactionMiddleware, | ||
21 | authenticate, | ||
22 | videoImportAddValidator, | ||
23 | videoImportCancelValidator, | ||
24 | videoImportDeleteValidator | ||
25 | } from '../../../middlewares' | ||
26 | |||
27 | const auditLogger = auditLoggerFactory('video-imports') | ||
28 | const videoImportsRouter = express.Router() | ||
29 | |||
30 | const reqVideoFileImport = createReqFiles( | ||
31 | [ 'thumbnailfile', 'previewfile', 'torrentfile' ], | ||
32 | { ...MIMETYPES.TORRENT.MIMETYPE_EXT, ...MIMETYPES.IMAGE.MIMETYPE_EXT } | ||
33 | ) | ||
34 | |||
35 | videoImportsRouter.post('/imports', | ||
36 | authenticate, | ||
37 | reqVideoFileImport, | ||
38 | asyncMiddleware(videoImportAddValidator), | ||
39 | asyncRetryTransactionMiddleware(handleVideoImport) | ||
40 | ) | ||
41 | |||
42 | videoImportsRouter.post('/imports/:id/cancel', | ||
43 | authenticate, | ||
44 | asyncMiddleware(videoImportCancelValidator), | ||
45 | asyncRetryTransactionMiddleware(cancelVideoImport) | ||
46 | ) | ||
47 | |||
48 | videoImportsRouter.delete('/imports/:id', | ||
49 | authenticate, | ||
50 | asyncMiddleware(videoImportDeleteValidator), | ||
51 | asyncRetryTransactionMiddleware(deleteVideoImport) | ||
52 | ) | ||
53 | |||
54 | // --------------------------------------------------------------------------- | ||
55 | |||
56 | export { | ||
57 | videoImportsRouter | ||
58 | } | ||
59 | |||
60 | // --------------------------------------------------------------------------- | ||
61 | |||
62 | async function deleteVideoImport (req: express.Request, res: express.Response) { | ||
63 | const videoImport = res.locals.videoImport | ||
64 | |||
65 | await videoImport.destroy() | ||
66 | |||
67 | return res.sendStatus(HttpStatusCode.NO_CONTENT_204) | ||
68 | } | ||
69 | |||
70 | async function cancelVideoImport (req: express.Request, res: express.Response) { | ||
71 | const videoImport = res.locals.videoImport | ||
72 | |||
73 | videoImport.state = VideoImportState.CANCELLED | ||
74 | await videoImport.save() | ||
75 | |||
76 | return res.sendStatus(HttpStatusCode.NO_CONTENT_204) | ||
77 | } | ||
78 | |||
79 | function handleVideoImport (req: express.Request, res: express.Response) { | ||
80 | if (req.body.targetUrl) return handleYoutubeDlImport(req, res) | ||
81 | |||
82 | const file = req.files?.['torrentfile']?.[0] | ||
83 | if (req.body.magnetUri || file) return handleTorrentImport(req, res, file) | ||
84 | } | ||
85 | |||
86 | async function handleTorrentImport (req: express.Request, res: express.Response, torrentfile: Express.Multer.File) { | ||
87 | const body: VideoImportCreate = req.body | ||
88 | const user = res.locals.oauth.token.User | ||
89 | |||
90 | let videoName: string | ||
91 | let torrentName: string | ||
92 | let magnetUri: string | ||
93 | |||
94 | if (torrentfile) { | ||
95 | const result = await processTorrentOrAbortRequest(req, res, torrentfile) | ||
96 | if (!result) return | ||
97 | |||
98 | videoName = result.name | ||
99 | torrentName = result.torrentName | ||
100 | } else { | ||
101 | const result = processMagnetURI(body) | ||
102 | magnetUri = result.magnetUri | ||
103 | videoName = result.name | ||
104 | } | ||
105 | |||
106 | const video = await buildVideoFromImport({ | ||
107 | channelId: res.locals.videoChannel.id, | ||
108 | importData: { name: videoName }, | ||
109 | importDataOverride: body, | ||
110 | importType: 'torrent' | ||
111 | }) | ||
112 | |||
113 | const thumbnailModel = await processThumbnail(req, video) | ||
114 | const previewModel = await processPreview(req, video) | ||
115 | |||
116 | const videoImport = await insertFromImportIntoDB({ | ||
117 | video, | ||
118 | thumbnailModel, | ||
119 | previewModel, | ||
120 | videoChannel: res.locals.videoChannel, | ||
121 | tags: body.tags || undefined, | ||
122 | user, | ||
123 | videoPasswords: body.videoPasswords, | ||
124 | videoImportAttributes: { | ||
125 | magnetUri, | ||
126 | torrentName, | ||
127 | state: VideoImportState.PENDING, | ||
128 | userId: user.id | ||
129 | } | ||
130 | }) | ||
131 | |||
132 | const payload: VideoImportPayload = { | ||
133 | type: torrentfile | ||
134 | ? 'torrent-file' | ||
135 | : 'magnet-uri', | ||
136 | videoImportId: videoImport.id, | ||
137 | preventException: false | ||
138 | } | ||
139 | await JobQueue.Instance.createJob({ type: 'video-import', payload }) | ||
140 | |||
141 | auditLogger.create(getAuditIdFromRes(res), new VideoImportAuditView(videoImport.toFormattedJSON())) | ||
142 | |||
143 | return res.json(videoImport.toFormattedJSON()).end() | ||
144 | } | ||
145 | |||
146 | function statusFromYtDlImportError (err: YoutubeDlImportError): number { | ||
147 | switch (err.code) { | ||
148 | case YoutubeDlImportError.CODE.NOT_ONLY_UNICAST_URL: | ||
149 | return HttpStatusCode.FORBIDDEN_403 | ||
150 | |||
151 | case YoutubeDlImportError.CODE.FETCH_ERROR: | ||
152 | return HttpStatusCode.BAD_REQUEST_400 | ||
153 | |||
154 | default: | ||
155 | return HttpStatusCode.INTERNAL_SERVER_ERROR_500 | ||
156 | } | ||
157 | } | ||
158 | |||
159 | async function handleYoutubeDlImport (req: express.Request, res: express.Response) { | ||
160 | const body: VideoImportCreate = req.body | ||
161 | const targetUrl = body.targetUrl | ||
162 | const user = res.locals.oauth.token.User | ||
163 | |||
164 | try { | ||
165 | const { job, videoImport } = await buildYoutubeDLImport({ | ||
166 | targetUrl, | ||
167 | channel: res.locals.videoChannel, | ||
168 | importDataOverride: body, | ||
169 | thumbnailFilePath: req.files?.['thumbnailfile']?.[0].path, | ||
170 | previewFilePath: req.files?.['previewfile']?.[0].path, | ||
171 | user | ||
172 | }) | ||
173 | await JobQueue.Instance.createJob(job) | ||
174 | |||
175 | auditLogger.create(getAuditIdFromRes(res), new VideoImportAuditView(videoImport.toFormattedJSON())) | ||
176 | |||
177 | return res.json(videoImport.toFormattedJSON()).end() | ||
178 | } catch (err) { | ||
179 | logger.error('An error occurred while importing the video %s. ', targetUrl, { err }) | ||
180 | |||
181 | return res.fail({ | ||
182 | message: err.message, | ||
183 | status: statusFromYtDlImportError(err), | ||
184 | data: { | ||
185 | targetUrl | ||
186 | } | ||
187 | }) | ||
188 | } | ||
189 | } | ||
190 | |||
191 | async function processThumbnail (req: express.Request, video: MVideoThumbnail) { | ||
192 | const thumbnailField = req.files ? req.files['thumbnailfile'] : undefined | ||
193 | if (thumbnailField) { | ||
194 | const thumbnailPhysicalFile = thumbnailField[0] | ||
195 | |||
196 | return updateLocalVideoMiniatureFromExisting({ | ||
197 | inputPath: thumbnailPhysicalFile.path, | ||
198 | video, | ||
199 | type: ThumbnailType.MINIATURE, | ||
200 | automaticallyGenerated: false | ||
201 | }) | ||
202 | } | ||
203 | |||
204 | return undefined | ||
205 | } | ||
206 | |||
207 | async function processPreview (req: express.Request, video: MVideoThumbnail): Promise<MThumbnail> { | ||
208 | const previewField = req.files ? req.files['previewfile'] : undefined | ||
209 | if (previewField) { | ||
210 | const previewPhysicalFile = previewField[0] | ||
211 | |||
212 | return updateLocalVideoMiniatureFromExisting({ | ||
213 | inputPath: previewPhysicalFile.path, | ||
214 | video, | ||
215 | type: ThumbnailType.PREVIEW, | ||
216 | automaticallyGenerated: false | ||
217 | }) | ||
218 | } | ||
219 | |||
220 | return undefined | ||
221 | } | ||
222 | |||
223 | async function processTorrentOrAbortRequest (req: express.Request, res: express.Response, torrentfile: Express.Multer.File) { | ||
224 | const torrentName = torrentfile.originalname | ||
225 | |||
226 | // Rename the torrent to a secured name | ||
227 | const newTorrentPath = join(CONFIG.STORAGE.TORRENTS_DIR, getSecureTorrentName(torrentName)) | ||
228 | await move(torrentfile.path, newTorrentPath, { overwrite: true }) | ||
229 | torrentfile.path = newTorrentPath | ||
230 | |||
231 | const buf = await readFile(torrentfile.path) | ||
232 | const parsedTorrent = parseTorrent(buf) as Instance | ||
233 | |||
234 | if (parsedTorrent.files.length !== 1) { | ||
235 | cleanUpReqFiles(req) | ||
236 | |||
237 | res.fail({ | ||
238 | type: ServerErrorCode.INCORRECT_FILES_IN_TORRENT, | ||
239 | message: 'Torrents with only 1 file are supported.' | ||
240 | }) | ||
241 | return undefined | ||
242 | } | ||
243 | |||
244 | return { | ||
245 | name: extractNameFromArray(parsedTorrent.name), | ||
246 | torrentName | ||
247 | } | ||
248 | } | ||
249 | |||
250 | function processMagnetURI (body: VideoImportCreate) { | ||
251 | const magnetUri = body.magnetUri | ||
252 | const parsed = decode(magnetUri) | ||
253 | |||
254 | return { | ||
255 | name: extractNameFromArray(parsed.name), | ||
256 | magnetUri | ||
257 | } | ||
258 | } | ||
259 | |||
260 | function extractNameFromArray (name: string | string[]) { | ||
261 | return isArray(name) ? name[0] : name | ||
262 | } | ||