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

47 lines
1.7 KiB
TypeScript
Raw Normal View History

2023-06-06 04:14:13 -05: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, generateWebTorrentObjectStorageKey } from './keys'
import { buildKey, getClient } from './shared'
import { getHLSPublicFileUrl, getWebTorrentPublicFileUrl } from './urls'
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 = generateWebTorrentObjectStorageKey(file.filename)
const command = new GetObjectCommand({
Bucket: CONFIG.OBJECT_STORAGE.VIDEOS.BUCKET_NAME,
Key: buildKey(key, CONFIG.OBJECT_STORAGE.VIDEOS),
ResponseContentDisposition: `attachment; filename=${downloadFilename}`
})
const url = await getSignedUrl(getClient(), command, { expiresIn: 3600 * 24 })
return getWebTorrentPublicFileUrl(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 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 04:14:13 -05:00
}