2023-07-31 07:34:36 -05:00
|
|
|
import { move } from 'fs-extra/esm'
|
2022-10-12 09:09:02 -05:00
|
|
|
import { join } from 'path'
|
2024-02-12 03:47:52 -06:00
|
|
|
import { VideoPrivacy, VideoPrivacyType, FileStorage } from '@peertube/peertube-models'
|
2023-07-31 07:34:36 -05:00
|
|
|
import { logger } from '@server/helpers/logger.js'
|
|
|
|
import { DIRECTORIES } from '@server/initializers/constants.js'
|
|
|
|
import { MVideo, MVideoFile, MVideoFullLight } from '@server/types/models/index.js'
|
|
|
|
import { updateHLSFilesACL, updateWebVideoFileACL } from './object-storage/index.js'
|
2022-10-12 09:09:02 -05:00
|
|
|
|
2023-10-20 09:05:38 -05:00
|
|
|
const validPrivacySet = new Set<VideoPrivacyType>([
|
2023-06-29 02:48:55 -05:00
|
|
|
VideoPrivacy.PRIVATE,
|
|
|
|
VideoPrivacy.INTERNAL,
|
|
|
|
VideoPrivacy.PASSWORD_PROTECTED
|
|
|
|
])
|
|
|
|
|
2023-07-31 07:34:36 -05:00
|
|
|
function setVideoPrivacy (video: MVideo, newPrivacy: VideoPrivacyType) {
|
2022-10-12 09:09:02 -05:00
|
|
|
if (video.privacy === VideoPrivacy.PRIVATE && newPrivacy !== VideoPrivacy.PRIVATE) {
|
|
|
|
video.publishedAt = new Date()
|
|
|
|
}
|
|
|
|
|
|
|
|
video.privacy = newPrivacy
|
|
|
|
}
|
|
|
|
|
2023-10-20 09:05:38 -05:00
|
|
|
function isVideoInPrivateDirectory (privacy: VideoPrivacyType) {
|
2023-06-29 02:48:55 -05:00
|
|
|
return validPrivacySet.has(privacy)
|
2022-10-12 09:09:02 -05:00
|
|
|
}
|
|
|
|
|
2023-07-31 07:34:36 -05:00
|
|
|
function isVideoInPublicDirectory (privacy: VideoPrivacyType) {
|
2022-10-12 09:09:02 -05:00
|
|
|
return !isVideoInPrivateDirectory(privacy)
|
|
|
|
}
|
|
|
|
|
2023-07-31 07:34:36 -05:00
|
|
|
async function moveFilesIfPrivacyChanged (video: MVideoFullLight, oldPrivacy: VideoPrivacyType) {
|
2022-10-12 09:09:02 -05:00
|
|
|
// Now public, previously private
|
|
|
|
if (isVideoInPublicDirectory(video.privacy) && isVideoInPrivateDirectory(oldPrivacy)) {
|
|
|
|
await moveFiles({ type: 'private-to-public', video })
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now private, previously public
|
|
|
|
if (isVideoInPrivateDirectory(video.privacy) && isVideoInPublicDirectory(oldPrivacy)) {
|
|
|
|
await moveFiles({ type: 'public-to-private', video })
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
export {
|
|
|
|
setVideoPrivacy,
|
|
|
|
|
|
|
|
isVideoInPrivateDirectory,
|
|
|
|
isVideoInPublicDirectory,
|
|
|
|
|
|
|
|
moveFilesIfPrivacyChanged
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2022-10-19 03:43:53 -05:00
|
|
|
type MoveType = 'private-to-public' | 'public-to-private'
|
|
|
|
|
2022-10-12 09:09:02 -05:00
|
|
|
async function moveFiles (options: {
|
2022-10-19 03:43:53 -05:00
|
|
|
type: MoveType
|
2022-10-12 09:09:02 -05:00
|
|
|
video: MVideoFullLight
|
|
|
|
}) {
|
|
|
|
const { type, video } = options
|
|
|
|
|
2022-10-19 03:43:53 -05:00
|
|
|
for (const file of video.VideoFiles) {
|
2024-02-12 03:47:52 -06:00
|
|
|
if (file.storage === FileStorage.FILE_SYSTEM) {
|
2023-07-11 02:21:13 -05:00
|
|
|
await moveWebVideoFileOnFS(type, video, file)
|
2022-10-19 03:43:53 -05:00
|
|
|
} else {
|
2023-07-11 02:21:13 -05:00
|
|
|
await updateWebVideoFileACL(video, file)
|
2022-10-12 09:09:02 -05:00
|
|
|
}
|
2022-10-19 03:43:53 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
const hls = video.getHLSPlaylist()
|
|
|
|
|
|
|
|
if (hls) {
|
2024-02-12 03:47:52 -06:00
|
|
|
if (hls.storage === FileStorage.FILE_SYSTEM) {
|
2022-10-19 03:43:53 -05:00
|
|
|
await moveHLSFilesOnFS(type, video)
|
|
|
|
} else {
|
|
|
|
await updateHLSFilesACL(hls)
|
2022-10-12 09:09:02 -05:00
|
|
|
}
|
2022-10-19 03:43:53 -05:00
|
|
|
}
|
|
|
|
}
|
2022-10-12 09:09:02 -05:00
|
|
|
|
2023-07-11 02:21:13 -05:00
|
|
|
async function moveWebVideoFileOnFS (type: MoveType, video: MVideo, file: MVideoFile) {
|
|
|
|
const directories = getWebVideoDirectories(type)
|
2022-10-12 09:09:02 -05:00
|
|
|
|
2022-10-19 03:43:53 -05:00
|
|
|
const source = join(directories.old, file.filename)
|
|
|
|
const destination = join(directories.new, file.filename)
|
2022-10-12 09:09:02 -05:00
|
|
|
|
2022-10-19 03:43:53 -05:00
|
|
|
try {
|
2023-07-11 02:21:13 -05:00
|
|
|
logger.info('Moving web video files of %s after privacy change (%s -> %s).', video.uuid, source, destination)
|
2022-10-19 03:43:53 -05:00
|
|
|
|
|
|
|
await move(source, destination)
|
|
|
|
} catch (err) {
|
2023-07-11 02:21:13 -05:00
|
|
|
logger.error('Cannot move web video file %s to %s after privacy change', source, destination, { err })
|
2022-10-19 03:43:53 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-11 02:21:13 -05:00
|
|
|
function getWebVideoDirectories (moveType: MoveType) {
|
2022-10-19 03:43:53 -05:00
|
|
|
if (moveType === 'private-to-public') {
|
2024-03-15 09:47:18 -05:00
|
|
|
return { old: DIRECTORIES.WEB_VIDEOS.PRIVATE, new: DIRECTORIES.WEB_VIDEOS.PUBLIC }
|
2022-10-12 09:09:02 -05:00
|
|
|
}
|
|
|
|
|
2024-03-15 09:47:18 -05:00
|
|
|
return { old: DIRECTORIES.WEB_VIDEOS.PUBLIC, new: DIRECTORIES.WEB_VIDEOS.PRIVATE }
|
2022-10-19 03:43:53 -05:00
|
|
|
}
|
2022-10-12 09:09:02 -05:00
|
|
|
|
2022-10-19 03:43:53 -05:00
|
|
|
// ---------------------------------------------------------------------------
|
2022-10-12 09:09:02 -05:00
|
|
|
|
2022-10-19 03:43:53 -05:00
|
|
|
async function moveHLSFilesOnFS (type: MoveType, video: MVideo) {
|
|
|
|
const directories = getHLSDirectories(type)
|
2022-10-12 09:09:02 -05:00
|
|
|
|
2022-10-19 03:43:53 -05:00
|
|
|
const source = join(directories.old, video.uuid)
|
|
|
|
const destination = join(directories.new, video.uuid)
|
|
|
|
|
|
|
|
try {
|
|
|
|
logger.info('Moving HLS files of %s after privacy change (%s -> %s).', video.uuid, source, destination)
|
|
|
|
|
|
|
|
await move(source, destination)
|
|
|
|
} catch (err) {
|
|
|
|
logger.error('Cannot move HLS file %s to %s after privacy change', source, destination, { err })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function getHLSDirectories (moveType: MoveType) {
|
|
|
|
if (moveType === 'private-to-public') {
|
|
|
|
return { old: DIRECTORIES.HLS_STREAMING_PLAYLIST.PRIVATE, new: DIRECTORIES.HLS_STREAMING_PLAYLIST.PUBLIC }
|
2022-10-12 09:09:02 -05:00
|
|
|
}
|
2022-10-19 03:43:53 -05:00
|
|
|
|
|
|
|
return { old: DIRECTORIES.HLS_STREAMING_PLAYLIST.PUBLIC, new: DIRECTORIES.HLS_STREAMING_PLAYLIST.PRIVATE }
|
2022-10-12 09:09:02 -05:00
|
|
|
}
|