fixed some fragile funktions & removed duplicates

This commit is contained in:
lukas 2020-01-16 12:45:18 +01:00
parent d3227b3f55
commit 6ef8f320c3

View file

@ -34,6 +34,8 @@
(def public "resources/public") (def public "resources/public")
(def NoLinkOption (into-array [LinkOption/NOFOLLOW_LINKS]))
(defn path (defn path
"Creates path from given parts, ignore empty elements" "Creates path from given parts, ignore empty elements"
[& path-parts] [& path-parts]
@ -61,12 +63,12 @@
java-path :- JavaPath java-path :- JavaPath
source-type :- SourceType] source-type :- SourceType]
{:short-path short-path {:short-path short-path
:uri (java.net.URI. (str "file://" (.toString java-path))) :uri (.toUri java-path)
:java-path java-path :java-path java-path
:source-type source-type :source-type source-type
:resource-type (cond :resource-type (cond
(Files/isDirectory java-path (into-array [LinkOption/NOFOLLOW_LINKS])) :dir (Files/isDirectory java-path NoLinkOption) :dir
(Files/isRegularFile java-path (into-array [LinkOption/NOFOLLOW_LINKS])) :java-path (Files/isRegularFile java-path NoLinkOption) :java-path
:else :unknown)})) :else :unknown)}))
(s/defn is-file? :- s/Bool (s/defn is-file? :- s/Bool
@ -77,7 +79,7 @@
[resource-path :- ShortPath] [resource-path :- ShortPath]
(try (try
(let [path-from-cp (Paths/get (java.net.URI. (.toString (io/resource resource-path))))] (let [path-from-cp (Paths/get (java.net.URI. (.toString (io/resource resource-path))))]
(when (Files/exists path-from-cp (into-array [LinkOption/NOFOLLOW_LINKS])) (when (Files/exists path-from-cp NoLinkOption)
path-from-cp)) path-from-cp))
(catch Exception e (catch Exception e
nil))) nil)))
@ -87,7 +89,7 @@
resource-path :- ShortPath] resource-path :- ShortPath]
(let [path-from-fs (Paths/get (java.net.URI. (str "file://" fs-prefix resource-path)))] ;with this, you need to give the absolute path (let [path-from-fs (Paths/get (java.net.URI. (str "file://" fs-prefix resource-path)))] ;with this, you need to give the absolute path
(try (try
(when (Files/exists path-from-fs (into-array [LinkOption/NOFOLLOW_LINKS])) (when (Files/exists path-from-fs NoLinkOption)
path-from-fs) path-from-fs)
(catch Exception e (catch Exception e
nil)))) nil))))
@ -205,9 +207,9 @@
fs-prefix fs-prefix
base-path base-path
resource-path)] resource-path)]
(when (Files/isDirectory source-file (into-array [LinkOption/NOFOLLOW_LINKS])) (when (Files/isDirectory source-file NoLinkOption)
(Files/createDirectories target-file (into-array FileAttribute []))) (Files/createDirectories target-file (into-array FileAttribute [])))
(when (Files/isRegularFile source-file (into-array [LinkOption/NOFOLLOW_LINKS])) (when (Files/isRegularFile source-file NoLinkOption)
(Files/copy source-file target-file (into-array StandardCopyOption [StandardCopyOption/COPY_ATTRIBUTES StandardCopyOption/REPLACE_EXISTING])) (Files/copy source-file target-file (into-array StandardCopyOption [StandardCopyOption/COPY_ATTRIBUTES StandardCopyOption/REPLACE_EXISTING]))
)))))) ))))))