pre-signed-urls.ts 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import { CONFIG } from '@server/initializers/config.js'
  2. import { MStreamingPlaylistVideo, MUserExport, MVideoFile } from '@server/types/models/index.js'
  3. import { MVideoSource } from '@server/types/models/video/video-source.js'
  4. import {
  5. generateHLSObjectStorageKey,
  6. generateOriginalVideoObjectStorageKey,
  7. generateUserExportObjectStorageKey,
  8. generateWebVideoObjectStorageKey
  9. } from './keys.js'
  10. import { buildKey, getClient } from './shared/index.js'
  11. import { getObjectStoragePublicFileUrl } from './urls.js'
  12. export async function generateWebVideoPresignedUrl (options: {
  13. file: MVideoFile
  14. downloadFilename: string
  15. }) {
  16. const { file, downloadFilename } = options
  17. const url = await generatePresignedUrl({
  18. bucket: CONFIG.OBJECT_STORAGE.WEB_VIDEOS.BUCKET_NAME,
  19. key: buildKey(generateWebVideoObjectStorageKey(file.filename), CONFIG.OBJECT_STORAGE.WEB_VIDEOS),
  20. downloadFilename
  21. })
  22. return getObjectStoragePublicFileUrl(url, CONFIG.OBJECT_STORAGE.WEB_VIDEOS)
  23. }
  24. export async function generateHLSFilePresignedUrl (options: {
  25. streamingPlaylist: MStreamingPlaylistVideo
  26. file: MVideoFile
  27. downloadFilename: string
  28. }) {
  29. const { streamingPlaylist, file, downloadFilename } = options
  30. const url = await generatePresignedUrl({
  31. bucket: CONFIG.OBJECT_STORAGE.STREAMING_PLAYLISTS.BUCKET_NAME,
  32. key: buildKey(generateHLSObjectStorageKey(streamingPlaylist, file.filename), CONFIG.OBJECT_STORAGE.STREAMING_PLAYLISTS),
  33. downloadFilename
  34. })
  35. return getObjectStoragePublicFileUrl(url, CONFIG.OBJECT_STORAGE.STREAMING_PLAYLISTS)
  36. }
  37. export async function generateUserExportPresignedUrl (options: {
  38. userExport: MUserExport
  39. downloadFilename: string
  40. }) {
  41. const { userExport, downloadFilename } = options
  42. const url = await generatePresignedUrl({
  43. bucket: CONFIG.OBJECT_STORAGE.USER_EXPORTS.BUCKET_NAME,
  44. key: buildKey(generateUserExportObjectStorageKey(userExport.filename), CONFIG.OBJECT_STORAGE.USER_EXPORTS),
  45. downloadFilename
  46. })
  47. return getObjectStoragePublicFileUrl(url, CONFIG.OBJECT_STORAGE.USER_EXPORTS)
  48. }
  49. export async function generateOriginalFilePresignedUrl (options: {
  50. videoSource: MVideoSource
  51. downloadFilename: string
  52. }) {
  53. const { videoSource, downloadFilename } = options
  54. const url = await generatePresignedUrl({
  55. bucket: CONFIG.OBJECT_STORAGE.ORIGINAL_VIDEO_FILES.BUCKET_NAME,
  56. key: buildKey(generateOriginalVideoObjectStorageKey(videoSource.keptOriginalFilename), CONFIG.OBJECT_STORAGE.ORIGINAL_VIDEO_FILES),
  57. downloadFilename
  58. })
  59. return getObjectStoragePublicFileUrl(url, CONFIG.OBJECT_STORAGE.ORIGINAL_VIDEO_FILES)
  60. }
  61. // ---------------------------------------------------------------------------
  62. // Private
  63. // ---------------------------------------------------------------------------
  64. async function generatePresignedUrl (options: {
  65. bucket: string
  66. key: string
  67. downloadFilename: string
  68. }) {
  69. const { bucket, downloadFilename, key } = options
  70. const { GetObjectCommand } = await import('@aws-sdk/client-s3')
  71. const { getSignedUrl } = await import('@aws-sdk/s3-request-presigner')
  72. const command = new GetObjectCommand({
  73. Bucket: bucket,
  74. Key: key,
  75. ResponseContentDisposition: `attachment; filename="${encodeURI(downloadFilename)}"`
  76. })
  77. return getSignedUrl(await getClient(), command, { expiresIn: 3600 * 24 })
  78. }