Fork me on GitHub
#luminus
<
2019-07-19
>
jcb11:07:16

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

jcb11:07:23

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

jcb11:07:39

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

jumar11:07:02

@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 ;|clojure.java.io/file`;> instead you should look at

jcb12:07:34

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

jumar12:07:26

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

jcb12:07:56

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

jumar12:07:48

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 😞

jcb12:07:10

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

jcb12:07:12

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

jcb12:07:13

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

jumar12:07:07

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

jcb12:07:03

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

jcb12:07:47

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

jumar13:07:23

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

jcb13:07:55

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

jumar13:07:56

@jcb This could work although might require some polishing

😱 4
jumar13:07:11

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

jcb14:07:15

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

jcb14:07:48

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