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