File size: 1,146 Bytes
c1f12bf
ed9e9d0
 
 
 
c1f12bf
ed9e9d0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c1f12bf
ed9e9d0
c1f12bf
 
ed9e9d0
 
c1f12bf
ed9e9d0
 
 
c1f12bf
ed9e9d0
c1f12bf
ed9e9d0
c1f12bf
ed9e9d0
 
c1f12bf
ed9e9d0
 
 
 
 
 
c1f12bf
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
import { ClapOutputType } from '@aitube/clap'

/**
 * break a base64 string into sub-components
 */
export function getTypeAndExtension(base64: string = ''): {
  // category eg. video, audio, text
  category: string

  // file format eg. video/mp4 text/html audio/wave
  assetFileFormat: string

  // file extension eg. .mp4 .html .wav
  extension: string

  outputType: ClapOutputType
} {
  // Regular expression to extract the MIME type and the base64 data
  const matches = base64.match(/^data:([A-Za-z-+0-9/]+);base64,(.+)$/)

  if (!matches || matches.length !== 3) {
    throw new Error('Invalid base64 string')
  }

  const assetFileFormat = matches[1] || ''

  // this should be enough for most media formats (jpeg, png, webp, mp4)
  const [category, extension] = assetFileFormat.split('/')

  let outputType = ClapOutputType.TEXT

  if (category === 'audio') {
    outputType = ClapOutputType.AUDIO
  } else if (category === 'image') {
    outputType = ClapOutputType.IMAGE
  } else if (category === 'video') {
    outputType = ClapOutputType.VIDEO
  }

  return {
    category,
    assetFileFormat,
    extension,
    outputType,
  }
}