serialization.ts 4.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import {
  2. VideoMetadata,
  3. PublishedBeforeJoystream,
  4. License,
  5. MediaType,
  6. ChannelMetadata,
  7. ChannelCategoryMetadata,
  8. VideoCategoryMetadata,
  9. } from '@joystream/content-metadata-protobuf'
  10. import {
  11. ChannelCategoryInputParameters,
  12. ChannelInputParameters,
  13. VideoCategoryInputParameters,
  14. VideoInputParameters,
  15. } from '../Types'
  16. import { Bytes } from '@polkadot/types/primitive'
  17. import { createType } from '@joystream/types'
  18. type AnyMetadata = {
  19. serializeBinary(): Uint8Array
  20. }
  21. export function metadataToBytes(metadata: AnyMetadata): Bytes {
  22. const bytes = createType('Bytes', '0x' + Buffer.from(metadata.serializeBinary()).toString('hex'))
  23. console.log('Metadata as Bytes:', bytes.toString())
  24. return bytes
  25. }
  26. // TODO: If "fromObject()" was generated for the protobuffs we could avoid having to create separate converters for each metadata
  27. export function videoMetadataFromInput(videoParametersInput: VideoInputParameters): VideoMetadata {
  28. const videoMetadata = new VideoMetadata()
  29. videoMetadata.setTitle(videoParametersInput.title as string)
  30. videoMetadata.setDescription(videoParametersInput.description as string)
  31. videoMetadata.setDuration(videoParametersInput.duration as number)
  32. videoMetadata.setMediaPixelHeight(videoParametersInput.mediaPixelHeight as number)
  33. videoMetadata.setMediaPixelWidth(videoParametersInput.mediaPixelWidth as number)
  34. videoMetadata.setLanguage(videoParametersInput.language as string)
  35. videoMetadata.setHasMarketing(videoParametersInput.hasMarketing as boolean)
  36. videoMetadata.setIsPublic(videoParametersInput.isPublic as boolean)
  37. videoMetadata.setIsExplicit(videoParametersInput.isExplicit as boolean)
  38. videoMetadata.setPersonsList(videoParametersInput.personsList as number[])
  39. videoMetadata.setCategory(videoParametersInput.category as number)
  40. if (videoParametersInput.mediaType) {
  41. const mediaType = new MediaType()
  42. mediaType.setCodecName(videoParametersInput.mediaType.codecName as string)
  43. mediaType.setContainer(videoParametersInput.mediaType.container as string)
  44. mediaType.setMimeMediaType(videoParametersInput.mediaType.mimeMediaType as string)
  45. videoMetadata.setMediaType(mediaType)
  46. }
  47. if (videoParametersInput.publishedBeforeJoystream) {
  48. const publishedBeforeJoystream = new PublishedBeforeJoystream()
  49. publishedBeforeJoystream.setIsPublished(videoParametersInput.publishedBeforeJoystream.isPublished as boolean)
  50. publishedBeforeJoystream.setDate(videoParametersInput.publishedBeforeJoystream.date as string)
  51. videoMetadata.setPublishedBeforeJoystream(publishedBeforeJoystream)
  52. }
  53. if (videoParametersInput.license) {
  54. const license = new License()
  55. license.setCode(videoParametersInput.license.code as number)
  56. license.setAttribution(videoParametersInput.license.attribution as string)
  57. license.setCustomText(videoParametersInput.license.customText as string)
  58. videoMetadata.setLicense(license)
  59. }
  60. return videoMetadata
  61. }
  62. export function channelMetadataFromInput(channelParametersInput: ChannelInputParameters): ChannelMetadata {
  63. const channelMetadata = new ChannelMetadata()
  64. channelMetadata.setTitle(channelParametersInput.title as string)
  65. channelMetadata.setDescription(channelParametersInput.description as string)
  66. channelMetadata.setIsPublic(channelParametersInput.isPublic as boolean)
  67. channelMetadata.setLanguage(channelParametersInput.language as string)
  68. channelMetadata.setCategory(channelParametersInput.category as number)
  69. return channelMetadata
  70. }
  71. export function channelCategoryMetadataFromInput(
  72. channelCategoryParametersInput: ChannelCategoryInputParameters
  73. ): ChannelCategoryMetadata {
  74. const channelCategoryMetadata = new ChannelCategoryMetadata()
  75. channelCategoryMetadata.setName(channelCategoryParametersInput.name as string)
  76. return channelCategoryMetadata
  77. }
  78. export function videoCategoryMetadataFromInput(
  79. videoCategoryParametersInput: VideoCategoryInputParameters
  80. ): VideoCategoryMetadata {
  81. const videoCategoryMetadata = new VideoCategoryMetadata()
  82. videoCategoryMetadata.setName(videoCategoryParametersInput.name as string)
  83. return videoCategoryMetadata
  84. }