import { randomInt } from 'crypto' import { HttpStatusCode } from '@shared/models' import { makePostBodyRequest } from '../requests' export class ObjectStorageCommand { static readonly DEFAULT_SCALEWAY_BUCKET = 'peertube-ci-test' private readonly bucketsCreated: string[] = [] private readonly seed: number // --------------------------------------------------------------------------- constructor () { this.seed = randomInt(0, 10000) } static getMockCredentialsConfig () { return { access_key_id: 'AKIAIOSFODNN7EXAMPLE', secret_access_key: 'wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY' } } static getMockEndpointHost () { return 'localhost:9444' } static getMockRegion () { return 'us-east-1' } getDefaultMockConfig () { return { object_storage: { enabled: true, endpoint: 'http://' + ObjectStorageCommand.getMockEndpointHost(), region: ObjectStorageCommand.getMockRegion(), credentials: ObjectStorageCommand.getMockCredentialsConfig(), streaming_playlists: { bucket_name: this.getMockStreamingPlaylistsBucketName() }, videos: { bucket_name: this.getMockWebVideosBucketName() } } } } getMockWebVideosBaseUrl () { return `http://${this.getMockWebVideosBucketName()}.${ObjectStorageCommand.getMockEndpointHost()}/` } getMockPlaylistBaseUrl () { return `http://${this.getMockStreamingPlaylistsBucketName()}.${ObjectStorageCommand.getMockEndpointHost()}/` } async prepareDefaultMockBuckets () { await this.createMockBucket(this.getMockStreamingPlaylistsBucketName()) await this.createMockBucket(this.getMockWebVideosBucketName()) } async createMockBucket (name: string) { this.bucketsCreated.push(name) await this.deleteMockBucket(name) await makePostBodyRequest({ url: ObjectStorageCommand.getMockEndpointHost(), path: '/ui/' + name + '?create', expectedStatus: HttpStatusCode.TEMPORARY_REDIRECT_307 }) await makePostBodyRequest({ url: ObjectStorageCommand.getMockEndpointHost(), path: '/ui/' + name + '?make-public', expectedStatus: HttpStatusCode.TEMPORARY_REDIRECT_307 }) } async cleanupMock () { for (const name of this.bucketsCreated) { await this.deleteMockBucket(name) } } getMockStreamingPlaylistsBucketName (name = 'streaming-playlists') { return this.getMockBucketName(name) } getMockWebVideosBucketName (name = 'web-videos') { return this.getMockBucketName(name) } getMockBucketName (name: string) { return `${this.seed}-${name}` } private async deleteMockBucket (name: string) { await makePostBodyRequest({ url: ObjectStorageCommand.getMockEndpointHost(), path: '/ui/' + name + '?delete', expectedStatus: HttpStatusCode.TEMPORARY_REDIRECT_307 }) } // --------------------------------------------------------------------------- static getDefaultScalewayConfig (options: { serverNumber: number enablePrivateProxy?: boolean // default true privateACL?: 'private' | 'public-read' // default 'private' }) { const { serverNumber, enablePrivateProxy = true, privateACL = 'private' } = options return { object_storage: { enabled: true, endpoint: this.getScalewayEndpointHost(), region: this.getScalewayRegion(), credentials: this.getScalewayCredentialsConfig(), upload_acl: { private: privateACL }, proxy: { proxify_private_files: enablePrivateProxy }, streaming_playlists: { bucket_name: this.DEFAULT_SCALEWAY_BUCKET, prefix: `test:server-${serverNumber}-streaming-playlists:` }, videos: { bucket_name: this.DEFAULT_SCALEWAY_BUCKET, prefix: `test:server-${serverNumber}-videos:` } } } } static getScalewayCredentialsConfig () { return { access_key_id: process.env.OBJECT_STORAGE_SCALEWAY_KEY_ID, secret_access_key: process.env.OBJECT_STORAGE_SCALEWAY_ACCESS_KEY } } static getScalewayEndpointHost () { return 's3.fr-par.scw.cloud' } static getScalewayRegion () { return 'fr-par' } static getScalewayBaseUrl () { return `https://${this.DEFAULT_SCALEWAY_BUCKET}.${this.getScalewayEndpointHost()}/` } }