base.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import chalk from 'chalk'
  2. import removeEndingForwardSlash from '@joystream/storage-utils/stripEndingSlash'
  3. import { ContentId } from '@joystream/types/storage'
  4. import Debug from 'debug'
  5. const debug = Debug('joystream:storage-cli:base')
  6. // Commands base abstract class. Contains reusable methods.
  7. export abstract class BaseCommand {
  8. protected readonly api: any
  9. constructor(api: any) {
  10. this.api = api
  11. }
  12. // Creates the Colossus asset URL and logs it.
  13. protected createAndLogAssetUrl(url: string, contentId: string | ContentId): string {
  14. let normalizedContentId: string
  15. if (typeof contentId === 'string') {
  16. normalizedContentId = contentId
  17. } else {
  18. normalizedContentId = contentId.encode()
  19. }
  20. const normalizedUrl = removeEndingForwardSlash(url)
  21. const assetUrl = `${normalizedUrl}/asset/v0/${normalizedContentId}`
  22. console.log(chalk.yellow('Generated asset URL:', assetUrl))
  23. return assetUrl
  24. }
  25. // Abstract method to provide parameter validation.
  26. protected abstract validateParameters(): boolean
  27. // Abstract method to show command usage.
  28. protected abstract showUsage()
  29. // Checks command parameters and shows the usage if necessary.
  30. protected assertParameters(): boolean {
  31. // Create, validate and show parameters.
  32. if (!this.validateParameters()) {
  33. console.log(chalk.yellow(`Invalid parameters for the command:`))
  34. this.showUsage()
  35. return false
  36. }
  37. return true
  38. }
  39. // Shows the error message and ends the process with error code.
  40. protected fail(message: string): void {
  41. console.log(chalk.red(message))
  42. process.exit(1)
  43. }
  44. protected maxContentSize(): number {
  45. // Maximum content length for the assets (files)
  46. return 2000 * 1024 * 1024
  47. }
  48. // Requests the runtime and obtains the storage node endpoint URL.
  49. protected async getStorageProviderEndpoint(storageProviderId: string): Promise<string> {
  50. try {
  51. const endpoint = await this.api.workers.getWorkerStorageValue(storageProviderId)
  52. debug(`Resolved endpoint: ${endpoint}`)
  53. return endpoint
  54. } catch (err) {
  55. this.fail(`Could not get provider endpoint: ${err}`)
  56. }
  57. }
  58. protected async getAnyProviderEndpoint(): Promise<string> {
  59. try {
  60. const providers = await this.api.workers.getAllProviders()
  61. debug(`Available Providers: ${providers}`)
  62. // select first provider
  63. do {
  64. const id = providers.ids.pop()
  65. const endpoint = await this.getStorageProviderEndpoint(id)
  66. if (endpoint) {
  67. return endpoint
  68. }
  69. } while (providers.ids.length)
  70. throw new Error('No Providers registered endpoint')
  71. } catch (err) {
  72. this.fail(`Could not get provider endpoint: ${err}`)
  73. }
  74. }
  75. }