Clojure File Response

I am currently creating a Clojure database with database backing in Luminus + h2 . I am working on file uploads currently and am stuck at the point of actually extracting the files. While I'm not entirely sure if this is the best way to approach the situation, here's what I've done so far:

I have a route that allows me to upload files. The downloaded files are copied to the / resources / public / Files folder inside my project:

(io/copy actual-file (io/file "resources" "public" "Files" file-name))

      

Also, I store the filename of each of the files inside the database table that are created and processed using SQL.

Then I have a new route that shows all the files that have been downloaded as links (by accessing the database). On the back, the links point the page to a new route "/ file /: filename", which calls the file response function. I hope the links work as "downloads" for the files.

As a first attempt at this work, I copied all the files into the C: / AllFiles folder and did the following:

(defn serve-file [file-name]
  (file-response (str (files-path) File/separator file-name)))

      

where is the path to the files:

(defn files-path [] "/AllFiles")

      

This actually worked for me. However, I want the file I am using to be one of my specific project directory, without having to enter the entire path (i.e. so that it works for everyone using it in ~ / Project-Name / resources / public / files ").

For some reason I cannot get the response file to work like this.

Thank you for your help.

+3


source to share


1 answer


OK, so a couple of ideas (I'm not sure what works best for your situation):

  • you can get the current working directory of a process like this:

(System/getProperty "user.dir")

  • you can change the current working directory like this:

(System/getProperty "user.dir" "/users/lispHK01")



So, you can do something like this:

(def initial-working-path (System/getProperty "user.dir"))

(def my-relative-files-path "foo/bar/wherefileswillbe")

(def files-path
  (str
    initial-working-path
    File/separator
    my-relative-files-path))

      

If you need to update files-path

multiple times, you can use it atom

for that (although the standard Clojure "best practice recommendation": try to find a functional / immutable approach before relying on the atom). The atom is separated by using @

, for example:

user=> (def foo (atom "my/path"))
#'user/foo
user=> @foo
"my/path"
user=> (reset! foo "my/new/path")
"my/new/path"
user=> @foo
"my/new/path"
user=>

      

+1


source







All Articles