urls.ts 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import { OBJECT_STORAGE_PROXY_PATHS, WEBSERVER } from '@server/initializers/constants.js'
  2. import { MVideoUUID } from '@server/types/models/index.js'
  3. import { BucketInfo, buildKey, getEndpointParsed } from './shared/index.js'
  4. export function getInternalUrl (config: BucketInfo, keyWithoutPrefix: string) {
  5. return getBaseUrl(config) + buildKey(keyWithoutPrefix, config)
  6. }
  7. // ---------------------------------------------------------------------------
  8. export function getObjectStoragePublicFileUrl (fileUrl: string, objectStorageConfig: { BASE_URL: string }) {
  9. const baseUrl = objectStorageConfig.BASE_URL
  10. if (!baseUrl) return fileUrl
  11. return replaceByBaseUrl(fileUrl, baseUrl)
  12. }
  13. // ---------------------------------------------------------------------------
  14. export function getHLSPrivateFileUrl (video: MVideoUUID, filename: string) {
  15. return WEBSERVER.URL + OBJECT_STORAGE_PROXY_PATHS.STREAMING_PLAYLISTS.PRIVATE_HLS + video.uuid + `/${filename}`
  16. }
  17. export function getWebVideoPrivateFileUrl (filename: string) {
  18. return WEBSERVER.URL + OBJECT_STORAGE_PROXY_PATHS.PRIVATE_WEB_VIDEOS + filename
  19. }
  20. // ---------------------------------------------------------------------------
  21. function getBaseUrl (bucketInfo: BucketInfo, baseUrl?: string) {
  22. if (baseUrl) return baseUrl
  23. return `${getEndpointParsed().protocol}//${bucketInfo.BUCKET_NAME}.${getEndpointParsed().host}/`
  24. }
  25. const regex = new RegExp('https?://[^/]+')
  26. function replaceByBaseUrl (fileUrl: string, baseUrl: string) {
  27. if (!fileUrl) return fileUrl
  28. return fileUrl.replace(regex, baseUrl)
  29. }