Skip to content
This repository has been archived by the owner on Apr 24, 2021. It is now read-only.

Commit

Permalink
replace jmimemagic file-type detection with reflective Java-7 API
Browse files Browse the repository at this point in the history
  • Loading branch information
kumarshantanu committed Dec 10, 2013
1 parent 29a70bc commit 6fc46f1
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 13 deletions.
1 change: 0 additions & 1 deletion project.clj
Expand Up @@ -5,7 +5,6 @@
[com.fifesoft/autocomplete "2.5.0"]
[com.fifesoft/rsyntaxtextarea "2.5.0"]
[compliment "0.0.3"]
[jmimemagic "0.1.2"]
[leiningen "2.3.4"]
[lein-ancient "0.5.4" :exclusions [clj-aws-s3]]
[lein-cljsbuild "1.0.0"]
Expand Down
4 changes: 0 additions & 4 deletions resources/log4j.properties

This file was deleted.

25 changes: 17 additions & 8 deletions src/clojure/nightcode/utils.clj
Expand Up @@ -5,8 +5,7 @@
(:import [java.io File]
[java.util Locale]
[java.util.prefs Preferences]
[javax.swing.tree TreePath]
[net.sf.jmimemagic Magic MagicMatchNotFoundException]))
[javax.swing.tree TreePath]))

; preferences

Expand Down Expand Up @@ -130,12 +129,22 @@
(.isDirectory (io/file parent-path))
(.startsWith child-path (str parent-path File/separator)))))

(defn call-static-method
[^Class klass ^String method-name param-classes & args]
(-> klass
(.getDeclaredMethod method-name
(into-array Class param-classes))
(.invoke nil (into-array Object args))))

(def ^:const max-length (* 1024 1024 10))

(defn is-text-file?
"Returns true if the file is of type text, false otherwise."
[^File file]
(try
(-> (Magic/getMagicMatch file false)
.getMimeType
(.startsWith "text"))
(catch MagicMatchNotFoundException e
nil)))
(if-let [files-klass (Class/forName "java.nio.file.Files")]
(let [path-klass (Class/forName "java.nio.file.Path")]
(-> files-klass
(call-static-method "probeContentType" [path-klass] (.toPath file))
(or "")
(.startsWith "text")))
(< (.length file) max-length)))

0 comments on commit 6fc46f1

Please sign in to comment.