Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

io/resource-path returns semi-URL as string #115

Open
TGThorax opened this issue Apr 6, 2016 · 0 comments
Open

io/resource-path returns semi-URL as string #115

TGThorax opened this issue Apr 6, 2016 · 0 comments

Comments

@TGThorax
Copy link

TGThorax commented Apr 6, 2016

You might already know this, and this might even be intended behavior, but here I go:
The noir.io/resource-path function returns a (semi-)URL path to the resource given, since clojure.java.io/resource returns a URL. This can become a problem when you don't know the implementation since characters can be encoded (like spaces -> %20) in the path. Therefore consider

  1. (if it is intended behavior) mentioning in the doc something about the URL origin so people can make a URI/URL out of it again before trying to get a file,
  2. (not intended) changing the implementation to for example (.. path toURI getPath) instead of just (.getPath path) since the URI.getPath() will decode and URL will not.

In particular the inconsistency is apparent using following code

(require '[clojure.java.io :as jio] '[noir.io :as io] '[clojure.string :as str])

(let [a (io/resource-path) ;;=> "/data/.../Pragmatic%20Clojure/.../public/"
      b (jio/resource "public/") ;;=> #object[java.net.URL 0x58c7bfe0 "/data/../Pragmatic%20Clojure/.../public/"]
      c (io/get-resource "")] ; the normal way, returns URL as well
  (map #(.exists (jio/file %)) [a (str/replace a "%20" " ") b c])
) ;;=> (false true true true)

It's important to note that the first one fails because the file can't be found due to the encoded chars.

Of course files should be retrieved using get-resource, so that's my reason for suspicion that it may be intended behavior.

@TGThorax TGThorax changed the title resource-path return semi-URL as string resource-path returns semi-URL as string Apr 6, 2016
@TGThorax TGThorax changed the title resource-path returns semi-URL as string io/resource-path returns semi-URL as string Apr 6, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant