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/lib/video.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/lib/video.ts')
-rw-r--r-- | server/lib/video.ts | 189 |
1 files changed, 0 insertions, 189 deletions
diff --git a/server/lib/video.ts b/server/lib/video.ts deleted file mode 100644 index 362c861a5..000000000 --- a/server/lib/video.ts +++ /dev/null | |||
@@ -1,189 +0,0 @@ | |||
1 | import { UploadFiles } from 'express' | ||
2 | import memoizee from 'memoizee' | ||
3 | import { Transaction } from 'sequelize/types' | ||
4 | import { CONFIG } from '@server/initializers/config' | ||
5 | import { MEMOIZE_LENGTH, MEMOIZE_TTL } from '@server/initializers/constants' | ||
6 | import { TagModel } from '@server/models/video/tag' | ||
7 | import { VideoModel } from '@server/models/video/video' | ||
8 | import { VideoJobInfoModel } from '@server/models/video/video-job-info' | ||
9 | import { FilteredModelAttributes } from '@server/types' | ||
10 | import { MThumbnail, MVideoFullLight, MVideoTag, MVideoThumbnail, MVideoUUID } from '@server/types/models' | ||
11 | import { ManageVideoTorrentPayload, ThumbnailType, VideoCreate, VideoPrivacy, VideoState } from '@shared/models' | ||
12 | import { CreateJobArgument, JobQueue } from './job-queue/job-queue' | ||
13 | import { updateLocalVideoMiniatureFromExisting } from './thumbnail' | ||
14 | import { moveFilesIfPrivacyChanged } from './video-privacy' | ||
15 | |||
16 | function buildLocalVideoFromReq (videoInfo: VideoCreate, channelId: number): FilteredModelAttributes<VideoModel> { | ||
17 | return { | ||
18 | name: videoInfo.name, | ||
19 | remote: false, | ||
20 | category: videoInfo.category, | ||
21 | licence: videoInfo.licence ?? CONFIG.DEFAULTS.PUBLISH.LICENCE, | ||
22 | language: videoInfo.language, | ||
23 | commentsEnabled: videoInfo.commentsEnabled ?? CONFIG.DEFAULTS.PUBLISH.COMMENTS_ENABLED, | ||
24 | downloadEnabled: videoInfo.downloadEnabled ?? CONFIG.DEFAULTS.PUBLISH.DOWNLOAD_ENABLED, | ||
25 | waitTranscoding: videoInfo.waitTranscoding || false, | ||
26 | nsfw: videoInfo.nsfw || false, | ||
27 | description: videoInfo.description, | ||
28 | support: videoInfo.support, | ||
29 | privacy: videoInfo.privacy || VideoPrivacy.PRIVATE, | ||
30 | channelId, | ||
31 | originallyPublishedAt: videoInfo.originallyPublishedAt | ||
32 | ? new Date(videoInfo.originallyPublishedAt) | ||
33 | : null | ||
34 | } | ||
35 | } | ||
36 | |||
37 | async function buildVideoThumbnailsFromReq (options: { | ||
38 | video: MVideoThumbnail | ||
39 | files: UploadFiles | ||
40 | fallback: (type: ThumbnailType) => Promise<MThumbnail> | ||
41 | automaticallyGenerated?: boolean | ||
42 | }) { | ||
43 | const { video, files, fallback, automaticallyGenerated } = options | ||
44 | |||
45 | const promises = [ | ||
46 | { | ||
47 | type: ThumbnailType.MINIATURE, | ||
48 | fieldName: 'thumbnailfile' | ||
49 | }, | ||
50 | { | ||
51 | type: ThumbnailType.PREVIEW, | ||
52 | fieldName: 'previewfile' | ||
53 | } | ||
54 | ].map(p => { | ||
55 | const fields = files?.[p.fieldName] | ||
56 | |||
57 | if (fields) { | ||
58 | return updateLocalVideoMiniatureFromExisting({ | ||
59 | inputPath: fields[0].path, | ||
60 | video, | ||
61 | type: p.type, | ||
62 | automaticallyGenerated: automaticallyGenerated || false | ||
63 | }) | ||
64 | } | ||
65 | |||
66 | return fallback(p.type) | ||
67 | }) | ||
68 | |||
69 | return Promise.all(promises) | ||
70 | } | ||
71 | |||
72 | // --------------------------------------------------------------------------- | ||
73 | |||
74 | async function setVideoTags (options: { | ||
75 | video: MVideoTag | ||
76 | tags: string[] | ||
77 | transaction?: Transaction | ||
78 | }) { | ||
79 | const { video, tags, transaction } = options | ||
80 | |||
81 | const internalTags = tags || [] | ||
82 | const tagInstances = await TagModel.findOrCreateTags(internalTags, transaction) | ||
83 | |||
84 | await video.$set('Tags', tagInstances, { transaction }) | ||
85 | video.Tags = tagInstances | ||
86 | } | ||
87 | |||
88 | // --------------------------------------------------------------------------- | ||
89 | |||
90 | async function buildMoveToObjectStorageJob (options: { | ||
91 | video: MVideoUUID | ||
92 | previousVideoState: VideoState | ||
93 | isNewVideo?: boolean // Default true | ||
94 | }) { | ||
95 | const { video, previousVideoState, isNewVideo = true } = options | ||
96 | |||
97 | await VideoJobInfoModel.increaseOrCreate(video.uuid, 'pendingMove') | ||
98 | |||
99 | return { | ||
100 | type: 'move-to-object-storage' as 'move-to-object-storage', | ||
101 | payload: { | ||
102 | videoUUID: video.uuid, | ||
103 | isNewVideo, | ||
104 | previousVideoState | ||
105 | } | ||
106 | } | ||
107 | } | ||
108 | |||
109 | // --------------------------------------------------------------------------- | ||
110 | |||
111 | async function getVideoDuration (videoId: number | string) { | ||
112 | const video = await VideoModel.load(videoId) | ||
113 | |||
114 | const duration = video.isLive | ||
115 | ? undefined | ||
116 | : video.duration | ||
117 | |||
118 | return { duration, isLive: video.isLive } | ||
119 | } | ||
120 | |||
121 | const getCachedVideoDuration = memoizee(getVideoDuration, { | ||
122 | promise: true, | ||
123 | max: MEMOIZE_LENGTH.VIDEO_DURATION, | ||
124 | maxAge: MEMOIZE_TTL.VIDEO_DURATION | ||
125 | }) | ||
126 | |||
127 | // --------------------------------------------------------------------------- | ||
128 | |||
129 | async function addVideoJobsAfterUpdate (options: { | ||
130 | video: MVideoFullLight | ||
131 | isNewVideo: boolean | ||
132 | |||
133 | nameChanged: boolean | ||
134 | oldPrivacy: VideoPrivacy | ||
135 | }) { | ||
136 | const { video, nameChanged, oldPrivacy, isNewVideo } = options | ||
137 | const jobs: CreateJobArgument[] = [] | ||
138 | |||
139 | const filePathChanged = await moveFilesIfPrivacyChanged(video, oldPrivacy) | ||
140 | |||
141 | if (!video.isLive && (nameChanged || filePathChanged)) { | ||
142 | for (const file of (video.VideoFiles || [])) { | ||
143 | const payload: ManageVideoTorrentPayload = { action: 'update-metadata', videoId: video.id, videoFileId: file.id } | ||
144 | |||
145 | jobs.push({ type: 'manage-video-torrent', payload }) | ||
146 | } | ||
147 | |||
148 | const hls = video.getHLSPlaylist() | ||
149 | |||
150 | for (const file of (hls?.VideoFiles || [])) { | ||
151 | const payload: ManageVideoTorrentPayload = { action: 'update-metadata', streamingPlaylistId: hls.id, videoFileId: file.id } | ||
152 | |||
153 | jobs.push({ type: 'manage-video-torrent', payload }) | ||
154 | } | ||
155 | } | ||
156 | |||
157 | jobs.push({ | ||
158 | type: 'federate-video', | ||
159 | payload: { | ||
160 | videoUUID: video.uuid, | ||
161 | isNewVideo | ||
162 | } | ||
163 | }) | ||
164 | |||
165 | const wasConfidentialVideo = new Set([ VideoPrivacy.PRIVATE, VideoPrivacy.UNLISTED, VideoPrivacy.INTERNAL ]).has(oldPrivacy) | ||
166 | |||
167 | if (wasConfidentialVideo) { | ||
168 | jobs.push({ | ||
169 | type: 'notify', | ||
170 | payload: { | ||
171 | action: 'new-video', | ||
172 | videoUUID: video.uuid | ||
173 | } | ||
174 | }) | ||
175 | } | ||
176 | |||
177 | return JobQueue.Instance.createSequentialJobFlow(...jobs) | ||
178 | } | ||
179 | |||
180 | // --------------------------------------------------------------------------- | ||
181 | |||
182 | export { | ||
183 | buildLocalVideoFromReq, | ||
184 | buildVideoThumbnailsFromReq, | ||
185 | setVideoTags, | ||
186 | buildMoveToObjectStorageJob, | ||
187 | addVideoJobsAfterUpdate, | ||
188 | getCachedVideoDuration | ||
189 | } | ||