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

51 lines
1.9 KiB
TypeScript
Raw Normal View History

import { CONFIG } from '@server/initializers/config.js'
import { MStreamingPlaylistVideo, MVideoFile } from '@server/types/models/index.js'
import { generateHLSObjectStorageKey, generateWebVideoObjectStorageKey } from './keys.js'
import { buildKey, getClient } from './shared/index.js'
import { getHLSPublicFileUrl, getWebVideoPublicFileUrl } from './urls.js'
2023-06-06 04:14:13 -05:00
export async function generateWebVideoPresignedUrl (options: {
2023-06-06 04:14:13 -05:00
file: MVideoFile
downloadFilename: string
}) {
const { file, downloadFilename } = options
const key = generateWebVideoObjectStorageKey(file.filename)
2023-06-06 04:14:13 -05:00
const { GetObjectCommand } = await import('@aws-sdk/client-s3')
const { getSignedUrl } = await import('@aws-sdk/s3-request-presigner')
2023-06-06 04:14:13 -05:00
const command = new GetObjectCommand({
2023-07-11 04:23:51 -05:00
Bucket: CONFIG.OBJECT_STORAGE.WEB_VIDEOS.BUCKET_NAME,
Key: buildKey(key, CONFIG.OBJECT_STORAGE.WEB_VIDEOS),
ResponseContentDisposition: `attachment; filename=${encodeURI(downloadFilename)}`
2023-06-06 04:14:13 -05:00
})
const url = await getSignedUrl(await getClient(), command, { expiresIn: 3600 * 24 })
return getWebVideoPublicFileUrl(url)
2023-06-06 04:14:13 -05:00
}
export async function generateHLSFilePresignedUrl (options: {
2023-06-06 04:14:13 -05:00
streamingPlaylist: MStreamingPlaylistVideo
file: MVideoFile
downloadFilename: string
}) {
const { streamingPlaylist, file, downloadFilename } = options
const key = generateHLSObjectStorageKey(streamingPlaylist, file.filename)
const { GetObjectCommand } = await import('@aws-sdk/client-s3')
const { getSignedUrl } = await import('@aws-sdk/s3-request-presigner')
2023-06-06 04:14:13 -05:00
const command = new GetObjectCommand({
Bucket: CONFIG.OBJECT_STORAGE.STREAMING_PLAYLISTS.BUCKET_NAME,
Key: buildKey(key, CONFIG.OBJECT_STORAGE.STREAMING_PLAYLISTS),
ResponseContentDisposition: `attachment; filename=${encodeURI(downloadFilename)}`
2023-06-06 04:14:13 -05:00
})
const url = await getSignedUrl(await getClient(), command, { expiresIn: 3600 * 24 })
return getHLSPublicFileUrl(url)
2023-06-06 04:14:13 -05:00
}