diff options
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 | } | ||