luminus

jcb 2019-07-19T11:11:16.094400Z

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

jcb 2019-07-19T11:11:23.094600Z

["/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!

jcb 2019-07-19T11:32:39.096400Z

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

jumar 2019-07-19T11:56:02.098700Z

@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>

jcb 2019-07-19T12:13:34.100300Z

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

jcb 2019-07-19T12:14:35.100700Z

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

jumar 2019-07-19T12:33:26.101700Z

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 ...)

jcb 2019-07-19T12:35:56.103400Z

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

jumar 2019-07-19T12:37:48.104500Z

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 😞

jumar 2019-07-19T12:42:44.105500Z

I couldn't find anything better than this: https://stackoverflow.com/questions/23139331/how-to-get-list-of-classpath-resources-in-nested-jar

jcb 2019-07-19T12:43:10.105700Z

oh that's a shame, it was working very nicely until now! thank you

jcb 2019-07-19T12:44:12.106200Z

I saw that and didn't really understand if it would help

jcb 2019-07-19T12:49:13.107100Z

I was considering this https://github.com/xsc/cpath-clj but it seems untested beyond jdk8

jumar 2019-07-19T12:53:07.107700Z

I don't see why it shouldn't work but the easiest think to do is to try it.

jcb 2019-07-19T12:54:03.108700Z

I'm on it, however reading the source it seems to be doing something very similar with io/file anyway

jcb 2019-07-19T12:56:47.109400Z

is this really such an odd thing to be doing? I would have thought it would come up more often

jumar 2019-07-19T13:02:23.110100Z

I guess "listing a dir" is uncommon - people usually refer to a file which should work fine

jcb 2019-07-19T13:16:55.111Z

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

jumar 2019-07-19T13:53:56.111400Z

@jcb This could work although might require some polishing

😱 1
jumar 2019-07-19T13:54:04.111500Z

jumar 2019-07-19T13:56:11.113100Z

In particular you get JarEntry from the (-&gt;&gt; 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

jcb 2019-07-19T14:11:15.113900Z

oh amazing, thanks so much for taking the time to get stuck into this!

jcb 2019-07-19T14:11:48.114300Z

does this work in dev too? -sorry "you can take a horse to water..." etc