]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/video-privacy.ts
Bumped to version v5.2.1
[github/Chocobozzz/PeerTube.git] / server / lib / video-privacy.ts
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, updateWebTorrentFileACL } from './object-storage'
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
54 type MoveType = 'private-to-public' | 'public-to-private'
55
56 async function moveFiles (options: {
57 type: MoveType
58 video: MVideoFullLight
59 }) {
60 const { type, video } = options
61
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)
67 }
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)
77 }
78 }
79 }
80
81 async function moveWebTorrentFileOnFS (type: MoveType, video: MVideo, file: MVideoFile) {
82 const directories = getWebTorrentDirectories(type)
83
84 const source = join(directories.old, file.filename)
85 const destination = join(directories.new, file.filename)
86
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 }
99 }
100
101 return { old: DIRECTORIES.VIDEOS.PUBLIC, new: DIRECTORIES.VIDEOS.PRIVATE }
102 }
103
104 // ---------------------------------------------------------------------------
105
106 async function moveHLSFilesOnFS (type: MoveType, video: MVideo) {
107 const directories = getHLSDirectories(type)
108
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 }
124 }
125
126 return { old: DIRECTORIES.HLS_STREAMING_PLAYLIST.PUBLIC, new: DIRECTORIES.HLS_STREAMING_PLAYLIST.PRIVATE }
127 }