11using System ;
22using System . Collections . Generic ;
33using System . IO ;
4+ using System . Text . RegularExpressions ;
45
56namespace ManagedCode . MimeTypes ;
67
@@ -35,4 +36,72 @@ public static string GetMimeType(string extension)
3536 extension = extension . Replace ( '.' , '\0 ' ) ;
3637 return MimeTypes . TryGetValue ( extension , out var mime ) ? mime : "application/octet-stream" ;
3738 }
39+
40+ private static readonly Regex XmlPattern = new ( @"^(?:application|text)/.*?\+?xml$" , RegexOptions . Compiled | RegexOptions . IgnoreCase ) ;
41+ private static readonly Regex JsonPattern = new ( @"^application/(?:.*?\+)?json5?$" , RegexOptions . Compiled | RegexOptions . IgnoreCase ) ;
42+ private static readonly Regex ArchivePattern = new ( @"^application/(?:zip|x-(?:7z|rar|tar|bzip2|gzip)-compressed|gzip|vnd\.rar|octet-stream)$" , RegexOptions . Compiled | RegexOptions . IgnoreCase ) ;
43+ private static readonly Regex ExecutablePattern = new ( @"^application/(?:x-msdownload|x-executable|x-msi|x-apple-diskimage|octet-stream)$" , RegexOptions . Compiled | RegexOptions . IgnoreCase ) ;
44+ private static readonly Regex CertificatePattern = new ( @"^application/(?:x-x509-ca-cert|pkix-cert|x-pkcs12)$" , RegexOptions . Compiled | RegexOptions . IgnoreCase ) ;
45+
46+ // New patterns for specific document types
47+ private static readonly Regex WordPattern = new ( @"^application/(?:msword|vnd\.openxmlformats-officedocument\.wordprocessingml\.|vnd\.ms-word\.|vnd\.oasis\.opendocument\.text)" , RegexOptions . Compiled | RegexOptions . IgnoreCase ) ;
48+ private static readonly Regex SpreadsheetPattern = new ( @"^application/(?:vnd\.ms-excel|vnd\.openxmlformats-officedocument\.spreadsheetml\.|vnd\.oasis\.opendocument\.spreadsheet)" , RegexOptions . Compiled | RegexOptions . IgnoreCase ) ;
49+ private static readonly Regex PresentationPattern = new ( @"^application/(?:vnd\.ms-powerpoint|vnd\.openxmlformats-officedocument\.presentationml\.|vnd\.oasis\.opendocument\.presentation)" , RegexOptions . Compiled | RegexOptions . IgnoreCase ) ;
50+ private static readonly Regex CalendarPattern = new ( @"^(?:text/calendar|application/ics)$" , RegexOptions . Compiled | RegexOptions . IgnoreCase ) ;
51+ private static readonly Regex EmailPattern = new ( @"^(?:message/(?:rfc822|global)|application/(?:mbox|x-msmessage))" , RegexOptions . Compiled | RegexOptions . IgnoreCase ) ;
52+
53+ public static MimeTypeCategory GetMimeCategory ( string mime )
54+ {
55+ if ( string . IsNullOrWhiteSpace ( mime ) )
56+ return MimeTypeCategory . Unknown ;
57+
58+ var m = mime . ToLowerInvariant ( ) ;
59+
60+ // Primary types
61+ if ( m . StartsWith ( "video/" ) ) return MimeTypeCategory . Video ;
62+ if ( m . StartsWith ( "audio/" ) ) return MimeTypeCategory . Audio ;
63+ if ( m . StartsWith ( "image/" ) ) return MimeTypeCategory . Image ;
64+ if ( m . StartsWith ( "text/" ) ) return MimeTypeCategory . Text ;
65+ if ( m . StartsWith ( "font/" ) ) return MimeTypeCategory . Font ;
66+ if ( m . StartsWith ( "model/" ) ) return MimeTypeCategory . Model ;
67+
68+ // Document types
69+ if ( m == "application/pdf" ) return MimeTypeCategory . Pdf ;
70+ if ( WordPattern . IsMatch ( m ) ) return MimeTypeCategory . Document ;
71+ if ( SpreadsheetPattern . IsMatch ( m ) ) return MimeTypeCategory . Spreadsheet ;
72+ if ( PresentationPattern . IsMatch ( m ) ) return MimeTypeCategory . Presentation ;
73+
74+ // Data formats
75+ if ( JsonPattern . IsMatch ( m ) ) return MimeTypeCategory . Json ;
76+ if ( XmlPattern . IsMatch ( m ) ) return MimeTypeCategory . Xml ;
77+
78+ // Special types
79+ if ( ArchivePattern . IsMatch ( m ) ) return MimeTypeCategory . Archive ;
80+ if ( ExecutablePattern . IsMatch ( m ) ) return MimeTypeCategory . Executable ;
81+ if ( CertificatePattern . IsMatch ( m ) ) return MimeTypeCategory . Certificate ;
82+ if ( CalendarPattern . IsMatch ( m ) ) return MimeTypeCategory . Calendar ;
83+ if ( EmailPattern . IsMatch ( m ) ) return MimeTypeCategory . Email ;
84+
85+ // Generic application type
86+ return m . StartsWith ( "application/" ) ? MimeTypeCategory . Document : MimeTypeCategory . Unknown ;
87+ }
88+
89+ public static bool IsVideo ( string mime ) => GetMimeCategory ( mime ) == MimeTypeCategory . Video ;
90+ public static bool IsAudio ( string mime ) => GetMimeCategory ( mime ) == MimeTypeCategory . Audio ;
91+ public static bool IsImage ( string mime ) => GetMimeCategory ( mime ) == MimeTypeCategory . Image ;
92+ public static bool IsDocument ( string mime ) => GetMimeCategory ( mime ) == MimeTypeCategory . Document ;
93+ public static bool IsPdf ( string mime ) => GetMimeCategory ( mime ) == MimeTypeCategory . Pdf ;
94+ public static bool IsArchive ( string mime ) => GetMimeCategory ( mime ) == MimeTypeCategory . Archive ;
95+
96+ public static bool IsText ( string mime ) => GetMimeCategory ( mime ) == MimeTypeCategory . Text ;
97+ public static bool IsJson ( string mime ) => GetMimeCategory ( mime ) == MimeTypeCategory . Json ;
98+ public static bool IsXml ( string mime ) => GetMimeCategory ( mime ) == MimeTypeCategory . Xml ;
99+ public static bool IsFont ( string mime ) => GetMimeCategory ( mime ) == MimeTypeCategory . Font ;
100+ public static bool IsModel ( string mime ) => GetMimeCategory ( mime ) == MimeTypeCategory . Model ;
101+ public static bool IsExecutable ( string mime ) => GetMimeCategory ( mime ) == MimeTypeCategory . Executable ;
102+ public static bool IsCertificate ( string mime ) => GetMimeCategory ( mime ) == MimeTypeCategory . Certificate ;
103+ public static bool IsSpreadsheet ( string mime ) => GetMimeCategory ( mime ) == MimeTypeCategory . Spreadsheet ;
104+ public static bool IsPresentation ( string mime ) => GetMimeCategory ( mime ) == MimeTypeCategory . Presentation ;
105+ public static bool IsCalendar ( string mime ) => GetMimeCategory ( mime ) == MimeTypeCategory . Calendar ;
106+ public static bool IsEmail ( string mime ) => GetMimeCategory ( mime ) == MimeTypeCategory . Email ;
38107}
0 commit comments