]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/object-storage/videos.ts
Fix peertube runner concurrency
[github/Chocobozzz/PeerTube.git] / server / lib / object-storage / videos.ts
CommitLineData
cfd57d2c 1import { basename, join } from 'path'
0305db28
JB
2import { logger } from '@server/helpers/logger'
3import { CONFIG } from '@server/initializers/config'
3545e72c 4import { MStreamingPlaylistVideo, MVideo, MVideoFile } from '@server/types/models'
0305db28 5import { getHLSDirectory } from '../paths'
3545e72c 6import { VideoPathManager } from '../video-path-manager'
0305db28 7import { generateHLSObjectBaseStorageKey, generateHLSObjectStorageKey, generateWebTorrentObjectStorageKey } from './keys'
9ab330b9
C
8import {
9 createObjectReadStream,
10 listKeysOfPrefix,
11 lTags,
12 makeAvailable,
13 removeObject,
aa887096 14 removeObjectByFullKey,
9ab330b9
C
15 removePrefix,
16 storeObject,
17 updateObjectACL,
18 updatePrefixACL
19} from './shared'
0305db28 20
cfd57d2c
C
21function listHLSFileKeysOf (playlist: MStreamingPlaylistVideo) {
22 return listKeysOfPrefix(generateHLSObjectBaseStorageKey(playlist), CONFIG.OBJECT_STORAGE.STREAMING_PLAYLISTS)
23}
24
25// ---------------------------------------------------------------------------
26
27function storeHLSFileFromFilename (playlist: MStreamingPlaylistVideo, filename: string) {
0305db28 28 return storeObject({
cfd57d2c 29 inputPath: join(getHLSDirectory(playlist.Video), filename),
ad5db104 30 objectStorageKey: generateHLSObjectStorageKey(playlist, filename),
9ab330b9
C
31 bucketInfo: CONFIG.OBJECT_STORAGE.STREAMING_PLAYLISTS,
32 isPrivate: playlist.Video.hasPrivateStaticPath()
0305db28
JB
33 })
34}
35
cfd57d2c
C
36function storeHLSFileFromPath (playlist: MStreamingPlaylistVideo, path: string) {
37 return storeObject({
38 inputPath: path,
39 objectStorageKey: generateHLSObjectStorageKey(playlist, basename(path)),
9ab330b9
C
40 bucketInfo: CONFIG.OBJECT_STORAGE.STREAMING_PLAYLISTS,
41 isPrivate: playlist.Video.hasPrivateStaticPath()
cfd57d2c
C
42 })
43}
44
34023e12
C
45function 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
cfd57d2c
C
54// ---------------------------------------------------------------------------
55
3545e72c 56function storeWebTorrentFile (video: MVideo, file: MVideoFile) {
0305db28 57 return storeObject({
3545e72c
C
58 inputPath: VideoPathManager.Instance.getFSVideoFileOutputPath(video, file),
59 objectStorageKey: generateWebTorrentObjectStorageKey(file.filename),
9ab330b9
C
60 bucketInfo: CONFIG.OBJECT_STORAGE.VIDEOS,
61 isPrivate: video.hasPrivateStaticPath()
62 })
63}
64
65// ---------------------------------------------------------------------------
66
8180f604
C
67async function updateWebTorrentFileACL (video: MVideo, file: MVideoFile) {
68 await updateObjectACL({
9ab330b9
C
69 objectStorageKey: generateWebTorrentObjectStorageKey(file.filename),
70 bucketInfo: CONFIG.OBJECT_STORAGE.VIDEOS,
71 isPrivate: video.hasPrivateStaticPath()
72 })
73}
74
8180f604
C
75async function updateHLSFilesACL (playlist: MStreamingPlaylistVideo) {
76 await updatePrefixACL({
9ab330b9
C
77 prefix: generateHLSObjectBaseStorageKey(playlist),
78 bucketInfo: CONFIG.OBJECT_STORAGE.STREAMING_PLAYLISTS,
79 isPrivate: playlist.Video.hasPrivateStaticPath()
0305db28
JB
80 })
81}
82
cfd57d2c
C
83// ---------------------------------------------------------------------------
84
ad5db104
C
85function removeHLSObjectStorage (playlist: MStreamingPlaylistVideo) {
86 return removePrefix(generateHLSObjectBaseStorageKey(playlist), CONFIG.OBJECT_STORAGE.STREAMING_PLAYLISTS)
0305db28
JB
87}
88
aa887096 89function removeHLSFileObjectStorageByFilename (playlist: MStreamingPlaylistVideo, filename: string) {
7b6b445d
C
90 return removeObject(generateHLSObjectStorageKey(playlist, filename), CONFIG.OBJECT_STORAGE.STREAMING_PLAYLISTS)
91}
92
aa887096
C
93function removeHLSFileObjectStorageByPath (playlist: MStreamingPlaylistVideo, path: string) {
94 return removeObject(generateHLSObjectStorageKey(playlist, basename(path)), CONFIG.OBJECT_STORAGE.STREAMING_PLAYLISTS)
95}
96
97function removeHLSFileObjectStorageByFullKey (key: string) {
98 return removeObjectByFullKey(key, CONFIG.OBJECT_STORAGE.STREAMING_PLAYLISTS)
99}
100
cfd57d2c
C
101// ---------------------------------------------------------------------------
102
0305db28
JB
103function removeWebTorrentObjectStorage (videoFile: MVideoFile) {
104 return removeObject(generateWebTorrentObjectStorageKey(videoFile.filename), CONFIG.OBJECT_STORAGE.VIDEOS)
105}
106
cfd57d2c
C
107// ---------------------------------------------------------------------------
108
ad5db104
C
109async function makeHLSFileAvailable (playlist: MStreamingPlaylistVideo, filename: string, destination: string) {
110 const key = generateHLSObjectStorageKey(playlist, filename)
0305db28
JB
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
123async 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
cfd57d2c
C
137// ---------------------------------------------------------------------------
138
9ab330b9
C
139function 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
154function 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
0305db28 172export {
cfd57d2c
C
173 listHLSFileKeysOf,
174
0305db28 175 storeWebTorrentFile,
cfd57d2c
C
176 storeHLSFileFromFilename,
177 storeHLSFileFromPath,
34023e12 178 storeHLSFileFromContent,
0305db28 179
9ab330b9
C
180 updateWebTorrentFileACL,
181 updateHLSFilesACL,
182
0305db28 183 removeHLSObjectStorage,
aa887096
C
184 removeHLSFileObjectStorageByFilename,
185 removeHLSFileObjectStorageByPath,
186 removeHLSFileObjectStorageByFullKey,
187
0305db28
JB
188 removeWebTorrentObjectStorage,
189
190 makeWebTorrentFileAvailable,
9ab330b9
C
191 makeHLSFileAvailable,
192
193 getWebTorrentFileReadStream,
194 getHLSFileReadStream
0305db28 195}