urls.ts 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import { CONFIG } from '@server/initializers/config'
  2. import { OBJECT_STORAGE_PROXY_PATHS, WEBSERVER } from '@server/initializers/constants'
  3. import { MVideoUUID } from '@server/types/models'
  4. import { BucketInfo, buildKey, getEndpointParsed } from './shared'
  5. function getInternalUrl (config: BucketInfo, keyWithoutPrefix: string) {
  6. return getBaseUrl(config) + buildKey(keyWithoutPrefix, config)
  7. }
  8. // ---------------------------------------------------------------------------
  9. function getWebTorrentPublicFileUrl (fileUrl: string) {
  10. const baseUrl = CONFIG.OBJECT_STORAGE.VIDEOS.BASE_URL
  11. if (!baseUrl) return fileUrl
  12. return replaceByBaseUrl(fileUrl, baseUrl)
  13. }
  14. function getHLSPublicFileUrl (fileUrl: string) {
  15. const baseUrl = CONFIG.OBJECT_STORAGE.STREAMING_PLAYLISTS.BASE_URL
  16. if (!baseUrl) return fileUrl
  17. return replaceByBaseUrl(fileUrl, baseUrl)
  18. }
  19. // ---------------------------------------------------------------------------
  20. function getHLSPrivateFileUrl (video: MVideoUUID, filename: string) {
  21. return WEBSERVER.URL + OBJECT_STORAGE_PROXY_PATHS.STREAMING_PLAYLISTS.PRIVATE_HLS + video.uuid + `/${filename}`
  22. }
  23. function getWebTorrentPrivateFileUrl (filename: string) {
  24. return WEBSERVER.URL + OBJECT_STORAGE_PROXY_PATHS.PRIVATE_WEBSEED + filename
  25. }
  26. // ---------------------------------------------------------------------------
  27. export {
  28. getInternalUrl,
  29. getWebTorrentPublicFileUrl,
  30. getHLSPublicFileUrl,
  31. getHLSPrivateFileUrl,
  32. getWebTorrentPrivateFileUrl,
  33. replaceByBaseUrl
  34. }
  35. // ---------------------------------------------------------------------------
  36. function getBaseUrl (bucketInfo: BucketInfo, baseUrl?: string) {
  37. if (baseUrl) return baseUrl
  38. return `${getEndpointParsed().protocol}//${bucketInfo.BUCKET_NAME}.${getEndpointParsed().host}/`
  39. }
  40. const regex = new RegExp('https?://[^/]+')
  41. function replaceByBaseUrl (fileUrl: string, baseUrl: string) {
  42. return fileUrl.replace(regex, baseUrl)
  43. }