aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/video-privacy.ts
diff options
context:
space:
mode:
Diffstat (limited to 'server/lib/video-privacy.ts')
-rw-r--r--server/lib/video-privacy.ts96
1 files changed, 96 insertions, 0 deletions
diff --git a/server/lib/video-privacy.ts b/server/lib/video-privacy.ts
new file mode 100644
index 000000000..1a4a5a22d
--- /dev/null
+++ b/server/lib/video-privacy.ts
@@ -0,0 +1,96 @@
1import { move } from 'fs-extra'
2import { join } from 'path'
3import { logger } from '@server/helpers/logger'
4import { DIRECTORIES } from '@server/initializers/constants'
5import { MVideo, MVideoFullLight } from '@server/types/models'
6import { VideoPrivacy } from '@shared/models'
7
8function setVideoPrivacy (video: MVideo, newPrivacy: VideoPrivacy) {
9 if (video.privacy === VideoPrivacy.PRIVATE && newPrivacy !== VideoPrivacy.PRIVATE) {
10 video.publishedAt = new Date()
11 }
12
13 video.privacy = newPrivacy
14}
15
16function isVideoInPrivateDirectory (privacy: VideoPrivacy) {
17 return privacy === VideoPrivacy.PRIVATE || privacy === VideoPrivacy.INTERNAL
18}
19
20function isVideoInPublicDirectory (privacy: VideoPrivacy) {
21 return !isVideoInPrivateDirectory(privacy)
22}
23
24async function moveFilesIfPrivacyChanged (video: MVideoFullLight, oldPrivacy: VideoPrivacy) {
25 // Now public, previously private
26 if (isVideoInPublicDirectory(video.privacy) && isVideoInPrivateDirectory(oldPrivacy)) {
27 await moveFiles({ type: 'private-to-public', video })
28
29 return true
30 }
31
32 // Now private, previously public
33 if (isVideoInPrivateDirectory(video.privacy) && isVideoInPublicDirectory(oldPrivacy)) {
34 await moveFiles({ type: 'public-to-private', video })
35
36 return true
37 }
38
39 return false
40}
41
42export {
43 setVideoPrivacy,
44
45 isVideoInPrivateDirectory,
46 isVideoInPublicDirectory,
47
48 moveFilesIfPrivacyChanged
49}
50
51// ---------------------------------------------------------------------------
52
53async function moveFiles (options: {
54 type: 'private-to-public' | 'public-to-private'
55 video: MVideoFullLight
56}) {
57 const { type, video } = options
58
59 const directories = type === 'private-to-public'
60 ? {
61 webtorrent: { old: DIRECTORIES.VIDEOS.PRIVATE, new: DIRECTORIES.VIDEOS.PUBLIC },
62 hls: { old: DIRECTORIES.HLS_STREAMING_PLAYLIST.PRIVATE, new: DIRECTORIES.HLS_STREAMING_PLAYLIST.PUBLIC }
63 }
64 : {
65 webtorrent: { old: DIRECTORIES.VIDEOS.PUBLIC, new: DIRECTORIES.VIDEOS.PRIVATE },
66 hls: { old: DIRECTORIES.HLS_STREAMING_PLAYLIST.PUBLIC, new: DIRECTORIES.HLS_STREAMING_PLAYLIST.PRIVATE }
67 }
68
69 for (const file of video.VideoFiles) {
70 const source = join(directories.webtorrent.old, file.filename)
71 const destination = join(directories.webtorrent.new, file.filename)
72
73 try {
74 logger.info('Moving WebTorrent files of %s after privacy change (%s -> %s).', video.uuid, source, destination)
75
76 await move(source, destination)
77 } catch (err) {
78 logger.error('Cannot move webtorrent file %s to %s after privacy change', source, destination, { err })
79 }
80 }
81
82 const hls = video.getHLSPlaylist()
83
84 if (hls) {
85 const source = join(directories.hls.old, video.uuid)
86 const destination = join(directories.hls.new, video.uuid)
87
88 try {
89 logger.info('Moving HLS files of %s after privacy change (%s -> %s).', video.uuid, source, destination)
90
91 await move(source, destination)
92 } catch (err) {
93 logger.error('Cannot move HLS file %s to %s after privacy change', source, destination, { err })
94 }
95 }
96}