99using System . Collections . Generic ;
1010using System . Drawing . Imaging ;
1111using System . IO ;
12+ using System . Linq ;
1213using System . Text ;
1314using System . Windows . Media . Imaging ;
1415using System . Xml ;
@@ -20,6 +21,24 @@ namespace MediaExtractor
2021 /// </summary>
2122 public class ExtractorItem
2223 {
24+ /// <summary>
25+ /// Default file endings that are previewed as text
26+ /// </summary>
27+ public const string FALLBACK_TEXT_EXTENTIONS = "asc,bas,bat,c,cfg,cmd,cpp,cs,css,csv,h,hex,htm,html,inc,inf,info,ini,java,js,json,kt,ktm,kts,latex,less,lisp,log,lst,lua,markdown,md,me,meta,mf,p,pas,php,pl,pp,ps,ps1,psm1,py,r,rb,readme,reg,rs,rst,sh,sln,sql,sty,tcl,tex,ts,tsx,txt,vb,vba,vbs,yaml,yml" ;
28+ /// <summary>
29+ /// Default file endings that are previewed as image
30+ /// </summary>
31+ public const string FALLBACK_IMAGE_EXTENTIONS = "jpg,jpeg,png,wmf,emf,gif,bmp,ico,wdp" ;
32+ /// <summary>
33+ /// Default file endings that are previewed as XML
34+ /// </summary>
35+ public const string FALLBACK_XML_EXTENTIONS = "xml,manifest,rels,xhtml,xaml,svg,pom,dtd,xsd,x3d,collada,cdxml,config,nuspec,graphml" ;
36+ private static readonly char [ ] EXT_SPLITTERS = new char [ ] { ',' , ';' , ' ' , '.' , '/' , '\\ ' , '|' } ;
37+
38+ private static List < string > imageExtensions ;
39+ private static List < string > textExtensions ;
40+ private static List < string > xmlExtensions ;
41+
2342 /// <summary>
2443 /// Enum to define the coarse file type of the entry
2544 /// </summary>
@@ -88,7 +107,7 @@ public BitmapImage Image
88107 {
89108 get
90109 {
91- if ( image == null && initialized == false )
110+ if ( image == null && ! initialized )
92111 {
93112 CreateImage ( true ) ;
94113 initialized = true ;
@@ -104,7 +123,7 @@ public string GenericText
104123 {
105124 get
106125 {
107- if ( genericText == null && initialized == false )
126+ if ( genericText == null && ! initialized )
108127 {
109128 if ( ItemType == Type . Text )
110129 {
@@ -191,90 +210,19 @@ public ExtractorItem(string fileName, MemoryStream stream, bool createFile, stri
191210 private static Type GetExtensionType ( string extension )
192211 {
193212 string ext = extension . ToLower ( ) ;
194- switch ( ext )
213+ if ( textExtensions . Contains ( ext ) )
195214 {
196- case "asc" :
197- case "bas" :
198- case "bat" :
199- case "c" :
200- case "cmd" :
201- case "config" :
202- case "cpp" :
203- case "cs" :
204- case "css" :
205- case "h" :
206- case "hex" :
207- case "htm" :
208- case "html" :
209- case "inc" :
210- case "ini" :
211- case "java" :
212- case "js" :
213- case "json" :
214- case "kt" :
215- case "ktm" :
216- case "kts" :
217- case "latex" :
218- case "lisp" :
219- case "log" :
220- case "lst" :
221- case "lua" :
222- case "md" :
223- case "me" :
224- case "mf" :
225- case "p" :
226- case "pas" :
227- case "php" :
228- case "pl" :
229- case "pp" :
230- case "ps" :
231- case "py" :
232- case "r" :
233- case "rb" :
234- case "readme" :
235- case "reg" :
236- case "rs" :
237- case "rst" :
238- case "sh" :
239- case "sql" :
240- case "sty" :
241- case "tcl" :
242- case "tex" :
243- case "ts" :
244- case "tsx" :
245- case "txt" :
246- case "vb" :
247- case "vba" :
248- case "vbs" :
249- case "yaml" :
250- case "yml" :
251- return Type . Text ;
215+ return Type . Text ;
252216 }
253- switch ( ext )
217+ if ( xmlExtensions . Contains ( ext ) )
254218 {
255- case "jpg" :
256- case "jpeg" :
257- case "png" :
258- case "wmf" :
259- case "emf" :
260- case "gif" :
261- case "bmp" :
262- case "ico" :
263- return Type . Image ;
219+ return Type . Xml ;
264220 }
265- switch ( ext )
221+ if ( imageExtensions . Contains ( ext ) )
266222 {
267- case "xml" :
268- case "rels" :
269- case "xhtml" :
270- case "svg" :
271- case "x3d" :
272- case "collada" :
273- case "graphml" :
274- return Type . Text ;
275- default :
276- return Type . Other ;
223+ return Type . Image ;
277224 }
225+ return Type . Other ;
278226 }
279227
280228 /// <summary>
@@ -304,7 +252,7 @@ public void CreateText()
304252 public void CreateXml ( )
305253 {
306254 CreateText ( ) ;
307- if ( ValidGenericText == false )
255+ if ( ! ValidGenericText )
308256 {
309257 return ;
310258 }
@@ -383,5 +331,45 @@ public void CreateImage(bool retry)
383331 }
384332 }
385333
334+ /// <summary>
335+ /// Method to create valid extensions for the previews
336+ /// </summary>
337+ /// <param name="texts">Raw string of separated text extensions</param>
338+ /// <param name="images">Raw string of separated image extensions</param>
339+ /// <param name="xml">Raw string of separated XML extensions</param>
340+ /// <returns>True if all extensions could be resolved, otherwise false</returns>
341+ public static bool GetExtensions ( string texts , string images , string xml )
342+ {
343+ try
344+ {
345+ textExtensions = SplitExtensions ( texts ) ;
346+ imageExtensions = SplitExtensions ( images ) ;
347+ xmlExtensions = SplitExtensions ( xml ) ;
348+ return true ;
349+ }
350+ catch
351+ {
352+ textExtensions = SplitExtensions ( FALLBACK_TEXT_EXTENTIONS ) ;
353+ imageExtensions = SplitExtensions ( FALLBACK_IMAGE_EXTENTIONS ) ;
354+ xmlExtensions = SplitExtensions ( FALLBACK_XML_EXTENTIONS ) ;
355+ return false ;
356+ }
357+ }
358+
359+ /// <summary>
360+ /// Method to split a raw string of file extensions into a list
361+ /// </summary>
362+ /// <param name="input">input string</param>
363+ /// <returns>List of lowercase, distinct file extensions</returns>
364+ private static List < string > SplitExtensions ( string input )
365+ {
366+ if ( string . IsNullOrEmpty ( input ) )
367+ {
368+ throw new ArgumentException ( ) ;
369+ }
370+ string [ ] split = input . ToLower ( ) . Split ( EXT_SPLITTERS , StringSplitOptions . RemoveEmptyEntries ) ;
371+ return split . Distinct ( ) . ToList ( ) ;
372+ }
373+
386374 }
387375}
0 commit comments