@@ -1265,6 +1265,98 @@ public static string GetExtension(string mimeType)
1265
1265
1266
1266
throw new NotSupportedException ( "Unknown mimeType: " + mimeType ) ;
1267
1267
}
1268
+
1269
+ //lowercases and trims left part of content-type prior ';'
1270
+ public static string GetRealContentType ( string contentType )
1271
+ {
1272
+ if ( contentType == null )
1273
+ return null ;
1274
+
1275
+ int start = - 1 , end = - 1 ;
1276
+
1277
+ for ( int i = 0 ; i < contentType . Length ; i ++ )
1278
+ {
1279
+ if ( ! char . IsWhiteSpace ( contentType [ i ] ) )
1280
+ {
1281
+ if ( contentType [ i ] == ';' )
1282
+ break ;
1283
+ if ( start == - 1 )
1284
+ {
1285
+ start = i ;
1286
+ }
1287
+ end = i ;
1288
+ }
1289
+ }
1290
+
1291
+ return start != - 1
1292
+ ? contentType . Substring ( start , end - start + 1 ) . ToLowerInvariant ( )
1293
+ : null ;
1294
+ }
1295
+
1296
+ //Compares two string from start to ';' char, case-insensitive,
1297
+ //ignoring (trimming) spaces at start and end
1298
+ public static bool MatchesContentType ( string contentType , string matchesContentType )
1299
+ {
1300
+ if ( contentType == null || matchesContentType == null )
1301
+ return false ;
1302
+
1303
+ int start = - 1 , matchStart = - 1 , matchEnd = - 1 ;
1304
+
1305
+ for ( var i = 0 ; i < contentType . Length ; i ++ )
1306
+ {
1307
+ if ( char . IsWhiteSpace ( contentType [ i ] ) )
1308
+ continue ;
1309
+ start = i ;
1310
+ break ;
1311
+ }
1312
+
1313
+ for ( var i = 0 ; i < matchesContentType . Length ; i ++ )
1314
+ {
1315
+ if ( char . IsWhiteSpace ( matchesContentType [ i ] ) )
1316
+ continue ;
1317
+ if ( matchesContentType [ i ] == ';' )
1318
+ break ;
1319
+ if ( matchStart == - 1 )
1320
+ matchStart = i ;
1321
+ matchEnd = i ;
1322
+ }
1323
+
1324
+ return start != - 1 && matchStart != - 1 && matchEnd != - 1
1325
+ && string . Compare ( contentType , start ,
1326
+ matchesContentType , matchStart , matchEnd - matchStart + 1 ,
1327
+ StringComparison . OrdinalIgnoreCase ) == 0 ;
1328
+ }
1329
+
1330
+ public static Func < string , bool ? > IsBinaryFilter { get ; set ; }
1331
+
1332
+ public static bool IsBinary ( string contentType )
1333
+ {
1334
+ var userFilter = IsBinaryFilter ? . Invoke ( contentType ) ;
1335
+ if ( userFilter != null )
1336
+ return userFilter . Value ;
1337
+
1338
+ var realContentType = GetRealContentType ( contentType ) ;
1339
+ switch ( realContentType )
1340
+ {
1341
+ case ProtoBuf :
1342
+ case MsgPack :
1343
+ case Binary :
1344
+ case Bson :
1345
+ case Wire :
1346
+ return true ;
1347
+ }
1348
+
1349
+ var primaryType = realContentType . LeftPart ( '/' ) ;
1350
+ switch ( primaryType )
1351
+ {
1352
+ case "image" :
1353
+ case "audio" :
1354
+ case "video" :
1355
+ return true ;
1356
+ }
1357
+
1358
+ return false ;
1359
+ }
1268
1360
1269
1361
public static string GetMimeType ( string fileNameOrExt )
1270
1362
{
@@ -1297,6 +1389,9 @@ public static string GetMimeType(string fileNameOrExt)
1297
1389
1298
1390
case "svg" :
1299
1391
return "image/svg+xml" ;
1392
+
1393
+ case "ico" :
1394
+ return "image/x-icon" ;
1300
1395
1301
1396
case "htm" :
1302
1397
case "html" :
0 commit comments