Design system.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

50 lines
1.1 KiB

  1. import * as mimeTypes from 'mime-types';
  2. export enum ContentType {
  3. TEXT = 'text',
  4. AUDIO = 'audio',
  5. VIDEO = 'video',
  6. IMAGE = 'image',
  7. BINARY = 'binary',
  8. }
  9. export const getContentType = (mimeType?: string, filename?: string) => {
  10. let effectiveMimeType: string;
  11. if (typeof mimeType !== 'string') {
  12. if (typeof filename !== 'string') {
  13. return ContentType.BINARY;
  14. }
  15. const lookupMimeType = mimeTypes.lookup(filename);
  16. if (typeof lookupMimeType !== 'string') {
  17. return ContentType.BINARY;
  18. }
  19. effectiveMimeType = lookupMimeType;
  20. } else {
  21. effectiveMimeType = mimeType;
  22. }
  23. if (
  24. effectiveMimeType === 'application/json'
  25. || effectiveMimeType === 'application/xml'
  26. || effectiveMimeType.startsWith('text/')
  27. ) {
  28. return ContentType.TEXT;
  29. }
  30. if (effectiveMimeType.startsWith('video/')) {
  31. return ContentType.VIDEO;
  32. }
  33. if (effectiveMimeType.startsWith('audio/')) {
  34. return ContentType.AUDIO;
  35. }
  36. if (effectiveMimeType.startsWith('image/')) {
  37. return ContentType.IMAGE;
  38. }
  39. return ContentType.BINARY;
  40. };