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-privacy.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-privacy.ts')
-rw-r--r-- | server/lib/video-privacy.ts | 133 |
1 files changed, 0 insertions, 133 deletions
diff --git a/server/lib/video-privacy.ts b/server/lib/video-privacy.ts deleted file mode 100644 index 5dd4d9781..000000000 --- a/server/lib/video-privacy.ts +++ /dev/null | |||
@@ -1,133 +0,0 @@ | |||
1 | import { move } from 'fs-extra' | ||
2 | import { join } from 'path' | ||
3 | import { logger } from '@server/helpers/logger' | ||
4 | import { DIRECTORIES } from '@server/initializers/constants' | ||
5 | import { MVideo, MVideoFile, MVideoFullLight } from '@server/types/models' | ||
6 | import { VideoPrivacy, VideoStorage } from '@shared/models' | ||
7 | import { updateHLSFilesACL, updateWebVideoFileACL } from './object-storage' | ||
8 | |||
9 | const validPrivacySet = new Set([ | ||
10 | VideoPrivacy.PRIVATE, | ||
11 | VideoPrivacy.INTERNAL, | ||
12 | VideoPrivacy.PASSWORD_PROTECTED | ||
13 | ]) | ||
14 | |||
15 | function setVideoPrivacy (video: MVideo, newPrivacy: VideoPrivacy) { | ||
16 | if (video.privacy === VideoPrivacy.PRIVATE && newPrivacy !== VideoPrivacy.PRIVATE) { | ||
17 | video.publishedAt = new Date() | ||
18 | } | ||
19 | |||
20 | video.privacy = newPrivacy | ||
21 | } | ||
22 | |||
23 | function isVideoInPrivateDirectory (privacy) { | ||
24 | return validPrivacySet.has(privacy) | ||
25 | } | ||
26 | |||
27 | function isVideoInPublicDirectory (privacy: VideoPrivacy) { | ||
28 | return !isVideoInPrivateDirectory(privacy) | ||
29 | } | ||
30 | |||
31 | async function moveFilesIfPrivacyChanged (video: MVideoFullLight, oldPrivacy: VideoPrivacy) { | ||
32 | // Now public, previously private | ||
33 | if (isVideoInPublicDirectory(video.privacy) && isVideoInPrivateDirectory(oldPrivacy)) { | ||
34 | await moveFiles({ type: 'private-to-public', video }) | ||
35 | |||
36 | return true | ||
37 | } | ||
38 | |||
39 | // Now private, previously public | ||
40 | if (isVideoInPrivateDirectory(video.privacy) && isVideoInPublicDirectory(oldPrivacy)) { | ||
41 | await moveFiles({ type: 'public-to-private', video }) | ||
42 | |||
43 | return true | ||
44 | } | ||
45 | |||
46 | return false | ||
47 | } | ||
48 | |||
49 | export { | ||
50 | setVideoPrivacy, | ||
51 | |||
52 | isVideoInPrivateDirectory, | ||
53 | isVideoInPublicDirectory, | ||
54 | |||
55 | moveFilesIfPrivacyChanged | ||
56 | } | ||
57 | |||
58 | // --------------------------------------------------------------------------- | ||
59 | |||
60 | type MoveType = 'private-to-public' | 'public-to-private' | ||
61 | |||
62 | async function moveFiles (options: { | ||
63 | type: MoveType | ||
64 | video: MVideoFullLight | ||
65 | }) { | ||
66 | const { type, video } = options | ||
67 | |||
68 | for (const file of video.VideoFiles) { | ||
69 | if (file.storage === VideoStorage.FILE_SYSTEM) { | ||
70 | await moveWebVideoFileOnFS(type, video, file) | ||
71 | } else { | ||
72 | await updateWebVideoFileACL(video, file) | ||
73 | } | ||
74 | } | ||
75 | |||
76 | const hls = video.getHLSPlaylist() | ||
77 | |||
78 | if (hls) { | ||
79 | if (hls.storage === VideoStorage.FILE_SYSTEM) { | ||
80 | await moveHLSFilesOnFS(type, video) | ||
81 | } else { | ||
82 | await updateHLSFilesACL(hls) | ||
83 | } | ||
84 | } | ||
85 | } | ||
86 | |||
87 | async function moveWebVideoFileOnFS (type: MoveType, video: MVideo, file: MVideoFile) { | ||
88 | const directories = getWebVideoDirectories(type) | ||
89 | |||
90 | const source = join(directories.old, file.filename) | ||
91 | const destination = join(directories.new, file.filename) | ||
92 | |||
93 | try { | ||
94 | logger.info('Moving web video files of %s after privacy change (%s -> %s).', video.uuid, source, destination) | ||
95 | |||
96 | await move(source, destination) | ||
97 | } catch (err) { | ||
98 | logger.error('Cannot move web video file %s to %s after privacy change', source, destination, { err }) | ||
99 | } | ||
100 | } | ||
101 | |||
102 | function getWebVideoDirectories (moveType: MoveType) { | ||
103 | if (moveType === 'private-to-public') { | ||
104 | return { old: DIRECTORIES.VIDEOS.PRIVATE, new: DIRECTORIES.VIDEOS.PUBLIC } | ||
105 | } | ||
106 | |||
107 | return { old: DIRECTORIES.VIDEOS.PUBLIC, new: DIRECTORIES.VIDEOS.PRIVATE } | ||
108 | } | ||
109 | |||
110 | // --------------------------------------------------------------------------- | ||
111 | |||
112 | async function moveHLSFilesOnFS (type: MoveType, video: MVideo) { | ||
113 | const directories = getHLSDirectories(type) | ||
114 | |||
115 | const source = join(directories.old, video.uuid) | ||
116 | const destination = join(directories.new, video.uuid) | ||
117 | |||
118 | try { | ||
119 | logger.info('Moving HLS files of %s after privacy change (%s -> %s).', video.uuid, source, destination) | ||
120 | |||
121 | await move(source, destination) | ||
122 | } catch (err) { | ||
123 | logger.error('Cannot move HLS file %s to %s after privacy change', source, destination, { err }) | ||
124 | } | ||
125 | } | ||
126 | |||
127 | function getHLSDirectories (moveType: MoveType) { | ||
128 | if (moveType === 'private-to-public') { | ||
129 | return { old: DIRECTORIES.HLS_STREAMING_PLAYLIST.PRIVATE, new: DIRECTORIES.HLS_STREAMING_PLAYLIST.PUBLIC } | ||
130 | } | ||
131 | |||
132 | return { old: DIRECTORIES.HLS_STREAMING_PLAYLIST.PUBLIC, new: DIRECTORIES.HLS_STREAMING_PLAYLIST.PRIVATE } | ||
133 | } | ||