aboutsummaryrefslogtreecommitdiffhomepage
path: root/shared/server-commands/server/object-storage-command.ts
blob: b4de8f4cbbbab7d42c0054a09e9ecb350b10c6b8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import { HttpStatusCode } from '@shared/models'
import { makePostBodyRequest } from '../requests'
import { AbstractCommand } from '../shared'

export class ObjectStorageCommand extends AbstractCommand {
  static readonly DEFAULT_PLAYLIST_BUCKET = 'streaming-playlists'
  static readonly DEFAULT_WEBTORRENT_BUCKET = 'videos'

  static getDefaultConfig () {
    return {
      object_storage: {
        enabled: true,
        endpoint: 'http://' + this.getEndpointHost(),
        region: this.getRegion(),

        credentials: this.getCredentialsConfig(),

        streaming_playlists: {
          bucket_name: this.DEFAULT_PLAYLIST_BUCKET
        },

        videos: {
          bucket_name: this.DEFAULT_WEBTORRENT_BUCKET
        }
      }
    }
  }

  static getCredentialsConfig () {
    return {
      access_key_id: 'AKIAIOSFODNN7EXAMPLE',
      secret_access_key: 'wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY'
    }
  }

  static getEndpointHost () {
    return 'localhost:9444'
  }

  static getRegion () {
    return 'us-east-1'
  }

  static getWebTorrentBaseUrl () {
    return `http://${this.DEFAULT_WEBTORRENT_BUCKET}.${this.getEndpointHost()}/`
  }

  static getPlaylistBaseUrl () {
    return `http://${this.DEFAULT_PLAYLIST_BUCKET}.${this.getEndpointHost()}/`
  }

  static async prepareDefaultBuckets () {
    await this.createBucket(this.DEFAULT_PLAYLIST_BUCKET)
    await this.createBucket(this.DEFAULT_WEBTORRENT_BUCKET)
  }

  static async createBucket (name: string) {
    await makePostBodyRequest({
      url: this.getEndpointHost(),
      path: '/ui/' + name + '?delete',
      expectedStatus: HttpStatusCode.TEMPORARY_REDIRECT_307
    })

    await makePostBodyRequest({
      url: this.getEndpointHost(),
      path: '/ui/' + name + '?create',
      expectedStatus: HttpStatusCode.TEMPORARY_REDIRECT_307
    })

    await makePostBodyRequest({
      url: this.getEndpointHost(),
      path: '/ui/' + name + '?make-public',
      expectedStatus: HttpStatusCode.TEMPORARY_REDIRECT_307
    })
  }
}