diff options
Diffstat (limited to 'server/lib/video-privacy.ts')
-rw-r--r-- | server/lib/video-privacy.ts | 89 |
1 files changed, 60 insertions, 29 deletions
diff --git a/server/lib/video-privacy.ts b/server/lib/video-privacy.ts index 1a4a5a22d..41f9d62b3 100644 --- a/server/lib/video-privacy.ts +++ b/server/lib/video-privacy.ts | |||
@@ -2,8 +2,9 @@ import { move } from 'fs-extra' | |||
2 | import { join } from 'path' | 2 | import { join } from 'path' |
3 | import { logger } from '@server/helpers/logger' | 3 | import { logger } from '@server/helpers/logger' |
4 | import { DIRECTORIES } from '@server/initializers/constants' | 4 | import { DIRECTORIES } from '@server/initializers/constants' |
5 | import { MVideo, MVideoFullLight } from '@server/types/models' | 5 | import { MVideo, MVideoFile, MVideoFullLight } from '@server/types/models' |
6 | import { VideoPrivacy } from '@shared/models' | 6 | import { VideoPrivacy, VideoStorage } from '@shared/models' |
7 | import { updateHLSFilesACL, updateWebTorrentFileACL } from './object-storage' | ||
7 | 8 | ||
8 | function setVideoPrivacy (video: MVideo, newPrivacy: VideoPrivacy) { | 9 | function setVideoPrivacy (video: MVideo, newPrivacy: VideoPrivacy) { |
9 | if (video.privacy === VideoPrivacy.PRIVATE && newPrivacy !== VideoPrivacy.PRIVATE) { | 10 | if (video.privacy === VideoPrivacy.PRIVATE && newPrivacy !== VideoPrivacy.PRIVATE) { |
@@ -50,47 +51,77 @@ export { | |||
50 | 51 | ||
51 | // --------------------------------------------------------------------------- | 52 | // --------------------------------------------------------------------------- |
52 | 53 | ||
54 | type MoveType = 'private-to-public' | 'public-to-private' | ||
55 | |||
53 | async function moveFiles (options: { | 56 | async function moveFiles (options: { |
54 | type: 'private-to-public' | 'public-to-private' | 57 | type: MoveType |
55 | video: MVideoFullLight | 58 | video: MVideoFullLight |
56 | }) { | 59 | }) { |
57 | const { type, video } = options | 60 | const { type, video } = options |
58 | 61 | ||
59 | const directories = type === 'private-to-public' | 62 | for (const file of video.VideoFiles) { |
60 | ? { | 63 | if (file.storage === VideoStorage.FILE_SYSTEM) { |
61 | webtorrent: { old: DIRECTORIES.VIDEOS.PRIVATE, new: DIRECTORIES.VIDEOS.PUBLIC }, | 64 | await moveWebTorrentFileOnFS(type, video, file) |
62 | hls: { old: DIRECTORIES.HLS_STREAMING_PLAYLIST.PRIVATE, new: DIRECTORIES.HLS_STREAMING_PLAYLIST.PUBLIC } | 65 | } else { |
66 | await updateWebTorrentFileACL(video, file) | ||
63 | } | 67 | } |
64 | : { | 68 | } |
65 | webtorrent: { old: DIRECTORIES.VIDEOS.PUBLIC, new: DIRECTORIES.VIDEOS.PRIVATE }, | 69 | |
66 | hls: { old: DIRECTORIES.HLS_STREAMING_PLAYLIST.PUBLIC, new: DIRECTORIES.HLS_STREAMING_PLAYLIST.PRIVATE } | 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) | ||
67 | } | 77 | } |
78 | } | ||
79 | } | ||
68 | 80 | ||
69 | for (const file of video.VideoFiles) { | 81 | async function moveWebTorrentFileOnFS (type: MoveType, video: MVideo, file: MVideoFile) { |
70 | const source = join(directories.webtorrent.old, file.filename) | 82 | const directories = getWebTorrentDirectories(type) |
71 | const destination = join(directories.webtorrent.new, file.filename) | ||
72 | 83 | ||
73 | try { | 84 | const source = join(directories.old, file.filename) |
74 | logger.info('Moving WebTorrent files of %s after privacy change (%s -> %s).', video.uuid, source, destination) | 85 | const destination = join(directories.new, file.filename) |
75 | 86 | ||
76 | await move(source, destination) | 87 | try { |
77 | } catch (err) { | 88 | logger.info('Moving WebTorrent files of %s after privacy change (%s -> %s).', video.uuid, source, destination) |
78 | logger.error('Cannot move webtorrent file %s to %s after privacy change', source, destination, { err }) | 89 | |
79 | } | 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 } | ||
80 | } | 99 | } |
81 | 100 | ||
82 | const hls = video.getHLSPlaylist() | 101 | return { old: DIRECTORIES.VIDEOS.PUBLIC, new: DIRECTORIES.VIDEOS.PRIVATE } |
102 | } | ||
83 | 103 | ||
84 | if (hls) { | 104 | // --------------------------------------------------------------------------- |
85 | const source = join(directories.hls.old, video.uuid) | ||
86 | const destination = join(directories.hls.new, video.uuid) | ||
87 | 105 | ||
88 | try { | 106 | async function moveHLSFilesOnFS (type: MoveType, video: MVideo) { |
89 | logger.info('Moving HLS files of %s after privacy change (%s -> %s).', video.uuid, source, destination) | 107 | const directories = getHLSDirectories(type) |
90 | 108 | ||
91 | await move(source, destination) | 109 | const source = join(directories.old, video.uuid) |
92 | } catch (err) { | 110 | const destination = join(directories.new, video.uuid) |
93 | logger.error('Cannot move HLS file %s to %s after privacy change', source, destination, { err }) | 111 | |
94 | } | 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 } | ||
95 | } | 124 | } |
125 | |||
126 | return { old: DIRECTORIES.HLS_STREAMING_PLAYLIST.PUBLIC, new: DIRECTORIES.HLS_STREAMING_PLAYLIST.PRIVATE } | ||
96 | } | 127 | } |