]>
Commit | Line | Data |
---|---|---|
3545e72c C |
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' | |
9ab330b9 C |
5 | import { MVideo, MVideoFile, MVideoFullLight } from '@server/types/models' |
6 | import { VideoPrivacy, VideoStorage } from '@shared/models' | |
7 | import { updateHLSFilesACL, updateWebTorrentFileACL } from './object-storage' | |
3545e72c C |
8 | |
9 | function setVideoPrivacy (video: MVideo, newPrivacy: VideoPrivacy) { | |
10 | if (video.privacy === VideoPrivacy.PRIVATE && newPrivacy !== VideoPrivacy.PRIVATE) { | |
11 | video.publishedAt = new Date() | |
12 | } | |
13 | ||
14 | video.privacy = newPrivacy | |
15 | } | |
16 | ||
17 | function isVideoInPrivateDirectory (privacy: VideoPrivacy) { | |
18 | return privacy === VideoPrivacy.PRIVATE || privacy === VideoPrivacy.INTERNAL | |
19 | } | |
20 | ||
21 | function isVideoInPublicDirectory (privacy: VideoPrivacy) { | |
22 | return !isVideoInPrivateDirectory(privacy) | |
23 | } | |
24 | ||
25 | async function moveFilesIfPrivacyChanged (video: MVideoFullLight, oldPrivacy: VideoPrivacy) { | |
26 | // Now public, previously private | |
27 | if (isVideoInPublicDirectory(video.privacy) && isVideoInPrivateDirectory(oldPrivacy)) { | |
28 | await moveFiles({ type: 'private-to-public', video }) | |
29 | ||
30 | return true | |
31 | } | |
32 | ||
33 | // Now private, previously public | |
34 | if (isVideoInPrivateDirectory(video.privacy) && isVideoInPublicDirectory(oldPrivacy)) { | |
35 | await moveFiles({ type: 'public-to-private', video }) | |
36 | ||
37 | return true | |
38 | } | |
39 | ||
40 | return false | |
41 | } | |
42 | ||
43 | export { | |
44 | setVideoPrivacy, | |
45 | ||
46 | isVideoInPrivateDirectory, | |
47 | isVideoInPublicDirectory, | |
48 | ||
49 | moveFilesIfPrivacyChanged | |
50 | } | |
51 | ||
52 | // --------------------------------------------------------------------------- | |
53 | ||
9ab330b9 C |
54 | type MoveType = 'private-to-public' | 'public-to-private' |
55 | ||
3545e72c | 56 | async function moveFiles (options: { |
9ab330b9 | 57 | type: MoveType |
3545e72c C |
58 | video: MVideoFullLight |
59 | }) { | |
60 | const { type, video } = options | |
61 | ||
9ab330b9 C |
62 | for (const file of video.VideoFiles) { |
63 | if (file.storage === VideoStorage.FILE_SYSTEM) { | |
64 | await moveWebTorrentFileOnFS(type, video, file) | |
65 | } else { | |
66 | await updateWebTorrentFileACL(video, file) | |
3545e72c | 67 | } |
9ab330b9 C |
68 | } |
69 | ||
70 | const hls = video.getHLSPlaylist() | |
71 | ||
72 | if (hls) { | |
73 | if (hls.storage === VideoStorage.FILE_SYSTEM) { | |
74 | await moveHLSFilesOnFS(type, video) | |
75 | } else { | |
76 | await updateHLSFilesACL(hls) | |
3545e72c | 77 | } |
9ab330b9 C |
78 | } |
79 | } | |
3545e72c | 80 | |
9ab330b9 C |
81 | async function moveWebTorrentFileOnFS (type: MoveType, video: MVideo, file: MVideoFile) { |
82 | const directories = getWebTorrentDirectories(type) | |
3545e72c | 83 | |
9ab330b9 C |
84 | const source = join(directories.old, file.filename) |
85 | const destination = join(directories.new, file.filename) | |
3545e72c | 86 | |
9ab330b9 C |
87 | try { |
88 | logger.info('Moving WebTorrent files of %s after privacy change (%s -> %s).', video.uuid, source, destination) | |
89 | ||
90 | await move(source, destination) | |
91 | } catch (err) { | |
92 | logger.error('Cannot move webtorrent file %s to %s after privacy change', source, destination, { err }) | |
93 | } | |
94 | } | |
95 | ||
96 | function getWebTorrentDirectories (moveType: MoveType) { | |
97 | if (moveType === 'private-to-public') { | |
98 | return { old: DIRECTORIES.VIDEOS.PRIVATE, new: DIRECTORIES.VIDEOS.PUBLIC } | |
3545e72c C |
99 | } |
100 | ||
9ab330b9 C |
101 | return { old: DIRECTORIES.VIDEOS.PUBLIC, new: DIRECTORIES.VIDEOS.PRIVATE } |
102 | } | |
3545e72c | 103 | |
9ab330b9 | 104 | // --------------------------------------------------------------------------- |
3545e72c | 105 | |
9ab330b9 C |
106 | async function moveHLSFilesOnFS (type: MoveType, video: MVideo) { |
107 | const directories = getHLSDirectories(type) | |
3545e72c | 108 | |
9ab330b9 C |
109 | const source = join(directories.old, video.uuid) |
110 | const destination = join(directories.new, video.uuid) | |
111 | ||
112 | try { | |
113 | logger.info('Moving HLS files of %s after privacy change (%s -> %s).', video.uuid, source, destination) | |
114 | ||
115 | await move(source, destination) | |
116 | } catch (err) { | |
117 | logger.error('Cannot move HLS file %s to %s after privacy change', source, destination, { err }) | |
118 | } | |
119 | } | |
120 | ||
121 | function getHLSDirectories (moveType: MoveType) { | |
122 | if (moveType === 'private-to-public') { | |
123 | return { old: DIRECTORIES.HLS_STREAMING_PLAYLIST.PRIVATE, new: DIRECTORIES.HLS_STREAMING_PLAYLIST.PUBLIC } | |
3545e72c | 124 | } |
9ab330b9 C |
125 | |
126 | return { old: DIRECTORIES.HLS_STREAMING_PLAYLIST.PUBLIC, new: DIRECTORIES.HLS_STREAMING_PLAYLIST.PRIVATE } | |
3545e72c | 127 | } |