diff options
Diffstat (limited to 'server/lib/object-storage')
-rw-r--r-- | server/lib/object-storage/shared/object-storage-helpers.ts | 223 | ||||
-rw-r--r-- | server/lib/object-storage/urls.ts | 29 | ||||
-rw-r--r-- | server/lib/object-storage/videos.ts | 138 |
3 files changed, 325 insertions, 65 deletions
diff --git a/server/lib/object-storage/shared/object-storage-helpers.ts b/server/lib/object-storage/shared/object-storage-helpers.ts index 16161362c..3046d76bc 100644 --- a/server/lib/object-storage/shared/object-storage-helpers.ts +++ b/server/lib/object-storage/shared/object-storage-helpers.ts | |||
@@ -1,19 +1,23 @@ | |||
1 | import { map } from 'bluebird' | ||
1 | import { createReadStream, createWriteStream, ensureDir, ReadStream } from 'fs-extra' | 2 | import { createReadStream, createWriteStream, ensureDir, ReadStream } from 'fs-extra' |
2 | import { dirname } from 'path' | 3 | import { dirname } from 'path' |
3 | import { Readable } from 'stream' | 4 | import { Readable } from 'stream' |
4 | import { | 5 | import { |
6 | _Object, | ||
5 | CompleteMultipartUploadCommandOutput, | 7 | CompleteMultipartUploadCommandOutput, |
6 | DeleteObjectCommand, | 8 | DeleteObjectCommand, |
7 | GetObjectCommand, | 9 | GetObjectCommand, |
8 | ListObjectsV2Command, | 10 | ListObjectsV2Command, |
9 | PutObjectCommandInput | 11 | PutObjectAclCommand, |
12 | PutObjectCommandInput, | ||
13 | S3Client | ||
10 | } from '@aws-sdk/client-s3' | 14 | } from '@aws-sdk/client-s3' |
11 | import { Upload } from '@aws-sdk/lib-storage' | 15 | import { Upload } from '@aws-sdk/lib-storage' |
12 | import { pipelinePromise } from '@server/helpers/core-utils' | 16 | import { pipelinePromise } from '@server/helpers/core-utils' |
13 | import { isArray } from '@server/helpers/custom-validators/misc' | 17 | import { isArray } from '@server/helpers/custom-validators/misc' |
14 | import { logger } from '@server/helpers/logger' | 18 | import { logger } from '@server/helpers/logger' |
15 | import { CONFIG } from '@server/initializers/config' | 19 | import { CONFIG } from '@server/initializers/config' |
16 | import { getPrivateUrl } from '../urls' | 20 | import { getInternalUrl } from '../urls' |
17 | import { getClient } from './client' | 21 | import { getClient } from './client' |
18 | import { lTags } from './logger' | 22 | import { lTags } from './logger' |
19 | 23 | ||
@@ -22,73 +26,125 @@ type BucketInfo = { | |||
22 | PREFIX?: string | 26 | PREFIX?: string |
23 | } | 27 | } |
24 | 28 | ||
29 | async function listKeysOfPrefix (prefix: string, bucketInfo: BucketInfo) { | ||
30 | const s3Client = getClient() | ||
31 | |||
32 | const commandPrefix = bucketInfo.PREFIX + prefix | ||
33 | const listCommand = new ListObjectsV2Command({ | ||
34 | Bucket: bucketInfo.BUCKET_NAME, | ||
35 | Prefix: commandPrefix | ||
36 | }) | ||
37 | |||
38 | const listedObjects = await s3Client.send(listCommand) | ||
39 | |||
40 | if (isArray(listedObjects.Contents) !== true) return [] | ||
41 | |||
42 | return listedObjects.Contents.map(c => c.Key) | ||
43 | } | ||
44 | |||
45 | // --------------------------------------------------------------------------- | ||
46 | |||
25 | async function storeObject (options: { | 47 | async function storeObject (options: { |
26 | inputPath: string | 48 | inputPath: string |
27 | objectStorageKey: string | 49 | objectStorageKey: string |
28 | bucketInfo: BucketInfo | 50 | bucketInfo: BucketInfo |
51 | isPrivate: boolean | ||
29 | }): Promise<string> { | 52 | }): Promise<string> { |
30 | const { inputPath, objectStorageKey, bucketInfo } = options | 53 | const { inputPath, objectStorageKey, bucketInfo, isPrivate } = options |
31 | 54 | ||
32 | logger.debug('Uploading file %s to %s%s in bucket %s', inputPath, bucketInfo.PREFIX, objectStorageKey, bucketInfo.BUCKET_NAME, lTags()) | 55 | logger.debug('Uploading file %s to %s%s in bucket %s', inputPath, bucketInfo.PREFIX, objectStorageKey, bucketInfo.BUCKET_NAME, lTags()) |
33 | 56 | ||
34 | const fileStream = createReadStream(inputPath) | 57 | const fileStream = createReadStream(inputPath) |
35 | 58 | ||
36 | return uploadToStorage({ objectStorageKey, content: fileStream, bucketInfo }) | 59 | return uploadToStorage({ objectStorageKey, content: fileStream, bucketInfo, isPrivate }) |
37 | } | 60 | } |
38 | 61 | ||
39 | async function removeObject (filename: string, bucketInfo: BucketInfo) { | 62 | // --------------------------------------------------------------------------- |
40 | const command = new DeleteObjectCommand({ | 63 | |
64 | function updateObjectACL (options: { | ||
65 | objectStorageKey: string | ||
66 | bucketInfo: BucketInfo | ||
67 | isPrivate: boolean | ||
68 | }) { | ||
69 | const { objectStorageKey, bucketInfo, isPrivate } = options | ||
70 | |||
71 | const key = buildKey(objectStorageKey, bucketInfo) | ||
72 | |||
73 | logger.debug('Updating ACL file %s in bucket %s', key, bucketInfo.BUCKET_NAME, lTags()) | ||
74 | |||
75 | const command = new PutObjectAclCommand({ | ||
41 | Bucket: bucketInfo.BUCKET_NAME, | 76 | Bucket: bucketInfo.BUCKET_NAME, |
42 | Key: buildKey(filename, bucketInfo) | 77 | Key: key, |
78 | ACL: getACL(isPrivate) | ||
43 | }) | 79 | }) |
44 | 80 | ||
45 | return getClient().send(command) | 81 | return getClient().send(command) |
46 | } | 82 | } |
47 | 83 | ||
48 | async function removePrefix (prefix: string, bucketInfo: BucketInfo) { | 84 | function updatePrefixACL (options: { |
49 | const s3Client = getClient() | 85 | prefix: string |
50 | 86 | bucketInfo: BucketInfo | |
51 | const commandPrefix = bucketInfo.PREFIX + prefix | 87 | isPrivate: boolean |
52 | const listCommand = new ListObjectsV2Command({ | 88 | }) { |
53 | Bucket: bucketInfo.BUCKET_NAME, | 89 | const { prefix, bucketInfo, isPrivate } = options |
54 | Prefix: commandPrefix | 90 | |
91 | logger.debug('Updating ACL of files in prefix %s in bucket %s', prefix, bucketInfo.BUCKET_NAME, lTags()) | ||
92 | |||
93 | return applyOnPrefix({ | ||
94 | prefix, | ||
95 | bucketInfo, | ||
96 | commandBuilder: obj => { | ||
97 | logger.debug('Updating ACL of %s inside prefix %s in bucket %s', obj.Key, prefix, bucketInfo.BUCKET_NAME, lTags()) | ||
98 | |||
99 | return new PutObjectAclCommand({ | ||
100 | Bucket: bucketInfo.BUCKET_NAME, | ||
101 | Key: obj.Key, | ||
102 | ACL: getACL(isPrivate) | ||
103 | }) | ||
104 | } | ||
55 | }) | 105 | }) |
106 | } | ||
56 | 107 | ||
57 | const listedObjects = await s3Client.send(listCommand) | 108 | // --------------------------------------------------------------------------- |
58 | |||
59 | // FIXME: use bulk delete when s3ninja will support this operation | ||
60 | // const deleteParams = { | ||
61 | // Bucket: bucketInfo.BUCKET_NAME, | ||
62 | // Delete: { Objects: [] } | ||
63 | // } | ||
64 | 109 | ||
65 | if (isArray(listedObjects.Contents) !== true) { | 110 | function removeObject (objectStorageKey: string, bucketInfo: BucketInfo) { |
66 | const message = `Cannot remove ${commandPrefix} prefix in bucket ${bucketInfo.BUCKET_NAME}: no files listed.` | 111 | const key = buildKey(objectStorageKey, bucketInfo) |
67 | 112 | ||
68 | logger.error(message, { response: listedObjects, ...lTags() }) | 113 | return removeObjectByFullKey(key, bucketInfo) |
69 | throw new Error(message) | 114 | } |
70 | } | ||
71 | 115 | ||
72 | for (const object of listedObjects.Contents) { | 116 | function removeObjectByFullKey (fullKey: string, bucketInfo: BucketInfo) { |
73 | const command = new DeleteObjectCommand({ | 117 | logger.debug('Removing file %s in bucket %s', fullKey, bucketInfo.BUCKET_NAME, lTags()) |
74 | Bucket: bucketInfo.BUCKET_NAME, | ||
75 | Key: object.Key | ||
76 | }) | ||
77 | 118 | ||
78 | await s3Client.send(command) | 119 | const command = new DeleteObjectCommand({ |
120 | Bucket: bucketInfo.BUCKET_NAME, | ||
121 | Key: fullKey | ||
122 | }) | ||
79 | 123 | ||
80 | // FIXME: use bulk delete when s3ninja will support this operation | 124 | return getClient().send(command) |
81 | // deleteParams.Delete.Objects.push({ Key: object.Key }) | 125 | } |
82 | } | ||
83 | 126 | ||
127 | async function removePrefix (prefix: string, bucketInfo: BucketInfo) { | ||
84 | // FIXME: use bulk delete when s3ninja will support this operation | 128 | // FIXME: use bulk delete when s3ninja will support this operation |
85 | // const deleteCommand = new DeleteObjectsCommand(deleteParams) | ||
86 | // await s3Client.send(deleteCommand) | ||
87 | 129 | ||
88 | // Repeat if not all objects could be listed at once (limit of 1000?) | 130 | logger.debug('Removing prefix %s in bucket %s', prefix, bucketInfo.BUCKET_NAME, lTags()) |
89 | if (listedObjects.IsTruncated) await removePrefix(prefix, bucketInfo) | 131 | |
132 | return applyOnPrefix({ | ||
133 | prefix, | ||
134 | bucketInfo, | ||
135 | commandBuilder: obj => { | ||
136 | logger.debug('Removing %s inside prefix %s in bucket %s', obj.Key, prefix, bucketInfo.BUCKET_NAME, lTags()) | ||
137 | |||
138 | return new DeleteObjectCommand({ | ||
139 | Bucket: bucketInfo.BUCKET_NAME, | ||
140 | Key: obj.Key | ||
141 | }) | ||
142 | } | ||
143 | }) | ||
90 | } | 144 | } |
91 | 145 | ||
146 | // --------------------------------------------------------------------------- | ||
147 | |||
92 | async function makeAvailable (options: { | 148 | async function makeAvailable (options: { |
93 | key: string | 149 | key: string |
94 | destination: string | 150 | destination: string |
@@ -116,13 +172,43 @@ function buildKey (key: string, bucketInfo: BucketInfo) { | |||
116 | 172 | ||
117 | // --------------------------------------------------------------------------- | 173 | // --------------------------------------------------------------------------- |
118 | 174 | ||
175 | async function createObjectReadStream (options: { | ||
176 | key: string | ||
177 | bucketInfo: BucketInfo | ||
178 | rangeHeader: string | ||
179 | }) { | ||
180 | const { key, bucketInfo, rangeHeader } = options | ||
181 | |||
182 | const command = new GetObjectCommand({ | ||
183 | Bucket: bucketInfo.BUCKET_NAME, | ||
184 | Key: buildKey(key, bucketInfo), | ||
185 | Range: rangeHeader | ||
186 | }) | ||
187 | |||
188 | const response = await getClient().send(command) | ||
189 | |||
190 | return response.Body as Readable | ||
191 | } | ||
192 | |||
193 | // --------------------------------------------------------------------------- | ||
194 | |||
119 | export { | 195 | export { |
120 | BucketInfo, | 196 | BucketInfo, |
121 | buildKey, | 197 | buildKey, |
198 | |||
122 | storeObject, | 199 | storeObject, |
200 | |||
123 | removeObject, | 201 | removeObject, |
202 | removeObjectByFullKey, | ||
124 | removePrefix, | 203 | removePrefix, |
125 | makeAvailable | 204 | |
205 | makeAvailable, | ||
206 | |||
207 | updateObjectACL, | ||
208 | updatePrefixACL, | ||
209 | |||
210 | listKeysOfPrefix, | ||
211 | createObjectReadStream | ||
126 | } | 212 | } |
127 | 213 | ||
128 | // --------------------------------------------------------------------------- | 214 | // --------------------------------------------------------------------------- |
@@ -131,17 +217,15 @@ async function uploadToStorage (options: { | |||
131 | content: ReadStream | 217 | content: ReadStream |
132 | objectStorageKey: string | 218 | objectStorageKey: string |
133 | bucketInfo: BucketInfo | 219 | bucketInfo: BucketInfo |
220 | isPrivate: boolean | ||
134 | }) { | 221 | }) { |
135 | const { content, objectStorageKey, bucketInfo } = options | 222 | const { content, objectStorageKey, bucketInfo, isPrivate } = options |
136 | 223 | ||
137 | const input: PutObjectCommandInput = { | 224 | const input: PutObjectCommandInput = { |
138 | Body: content, | 225 | Body: content, |
139 | Bucket: bucketInfo.BUCKET_NAME, | 226 | Bucket: bucketInfo.BUCKET_NAME, |
140 | Key: buildKey(objectStorageKey, bucketInfo) | 227 | Key: buildKey(objectStorageKey, bucketInfo), |
141 | } | 228 | ACL: getACL(isPrivate) |
142 | |||
143 | if (CONFIG.OBJECT_STORAGE.UPLOAD_ACL) { | ||
144 | input.ACL = CONFIG.OBJECT_STORAGE.UPLOAD_ACL | ||
145 | } | 229 | } |
146 | 230 | ||
147 | const parallelUploads3 = new Upload({ | 231 | const parallelUploads3 = new Upload({ |
@@ -171,5 +255,50 @@ async function uploadToStorage (options: { | |||
171 | bucketInfo.PREFIX, objectStorageKey, bucketInfo.BUCKET_NAME, lTags() | 255 | bucketInfo.PREFIX, objectStorageKey, bucketInfo.BUCKET_NAME, lTags() |
172 | ) | 256 | ) |
173 | 257 | ||
174 | return getPrivateUrl(bucketInfo, objectStorageKey) | 258 | return getInternalUrl(bucketInfo, objectStorageKey) |
259 | } | ||
260 | |||
261 | async function applyOnPrefix (options: { | ||
262 | prefix: string | ||
263 | bucketInfo: BucketInfo | ||
264 | commandBuilder: (obj: _Object) => Parameters<S3Client['send']>[0] | ||
265 | |||
266 | continuationToken?: string | ||
267 | }) { | ||
268 | const { prefix, bucketInfo, commandBuilder, continuationToken } = options | ||
269 | |||
270 | const s3Client = getClient() | ||
271 | |||
272 | const commandPrefix = buildKey(prefix, bucketInfo) | ||
273 | const listCommand = new ListObjectsV2Command({ | ||
274 | Bucket: bucketInfo.BUCKET_NAME, | ||
275 | Prefix: commandPrefix, | ||
276 | ContinuationToken: continuationToken | ||
277 | }) | ||
278 | |||
279 | const listedObjects = await s3Client.send(listCommand) | ||
280 | |||
281 | if (isArray(listedObjects.Contents) !== true) { | ||
282 | const message = `Cannot apply function on ${commandPrefix} prefix in bucket ${bucketInfo.BUCKET_NAME}: no files listed.` | ||
283 | |||
284 | logger.error(message, { response: listedObjects, ...lTags() }) | ||
285 | throw new Error(message) | ||
286 | } | ||
287 | |||
288 | await map(listedObjects.Contents, object => { | ||
289 | const command = commandBuilder(object) | ||
290 | |||
291 | return s3Client.send(command) | ||
292 | }, { concurrency: 10 }) | ||
293 | |||
294 | // Repeat if not all objects could be listed at once (limit of 1000?) | ||
295 | if (listedObjects.IsTruncated) { | ||
296 | await applyOnPrefix({ ...options, continuationToken: listedObjects.ContinuationToken }) | ||
297 | } | ||
298 | } | ||
299 | |||
300 | function getACL (isPrivate: boolean) { | ||
301 | return isPrivate | ||
302 | ? CONFIG.OBJECT_STORAGE.UPLOAD_ACL.PRIVATE | ||
303 | : CONFIG.OBJECT_STORAGE.UPLOAD_ACL.PUBLIC | ||
175 | } | 304 | } |
diff --git a/server/lib/object-storage/urls.ts b/server/lib/object-storage/urls.ts index 2a889190b..a47a98b98 100644 --- a/server/lib/object-storage/urls.ts +++ b/server/lib/object-storage/urls.ts | |||
@@ -1,10 +1,14 @@ | |||
1 | import { CONFIG } from '@server/initializers/config' | 1 | import { CONFIG } from '@server/initializers/config' |
2 | import { OBJECT_STORAGE_PROXY_PATHS, WEBSERVER } from '@server/initializers/constants' | ||
3 | import { MVideoUUID } from '@server/types/models' | ||
2 | import { BucketInfo, buildKey, getEndpointParsed } from './shared' | 4 | import { BucketInfo, buildKey, getEndpointParsed } from './shared' |
3 | 5 | ||
4 | function getPrivateUrl (config: BucketInfo, keyWithoutPrefix: string) { | 6 | function getInternalUrl (config: BucketInfo, keyWithoutPrefix: string) { |
5 | return getBaseUrl(config) + buildKey(keyWithoutPrefix, config) | 7 | return getBaseUrl(config) + buildKey(keyWithoutPrefix, config) |
6 | } | 8 | } |
7 | 9 | ||
10 | // --------------------------------------------------------------------------- | ||
11 | |||
8 | function getWebTorrentPublicFileUrl (fileUrl: string) { | 12 | function getWebTorrentPublicFileUrl (fileUrl: string) { |
9 | const baseUrl = CONFIG.OBJECT_STORAGE.VIDEOS.BASE_URL | 13 | const baseUrl = CONFIG.OBJECT_STORAGE.VIDEOS.BASE_URL |
10 | if (!baseUrl) return fileUrl | 14 | if (!baseUrl) return fileUrl |
@@ -19,11 +23,28 @@ function getHLSPublicFileUrl (fileUrl: string) { | |||
19 | return replaceByBaseUrl(fileUrl, baseUrl) | 23 | return replaceByBaseUrl(fileUrl, baseUrl) |
20 | } | 24 | } |
21 | 25 | ||
26 | // --------------------------------------------------------------------------- | ||
27 | |||
28 | function getHLSPrivateFileUrl (video: MVideoUUID, filename: string) { | ||
29 | return WEBSERVER.URL + OBJECT_STORAGE_PROXY_PATHS.STREAMING_PLAYLISTS.PRIVATE_HLS + video.uuid + `/${filename}` | ||
30 | } | ||
31 | |||
32 | function getWebTorrentPrivateFileUrl (filename: string) { | ||
33 | return WEBSERVER.URL + OBJECT_STORAGE_PROXY_PATHS.PRIVATE_WEBSEED + filename | ||
34 | } | ||
35 | |||
36 | // --------------------------------------------------------------------------- | ||
37 | |||
22 | export { | 38 | export { |
23 | getPrivateUrl, | 39 | getInternalUrl, |
40 | |||
24 | getWebTorrentPublicFileUrl, | 41 | getWebTorrentPublicFileUrl, |
25 | replaceByBaseUrl, | 42 | getHLSPublicFileUrl, |
26 | getHLSPublicFileUrl | 43 | |
44 | getHLSPrivateFileUrl, | ||
45 | getWebTorrentPrivateFileUrl, | ||
46 | |||
47 | replaceByBaseUrl | ||
27 | } | 48 | } |
28 | 49 | ||
29 | // --------------------------------------------------------------------------- | 50 | // --------------------------------------------------------------------------- |
diff --git a/server/lib/object-storage/videos.ts b/server/lib/object-storage/videos.ts index 66e738200..b764e4b22 100644 --- a/server/lib/object-storage/videos.ts +++ b/server/lib/object-storage/videos.ts | |||
@@ -1,39 +1,102 @@ | |||
1 | import { join } from 'path' | 1 | import { basename, join } from 'path' |
2 | import { logger } from '@server/helpers/logger' | 2 | import { logger } from '@server/helpers/logger' |
3 | import { CONFIG } from '@server/initializers/config' | 3 | import { CONFIG } from '@server/initializers/config' |
4 | import { MStreamingPlaylistVideo, MVideoFile } from '@server/types/models' | 4 | import { MStreamingPlaylistVideo, MVideo, MVideoFile } from '@server/types/models' |
5 | import { getHLSDirectory } from '../paths' | 5 | import { getHLSDirectory } from '../paths' |
6 | import { VideoPathManager } from '../video-path-manager' | ||
6 | import { generateHLSObjectBaseStorageKey, generateHLSObjectStorageKey, generateWebTorrentObjectStorageKey } from './keys' | 7 | import { generateHLSObjectBaseStorageKey, generateHLSObjectStorageKey, generateWebTorrentObjectStorageKey } from './keys' |
7 | import { lTags, makeAvailable, removeObject, removePrefix, storeObject } from './shared' | 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 | // --------------------------------------------------------------------------- | ||
8 | 26 | ||
9 | function storeHLSFile (playlist: MStreamingPlaylistVideo, filename: string, path?: string) { | 27 | function storeHLSFileFromFilename (playlist: MStreamingPlaylistVideo, filename: string) { |
10 | return storeObject({ | 28 | return storeObject({ |
11 | inputPath: path ?? join(getHLSDirectory(playlist.Video), filename), | 29 | inputPath: join(getHLSDirectory(playlist.Video), filename), |
12 | objectStorageKey: generateHLSObjectStorageKey(playlist, filename), | 30 | objectStorageKey: generateHLSObjectStorageKey(playlist, filename), |
13 | bucketInfo: CONFIG.OBJECT_STORAGE.STREAMING_PLAYLISTS | 31 | bucketInfo: CONFIG.OBJECT_STORAGE.STREAMING_PLAYLISTS, |
32 | isPrivate: playlist.Video.hasPrivateStaticPath() | ||
14 | }) | 33 | }) |
15 | } | 34 | } |
16 | 35 | ||
17 | function storeWebTorrentFile (filename: string) { | 36 | function storeHLSFileFromPath (playlist: MStreamingPlaylistVideo, path: string) { |
18 | return storeObject({ | 37 | return storeObject({ |
19 | inputPath: join(CONFIG.STORAGE.VIDEOS_DIR, filename), | 38 | inputPath: path, |
20 | objectStorageKey: generateWebTorrentObjectStorageKey(filename), | 39 | objectStorageKey: generateHLSObjectStorageKey(playlist, basename(path)), |
21 | bucketInfo: CONFIG.OBJECT_STORAGE.VIDEOS | 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() | ||
22 | }) | 71 | }) |
23 | } | 72 | } |
24 | 73 | ||
74 | // --------------------------------------------------------------------------- | ||
75 | |||
25 | function removeHLSObjectStorage (playlist: MStreamingPlaylistVideo) { | 76 | function removeHLSObjectStorage (playlist: MStreamingPlaylistVideo) { |
26 | return removePrefix(generateHLSObjectBaseStorageKey(playlist), CONFIG.OBJECT_STORAGE.STREAMING_PLAYLISTS) | 77 | return removePrefix(generateHLSObjectBaseStorageKey(playlist), CONFIG.OBJECT_STORAGE.STREAMING_PLAYLISTS) |
27 | } | 78 | } |
28 | 79 | ||
29 | function removeHLSFileObjectStorage (playlist: MStreamingPlaylistVideo, filename: string) { | 80 | function removeHLSFileObjectStorageByFilename (playlist: MStreamingPlaylistVideo, filename: string) { |
30 | return removeObject(generateHLSObjectStorageKey(playlist, filename), CONFIG.OBJECT_STORAGE.STREAMING_PLAYLISTS) | 81 | return removeObject(generateHLSObjectStorageKey(playlist, filename), CONFIG.OBJECT_STORAGE.STREAMING_PLAYLISTS) |
31 | } | 82 | } |
32 | 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 | |||
33 | function removeWebTorrentObjectStorage (videoFile: MVideoFile) { | 94 | function removeWebTorrentObjectStorage (videoFile: MVideoFile) { |
34 | return removeObject(generateWebTorrentObjectStorageKey(videoFile.filename), CONFIG.OBJECT_STORAGE.VIDEOS) | 95 | return removeObject(generateWebTorrentObjectStorageKey(videoFile.filename), CONFIG.OBJECT_STORAGE.VIDEOS) |
35 | } | 96 | } |
36 | 97 | ||
98 | // --------------------------------------------------------------------------- | ||
99 | |||
37 | async function makeHLSFileAvailable (playlist: MStreamingPlaylistVideo, filename: string, destination: string) { | 100 | async function makeHLSFileAvailable (playlist: MStreamingPlaylistVideo, filename: string, destination: string) { |
38 | const key = generateHLSObjectStorageKey(playlist, filename) | 101 | const key = generateHLSObjectStorageKey(playlist, filename) |
39 | 102 | ||
@@ -62,14 +125,61 @@ async function makeWebTorrentFileAvailable (filename: string, destination: strin | |||
62 | return destination | 125 | return destination |
63 | } | 126 | } |
64 | 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 | |||
65 | export { | 163 | export { |
164 | listHLSFileKeysOf, | ||
165 | |||
66 | storeWebTorrentFile, | 166 | storeWebTorrentFile, |
67 | storeHLSFile, | 167 | storeHLSFileFromFilename, |
168 | storeHLSFileFromPath, | ||
169 | |||
170 | updateWebTorrentFileACL, | ||
171 | updateHLSFilesACL, | ||
68 | 172 | ||
69 | removeHLSObjectStorage, | 173 | removeHLSObjectStorage, |
70 | removeHLSFileObjectStorage, | 174 | removeHLSFileObjectStorageByFilename, |
175 | removeHLSFileObjectStorageByPath, | ||
176 | removeHLSFileObjectStorageByFullKey, | ||
177 | |||
71 | removeWebTorrentObjectStorage, | 178 | removeWebTorrentObjectStorage, |
72 | 179 | ||
73 | makeWebTorrentFileAvailable, | 180 | makeWebTorrentFileAvailable, |
74 | makeHLSFileAvailable | 181 | makeHLSFileAvailable, |
182 | |||
183 | getWebTorrentFileReadStream, | ||
184 | getHLSFileReadStream | ||
75 | } | 185 | } |