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'
9 function setVideoPrivacy (video: MVideo, newPrivacy: VideoPrivacy) {
10 if (video.privacy === VideoPrivacy.PRIVATE && newPrivacy !== VideoPrivacy.PRIVATE) {
11 video.publishedAt = new Date()
14 video.privacy = newPrivacy
17 function isVideoInPrivateDirectory (privacy: VideoPrivacy) {
18 return privacy === VideoPrivacy.PRIVATE || privacy === VideoPrivacy.INTERNAL
21 function isVideoInPublicDirectory (privacy: VideoPrivacy) {
22 return !isVideoInPrivateDirectory(privacy)
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 })
33 // Now private, previously public
34 if (isVideoInPrivateDirectory(video.privacy) && isVideoInPublicDirectory(oldPrivacy)) {
35 await moveFiles({ type: 'public-to-private', video })
46 isVideoInPrivateDirectory,
47 isVideoInPublicDirectory,
49 moveFilesIfPrivacyChanged
52 // ---------------------------------------------------------------------------
54 type MoveType = 'private-to-public' | 'public-to-private'
56 async function moveFiles (options: {
58 video: MVideoFullLight
60 const { type, video } = options
62 for (const file of video.VideoFiles) {
63 if (file.storage === VideoStorage.FILE_SYSTEM) {
64 await moveWebTorrentFileOnFS(type, video, file)
66 await updateWebTorrentFileACL(video, file)
70 const hls = video.getHLSPlaylist()
73 if (hls.storage === VideoStorage.FILE_SYSTEM) {
74 await moveHLSFilesOnFS(type, video)
76 await updateHLSFilesACL(hls)
81 async function moveWebTorrentFileOnFS (type: MoveType, video: MVideo, file: MVideoFile) {
82 const directories = getWebTorrentDirectories(type)
84 const source = join(directories.old, file.filename)
85 const destination = join(directories.new, file.filename)
88 logger.info('Moving WebTorrent files of %s after privacy change (%s -> %s).', video.uuid, source, destination)
90 await move(source, destination)
92 logger.error('Cannot move webtorrent file %s to %s after privacy change', source, destination, { err })
96 function getWebTorrentDirectories (moveType: MoveType) {
97 if (moveType === 'private-to-public') {
98 return { old: DIRECTORIES.VIDEOS.PRIVATE, new: DIRECTORIES.VIDEOS.PUBLIC }
101 return { old: DIRECTORIES.VIDEOS.PUBLIC, new: DIRECTORIES.VIDEOS.PRIVATE }
104 // ---------------------------------------------------------------------------
106 async function moveHLSFilesOnFS (type: MoveType, video: MVideo) {
107 const directories = getHLSDirectories(type)
109 const source = join(directories.old, video.uuid)
110 const destination = join(directories.new, video.uuid)
113 logger.info('Moving HLS files of %s after privacy change (%s -> %s).', video.uuid, source, destination)
115 await move(source, destination)
117 logger.error('Cannot move HLS file %s to %s after privacy change', source, destination, { err })
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 }
126 return { old: DIRECTORIES.HLS_STREAMING_PLAYLIST.PUBLIC, new: DIRECTORIES.HLS_STREAMING_PLAYLIST.PRIVATE }