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

47 lines
1.7 KiB
TypeScript
Raw Normal View History

2023-06-06 11:14:13 +02:00
import { GetObjectCommand } from '@aws-sdk/client-s3'
import { getSignedUrl } from '@aws-sdk/s3-request-presigner'
import { CONFIG } from '@server/initializers/config'
import { MStreamingPlaylistVideo, MVideoFile } from '@server/types/models'
import { generateHLSObjectStorageKey, generateWebVideoObjectStorageKey } from './keys'
2023-06-06 11:14:13 +02:00
import { buildKey, getClient } from './shared'
import { getHLSPublicFileUrl, getWebVideoPublicFileUrl } from './urls'
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
const key = generateWebVideoObjectStorageKey(file.filename)
2023-06-06 11:14:13 +02:00
const command = new GetObjectCommand({
2023-07-11 11:23:51 +02:00
Bucket: CONFIG.OBJECT_STORAGE.WEB_VIDEOS.BUCKET_NAME,
Key: buildKey(key, CONFIG.OBJECT_STORAGE.WEB_VIDEOS),
2023-06-06 11:14:13 +02:00
ResponseContentDisposition: `attachment; filename=${downloadFilename}`
})
const url = await getSignedUrl(getClient(), command, { expiresIn: 3600 * 24 })
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
const key = generateHLSObjectStorageKey(streamingPlaylist, file.filename)
const command = new GetObjectCommand({
Bucket: CONFIG.OBJECT_STORAGE.STREAMING_PLAYLISTS.BUCKET_NAME,
Key: buildKey(key, CONFIG.OBJECT_STORAGE.STREAMING_PLAYLISTS),
ResponseContentDisposition: `attachment; filename=${downloadFilename}`
})
const url = await getSignedUrl(getClient(), command, { expiresIn: 3600 * 24 })
return getHLSPublicFileUrl(url)
2023-06-06 11:14:13 +02:00
}