meta.ts 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. export enum MetaType {
  2. image = 'image',
  3. video = 'video',
  4. audio = 'audio',
  5. xfile = 'xfile',
  6. other = 'other'
  7. }
  8. export const metaTypeExtnames = {
  9. [MetaType.image]: ['bmp', 'jpg', 'jpeg', 'png', 'tif', 'gif', 'pcx', 'tga', 'exif', 'fpx', 'svg', 'psd', 'cdr', 'pcd', 'dxf', 'ufo', 'eps', 'ai', 'WMF', 'webp', 'avif', 'apng'],
  10. [MetaType.audio]: ['mp3'],
  11. [MetaType.video]: ['wmv', 'asf', 'asx', 'rm', 'rmvb', 'mp4', '3gp', 'mov', 'm4v', 'avi', 'dat', 'mkv', 'flv', 'vob'],
  12. [MetaType.xfile]: ["raw", "dcm"]
  13. }
  14. export const getExtname = (url: string) => {
  15. const index = url.lastIndexOf('.')
  16. if (~index) {
  17. return url.substring(index + 1)
  18. } else {
  19. return null
  20. }
  21. }
  22. export const getUrlType = (url: string) => {
  23. if (url.includes(';base64')) {
  24. const type = url.substring(url.indexOf(':') + 1, url.indexOf('/'))
  25. if (type === 'image') {
  26. return MetaType.image
  27. } else if (type === 'video') {
  28. return MetaType.video
  29. } else if (type === 'audio') {
  30. return MetaType.audio
  31. } else if (type === 'raw' || type === 'dcm') {
  32. return MetaType.xfile
  33. } else {
  34. return MetaType.other
  35. }
  36. }
  37. const extname = getExtname(url)?.toLowerCase()
  38. if (extname) {
  39. for (const [type, extnames] of Object.entries(metaTypeExtnames)) {
  40. if (extnames.includes(extname)) {
  41. return type as MetaType
  42. }
  43. }
  44. return MetaType.other
  45. } else {
  46. return MetaType.other
  47. }
  48. }