diff --git a/src/cryogen_core/classpath_able_io/jar.clj b/src/cryogen_core/classpath_able_io/jar.clj index cee8d63..95883f7 100644 --- a/src/cryogen_core/classpath_able_io/jar.clj +++ b/src/cryogen_core/classpath_able_io/jar.clj @@ -7,11 +7,55 @@ ; You must not remove this notice, or any other, from this software. (ns cryogen-core.classpath-able-io.jar - (:require [cryogen-core.classpath-able-io.type :as type]) + (:require [clojure.java.io :as io] + [clojure.string :as st] + [cryogen-core.classpath-able-io.type :as type] + [cryogen-core.classpath-able-io.fs :as fs]) (:import [java.net URI] - [java.nio.file Paths Files LinkOption])) + [java.nio.file Paths Files FileSystems])) (defn is-from-classpath-jar? [uri ;:- JavaUri ] (= (.getScheme uri) "jar")) + +(defn create-resource + ([virtual-path ;:- VirtualPath + java-path ;:- JavaPath + ] + (let [java-uri (.toUri java-path)] + {:virtual-path virtual-path + :java-uri java-uri + :java-path java-path + :source-type :java-classpath-jar + :resource-type (cond + (Files/isDirectory java-path fs/no-link-option) :dir + (Files/isRegularFile java-path fs/no-link-option) :file + :else :unknown)}))) + +(defn + filesystem-uri + [resource-uri ;:- JavaUri + ] + (URI. (first (st/split (.toString resource-uri) #"!")))) + +(defn init-file-system + [resource-uri ;:- JavaUri + ] + (try + (FileSystems/getFileSystem (filesystem-uri resource-uri)) + (catch Exception e + (FileSystems/newFileSystem (filesystem-uri resource-uri) {})))) + +(defn path-if-exists ;:- JavaPath + [resource-path ;:- VirtualPath + ] + (try + (let [resource-uri (.toURI (io/resource resource-path))] + (when (is-from-classpath-jar? resource-uri) + (init-file-system resource-uri)) + ;; TODO: hier steckt auch eine "from-fs-cp" funktionalität drinne + (when (Files/exists (Paths/get resource-uri) fs/no-link-option) + (Paths/get resource-uri))) + (catch Exception e + nil))) \ No newline at end of file diff --git a/test/cryogen_core/classpath_able_io/jar_test.clj b/test/cryogen_core/classpath_able_io/jar_test.clj index f12f33c..9f4a783 100644 --- a/test/cryogen_core/classpath_able_io/jar_test.clj +++ b/test/cryogen_core/classpath_able_io/jar_test.clj @@ -18,3 +18,10 @@ (is (sut/is-from-classpath-jar? (.toURI (io/resource "dummy")))) ) + +(deftest test-path-if-exists + (is + (sut/path-if-exists "dummy/dummy_from_jar"))) + (is + (= nil + (sut/path-if-exists "not-existing")))