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