hi I'm currently working through a number of issues with a project that is acting very differently when compiled. One issue is that I have a route that I send a GET request to with a folder name as the :params. In dev mode it returns a list of strings that are the name of files inside that folder as an array. However once I compile the the project I get back an empty array. the route looks like this
["/folder-files" {:get (fn [] (let [folder (:img (:params ))] (response/ok (->> folder http://clojure.java.io/file file-seq (filter #(.isFile %)) (map #(.getName %)))) )) }] n.b there are single underscores as inputs in the project but they are being formatted out, also I'm still using immutant for now, thanks!
could it be to do with the path inside the jar? In the request I pass "resources/public/img" + a key to the folder name
@jcb when you use it like this, you assume that the resources
folder is directly in the folder where you run the JAR.
When you build an uberjar, resources folder isn't really there, but all its content is directly at the root of the jar.
However, even if the resources folder was there you wouldn't be able to use <http://clojure.java.io/file
;|clojure.java.io/file`;>
instead you should look at <http://clojure.java.io/resource|clojure.java.io/resource>
ok, thanks, that interesting. I was really hoping to create a list of files in a folder whereas that seems to rely on being given a specific file name
I am missing a few pieces - http://clojure-doc.org/articles/cookbooks/files_and_directories.html#get-a-list-of-the-files-and-dirs-in-a-given-directory - uses io/file for this purpose
Yes, that's correct as long as those files are somewhere on the file system.
You could also wrap io/resource
with io/file
: (io/file (io/resource "myresource.txt"))
(EDIT: that might not work ...)
that's my problem, I don't have the file names yet. I want to create a list of files within a given directory. apologies if I'm missing something obvious, I appreciate the help
Yeah, I know 🙂. silly example - I meant that myresource.txt
could also be a folder or whatever.
It seems that io/file doesn't play well with io/resource; especially if it's a folder you want to list with file-seq
😞
I couldn't find anything better than this: https://stackoverflow.com/questions/23139331/how-to-get-list-of-classpath-resources-in-nested-jar
oh that's a shame, it was working very nicely until now! thank you
I saw that and didn't really understand if it would help
I was considering this https://github.com/xsc/cpath-clj but it seems untested beyond jdk8
I don't see why it shouldn't work but the easiest think to do is to try it.
I'm on it, however reading the source it seems to be doing something very similar with io/file anyway
is this really such an odd thing to be doing? I would have thought it would come up more often
I guess "listing a dir" is uncommon - people usually refer to a file which should work fine
cpath isn't a magic bullet - java.lang.IllegalArgumentException: No implementation of method: :child-resources of protocol: #'cpath-clj.core/ResourceLookup found for class: nil
@jcb This could work although might require some polishing
In particular you get JarEntry
from the (->> jar-file .entries
) therefore I just call .getName
- the other list-files
implementation (which could work in the REPL) returns java.io.File instances right now
oh amazing, thanks so much for taking the time to get stuck into this!
does this work in dev too? -sorry "you can take a horse to water..." etc