]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/object-storage/videos.ts
Use private ACL for private videos in s3
[github/Chocobozzz/PeerTube.git] / server / lib / object-storage / videos.ts
1 import { basename, join } from 'path'
2 import { logger } from '@server/helpers/logger'
3 import { CONFIG } from '@server/initializers/config'
4 import { MStreamingPlaylistVideo, MVideo, MVideoFile } from '@server/types/models'
5 import { getHLSDirectory } from '../paths'
6 import { VideoPathManager } from '../video-path-manager'
7 import { generateHLSObjectBaseStorageKey, generateHLSObjectStorageKey, generateWebTorrentObjectStorageKey } from './keys'
8 import {
9 createObjectReadStream,
10 listKeysOfPrefix,
11 lTags,
12 makeAvailable,
13 removeObject,
14 removePrefix,
15 storeObject,
16 updateObjectACL,
17 updatePrefixACL
18 } from './shared'
19
20 function listHLSFileKeysOf (playlist: MStreamingPlaylistVideo) {
21 return listKeysOfPrefix(generateHLSObjectBaseStorageKey(playlist), CONFIG.OBJECT_STORAGE.STREAMING_PLAYLISTS)
22 }
23
24 // ---------------------------------------------------------------------------
25
26 function storeHLSFileFromFilename (playlist: MStreamingPlaylistVideo, filename: string) {
27 return storeObject({
28 inputPath: join(getHLSDirectory(playlist.Video), filename),
29 objectStorageKey: generateHLSObjectStorageKey(playlist, filename),
30 bucketInfo: CONFIG.OBJECT_STORAGE.STREAMING_PLAYLISTS,
31 isPrivate: playlist.Video.hasPrivateStaticPath()
32 })
33 }
34
35 function storeHLSFileFromPath (playlist: MStreamingPlaylistVideo, path: string) {
36 return storeObject({
37 inputPath: path,
38 objectStorageKey: generateHLSObjectStorageKey(playlist, basename(path)),
39 bucketInfo: CONFIG.OBJECT_STORAGE.STREAMING_PLAYLISTS,
40 isPrivate: playlist.Video.hasPrivateStaticPath()
41 })
42 }
43
44 // ---------------------------------------------------------------------------
45
46 function storeWebTorrentFile (video: MVideo, file: MVideoFile) {
47 return storeObject({
48 inputPath: VideoPathManager.Instance.getFSVideoFileOutputPath(video, file),
49 objectStorageKey: generateWebTorrentObjectStorageKey(file.filename),
50 bucketInfo: CONFIG.OBJECT_STORAGE.VIDEOS,
51 isPrivate: video.hasPrivateStaticPath()
52 })
53 }
54
55 // ---------------------------------------------------------------------------
56
57 function updateWebTorrentFileACL (video: MVideo, file: MVideoFile) {
58 return updateObjectACL({
59 objectStorageKey: generateWebTorrentObjectStorageKey(file.filename),
60 bucketInfo: CONFIG.OBJECT_STORAGE.VIDEOS,
61 isPrivate: video.hasPrivateStaticPath()
62 })
63 }
64
65 function updateHLSFilesACL (playlist: MStreamingPlaylistVideo) {
66 return updatePrefixACL({
67 prefix: generateHLSObjectBaseStorageKey(playlist),
68 bucketInfo: CONFIG.OBJECT_STORAGE.STREAMING_PLAYLISTS,
69 isPrivate: playlist.Video.hasPrivateStaticPath()
70 })
71 }
72
73 // ---------------------------------------------------------------------------
74
75 function removeHLSObjectStorage (playlist: MStreamingPlaylistVideo) {
76 return removePrefix(generateHLSObjectBaseStorageKey(playlist), CONFIG.OBJECT_STORAGE.STREAMING_PLAYLISTS)
77 }
78
79 function removeHLSFileObjectStorage (playlist: MStreamingPlaylistVideo, filename: string) {
80 return removeObject(generateHLSObjectStorageKey(playlist, filename), CONFIG.OBJECT_STORAGE.STREAMING_PLAYLISTS)
81 }
82
83 // ---------------------------------------------------------------------------
84
85 function removeWebTorrentObjectStorage (videoFile: MVideoFile) {
86 return removeObject(generateWebTorrentObjectStorageKey(videoFile.filename), CONFIG.OBJECT_STORAGE.VIDEOS)
87 }
88
89 // ---------------------------------------------------------------------------
90
91 async function makeHLSFileAvailable (playlist: MStreamingPlaylistVideo, filename: string, destination: string) {
92 const key = generateHLSObjectStorageKey(playlist, filename)
93
94 logger.info('Fetching HLS file %s from object storage to %s.', key, destination, lTags())
95
96 await makeAvailable({
97 key,
98 destination,
99 bucketInfo: CONFIG.OBJECT_STORAGE.STREAMING_PLAYLISTS
100 })
101
102 return destination
103 }
104
105 async function makeWebTorrentFileAvailable (filename: string, destination: string) {
106 const key = generateWebTorrentObjectStorageKey(filename)
107
108 logger.info('Fetching WebTorrent file %s from object storage to %s.', key, destination, lTags())
109
110 await makeAvailable({
111 key,
112 destination,
113 bucketInfo: CONFIG.OBJECT_STORAGE.VIDEOS
114 })
115
116 return destination
117 }
118
119 // ---------------------------------------------------------------------------
120
121 function getWebTorrentFileReadStream (options: {
122 filename: string
123 rangeHeader: string
124 }) {
125 const { filename, rangeHeader } = options
126
127 const key = generateWebTorrentObjectStorageKey(filename)
128
129 return createObjectReadStream({
130 key,
131 bucketInfo: CONFIG.OBJECT_STORAGE.VIDEOS,
132 rangeHeader
133 })
134 }
135
136 function getHLSFileReadStream (options: {
137 playlist: MStreamingPlaylistVideo
138 filename: string
139 rangeHeader: string
140 }) {
141 const { playlist, filename, rangeHeader } = options
142
143 const key = generateHLSObjectStorageKey(playlist, filename)
144
145 return createObjectReadStream({
146 key,
147 bucketInfo: CONFIG.OBJECT_STORAGE.STREAMING_PLAYLISTS,
148 rangeHeader
149 })
150 }
151
152 // ---------------------------------------------------------------------------
153
154 export {
155 listHLSFileKeysOf,
156
157 storeWebTorrentFile,
158 storeHLSFileFromFilename,
159 storeHLSFileFromPath,
160
161 updateWebTorrentFileACL,
162 updateHLSFilesACL,
163
164 removeHLSObjectStorage,
165 removeHLSFileObjectStorage,
166 removeWebTorrentObjectStorage,
167
168 makeWebTorrentFileAvailable,
169 makeHLSFileAvailable,
170
171 getWebTorrentFileReadStream,
172 getHLSFileReadStream
173 }