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