Thibaut Colar avatar Thibaut Colar committed 3a26441

Added method to determine more accurately if a file is text or not.

Comments (0)

Files changed (1)

netColarUtils/fan/FileUtils.fan

 **
 class FileUtils
 {
+  static const Str[] knownTextExts := ["fan", "axon", "htm", "html", "css", "js", "cs",
+  "properties", "props", "md", "txt", "java", "fog", "cpp", "bat", "sh", "h", "xml",
+  "json", "c", "php", "fwt", "fandoc", "log", "csv", "markdown", "rdf", "settings",
+  "mustache", "sql", "patch", "dtd"]
+
+  static const Str[] knownBinExts := ["pod", "zip", "class", "png", "jpg", "jpeg", "gif",
+  "obj", "dll", "exe", "jar", "ico", "rar", "tgz", "tar.gz", "bin", "debug", "pdf", "bmp",
+  "7z"]
+
+
   ** Create a directory along with whatever parent directories are needed
   static Void mkDirs(Uri dir)
   {
       f.create()
     }
   }
-}
+
+  ** Try to determin is a file is text
+  ** If fie size is > maxSize then wil be considered bn automatically
+  static Bool isTextFile(File? f, Int maxSize := 50000)
+  {
+    // sanity checks
+    if(f==null || f.isDir || ! f.exists)
+      return false
+
+    // too big?
+    if(f.size > maxSize) return false
+
+    // Check known common file types for fantom projects
+    if(knownTextExts.contains(f.ext)) return true
+    if(knownBinExts.contains(f.ext)) return false
+
+    // Chek if a mime type is present and it think it's text
+    if (f.mimeType != null && f.mimeType.mediaType == "text") return true
+
+    // alright, at this point we don't know, so we will try to have a look
+    echo("checking $f")
+    in := f.in
+    try
+    {
+       line := in.readLine()
+       // if null line or line too long, probaby not a text file
+       if(line == null || line.size > 300) return false
+       // starts with empty line -> probably text file
+       if(line.size == 0) return true
+       // Still don't know then check if it looks like text
+       count := 0
+       line.each |char| {if(char.isAlphaNum) count++}
+       // if at least 90% alphanum then consider text file
+       return (count==0 || count * 100 / line.size < 90) ? false : true
+    }
+    finally
+    {
+      in.close
+    }
+
+    return false
+  }
+}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.