4242 * @property  {ProbeFormat } format 
4343 */ 
4444
45+ /** 
46+  * Maps the ffprobe format.format_name string to a short MIME type. 
47+  * @type  {Object<string, string> } 
48+  */ 
49+ const  FORMAT_NAME_TO_SHORT_TYPE  =  { 
50+   'avi' : 'video/x-msvideo' , 
51+   'flac' : 'audio/flac' , 
52+   'mp3' : 'audio/mpeg' , 
53+   'wav' : 'audio/wav' , 
54+ } 
55+ 
4556/** 
4657 * TODO: Reconcile this with file/sniffer.js findMimeType() which does signature matching. 
4758 * @param  {ProbeInfo } info 
@@ -51,11 +62,9 @@ export function getShortMIMEString(info) {
5162  if  ( ! info )  throw  `Invalid ProbeInfo` ; 
5263  if  ( ! info . streams  ||  info . streams . length  ===  0 )  throw  `No streams in ProbeInfo` ; 
5364
54-   // mp3/flac files are always considered audio/ (even with mjpeg video streams). 
55-   if  ( info . format . format_name  ===  'mp3' )  { 
56-     return  'audio/mpeg' ; 
57-   }  else  if  ( info . format . format_name  ===  'flac' )  { 
58-     return  'audio/flac' ; 
65+   const  formatName  =  info ?. format ?. format_name ; 
66+   if  ( formatName  &&  Object . keys ( FORMAT_NAME_TO_SHORT_TYPE ) . includes ( formatName ) )  { 
67+     return  FORMAT_NAME_TO_SHORT_TYPE [ formatName ] ; 
5968  } 
6069
6170  // M4A files are specifically audio/mp4. 
@@ -75,10 +84,7 @@ export function getShortMIMEString(info) {
7584
7685  /** @type  {string } */ 
7786  let  subType ; 
78-   switch  ( info . format . format_name )  { 
79-     case  'avi' :
80-       subType  =  'x-msvideo' ; 
81-       break ; 
87+   switch  ( formatName )  { 
8288    case  'mpeg' :
8389      subType  =  'mpeg' ; 
8490      break ; 
@@ -93,7 +99,7 @@ export function getShortMIMEString(info) {
9399      subType  =  'webm' ; 
94100      break ; 
95101    default :
96-       throw  `Cannot handle format ${ info . format . format_name }   + 
102+       throw  `Cannot handle format ${ formatName }   + 
97103          `Please file a bug https://github.com/codedread/bitjs/issues/new` ; 
98104  } 
99105
@@ -113,9 +119,7 @@ export function getShortMIMEString(info) {
113119export  function  getFullMIMEString ( info )  { 
114120  /** A string like 'video/mp4' */ 
115121  let  contentType  =  `${ getShortMIMEString ( info ) }  ; 
116-   // If MP3/FLAC, just send back the type. 
117-   if  ( contentType  ===  'audio/mpeg' 
118-       ||  contentType  ===  'audio/flac' )  { 
122+   if  ( Object . values ( FORMAT_NAME_TO_SHORT_TYPE ) . includes ( contentType ) )  { 
119123    return  contentType ; 
120124  } 
121125
0 commit comments