object-storage-command.ts 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import { HttpStatusCode } from '@shared/models'
  2. import { makePostBodyRequest } from '../requests'
  3. import { AbstractCommand } from '../shared'
  4. export class ObjectStorageCommand extends AbstractCommand {
  5. static readonly DEFAULT_PLAYLIST_BUCKET = 'streaming-playlists'
  6. static readonly DEFAULT_WEBTORRENT_BUCKET = 'videos'
  7. static getDefaultConfig () {
  8. return {
  9. object_storage: {
  10. enabled: true,
  11. endpoint: 'http://' + this.getEndpointHost(),
  12. region: this.getRegion(),
  13. credentials: this.getCredentialsConfig(),
  14. streaming_playlists: {
  15. bucket_name: this.DEFAULT_PLAYLIST_BUCKET
  16. },
  17. videos: {
  18. bucket_name: this.DEFAULT_WEBTORRENT_BUCKET
  19. }
  20. }
  21. }
  22. }
  23. static getCredentialsConfig () {
  24. return {
  25. access_key_id: 'AKIAIOSFODNN7EXAMPLE',
  26. secret_access_key: 'wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY'
  27. }
  28. }
  29. static getEndpointHost () {
  30. return 'localhost:9444'
  31. }
  32. static getRegion () {
  33. return 'us-east-1'
  34. }
  35. static getWebTorrentBaseUrl () {
  36. return `http://${this.DEFAULT_WEBTORRENT_BUCKET}.${this.getEndpointHost()}/`
  37. }
  38. static getPlaylistBaseUrl () {
  39. return `http://${this.DEFAULT_PLAYLIST_BUCKET}.${this.getEndpointHost()}/`
  40. }
  41. static async prepareDefaultBuckets () {
  42. await this.createBucket(this.DEFAULT_PLAYLIST_BUCKET)
  43. await this.createBucket(this.DEFAULT_WEBTORRENT_BUCKET)
  44. }
  45. static async createBucket (name: string) {
  46. await makePostBodyRequest({
  47. url: this.getEndpointHost(),
  48. path: '/ui/' + name + '?delete',
  49. expectedStatus: HttpStatusCode.TEMPORARY_REDIRECT_307
  50. })
  51. await makePostBodyRequest({
  52. url: this.getEndpointHost(),
  53. path: '/ui/' + name + '?create',
  54. expectedStatus: HttpStatusCode.TEMPORARY_REDIRECT_307
  55. })
  56. await makePostBodyRequest({
  57. url: this.getEndpointHost(),
  58. path: '/ui/' + name + '?make-public',
  59. expectedStatus: HttpStatusCode.TEMPORARY_REDIRECT_307
  60. })
  61. }
  62. }