meta.ts 921 B

123456789101112131415161718192021222324252627282930313233
  1. export enum MetaType {
  2. image = 'image',
  3. video = 'video',
  4. other = 'other'
  5. }
  6. export const metaTypeExtnames = {
  7. [MetaType.image]: ['bmp', 'jpg', 'png', 'tif', 'gif', 'pcx', 'tga', 'exif', 'fpx', 'svg', 'psd', 'cdr', 'pcd', 'dxf', 'ufo', 'eps', 'ai', 'raw', 'WMF', 'webp', 'avif', 'apng'],
  8. [MetaType.video]: ['wmv', 'asf', 'asx', 'rm', 'rmvb', 'mp4', '3gp', 'mov', 'm4v', 'avi', 'dat', 'mkv', 'flv', 'vob']
  9. }
  10. export const getExtname = (url: string) => {
  11. const index = url.lastIndexOf('.')
  12. if (~index) {
  13. return url.substring(index + 1)
  14. } else {
  15. return null
  16. }
  17. }
  18. export const getUrlType = (url: string) => {
  19. const extname = getExtname(url)?.toLowerCase()
  20. if (extname) {
  21. for (const [type, extnames] of Object.entries(metaTypeExtnames)) {
  22. if (extnames.includes(extname)) {
  23. return type as MetaType
  24. }
  25. }
  26. return MetaType.other
  27. } else {
  28. return MetaType.other
  29. }
  30. }