PeerTube/server/core/lib/object-storage/pre-signed-urls.ts

75 lines
2.5 KiB
TypeScript
Raw Normal View History

import { CONFIG } from '@server/initializers/config.js'
2024-02-12 10:47:52 +01:00
import { MStreamingPlaylistVideo, MUserExport, MVideoFile } from '@server/types/models/index.js'
import { generateHLSObjectStorageKey, generateUserExportObjectStorageKey, generateWebVideoObjectStorageKey } from './keys.js'
import { buildKey, getClient } from './shared/index.js'
import { getHLSPublicFileUrl, getWebVideoPublicFileUrl } from './urls.js'
2023-06-06 11:14:13 +02:00
export async function generateWebVideoPresignedUrl (options: {
2023-06-06 11:14:13 +02:00
file: MVideoFile
downloadFilename: string
}) {
const { file, downloadFilename } = options
2024-02-12 10:47:52 +01:00
const url = await generatePresignedUrl({
bucket: CONFIG.OBJECT_STORAGE.WEB_VIDEOS.BUCKET_NAME,
key: buildKey(generateWebVideoObjectStorageKey(file.filename), CONFIG.OBJECT_STORAGE.WEB_VIDEOS),
downloadFilename
2023-06-06 11:14:13 +02:00
})
return getWebVideoPublicFileUrl(url)
2023-06-06 11:14:13 +02:00
}
export async function generateHLSFilePresignedUrl (options: {
2023-06-06 11:14:13 +02:00
streamingPlaylist: MStreamingPlaylistVideo
file: MVideoFile
downloadFilename: string
}) {
const { streamingPlaylist, file, downloadFilename } = options
2024-02-12 10:47:52 +01:00
const url = await generatePresignedUrl({
bucket: CONFIG.OBJECT_STORAGE.STREAMING_PLAYLISTS.BUCKET_NAME,
key: buildKey(generateHLSObjectStorageKey(streamingPlaylist, file.filename), CONFIG.OBJECT_STORAGE.STREAMING_PLAYLISTS),
downloadFilename
})
return getHLSPublicFileUrl(url)
}
export async function generateUserExportPresignedUrl (options: {
userExport: MUserExport
downloadFilename: string
}) {
const { userExport, downloadFilename } = options
const url = await generatePresignedUrl({
bucket: CONFIG.OBJECT_STORAGE.USER_EXPORTS.BUCKET_NAME,
key: buildKey(generateUserExportObjectStorageKey(userExport.filename), CONFIG.OBJECT_STORAGE.USER_EXPORTS),
downloadFilename
})
return getHLSPublicFileUrl(url)
}
// ---------------------------------------------------------------------------
// Private
// ---------------------------------------------------------------------------
async function generatePresignedUrl (options: {
bucket: string
key: string
downloadFilename: string
}) {
const { bucket, downloadFilename, key } = options
2023-06-06 11:14:13 +02:00
const { GetObjectCommand } = await import('@aws-sdk/client-s3')
const { getSignedUrl } = await import('@aws-sdk/s3-request-presigner')
2023-06-06 11:14:13 +02:00
const command = new GetObjectCommand({
2024-02-12 10:47:52 +01:00
Bucket: bucket,
Key: key,
2024-01-12 14:07:54 +01:00
ResponseContentDisposition: `attachment; filename="${encodeURI(downloadFilename)}"`
2023-06-06 11:14:13 +02:00
})
2024-02-12 10:47:52 +01:00
return getSignedUrl(await getClient(), command, { expiresIn: 3600 * 24 })
2023-06-06 11:14:13 +02:00
}