My entire file code is:
(ns pgapp.routes.upload
(:require [compojure.core :refer [defroutes GET POST]]
[pgapp.views.layout :as layout]
[noir.io :refer [upload-file resource-path]]
[noir.session :as session]
[noir.response :as resp]
[noir.util.route :refer [restricted]]
[clojure.java.io :as io]
[ring.util.response :refer [file-response]]
[taoensso.timbre :refer [error]]
[pgapp.models.db :as db]
[clj-time.core :as time]
[clj-time.coerce :as tc]
[pgapp.util
:refer [galleries gallery-path thumb-uri thumb-prefix unique-prefix]])
(:import [java.io File FileInputStream FileOutputStream]
javax.imageio.ImageIO))
(use 'ring.middleware.params
'ring.middleware.multipart-params)
(defn upload-page [info]
(layout/render "upload.html"))
(defn add-timestamp [filename]
(let [ext-position (.lastIndexOf filename ".")
timestamp (tc/to-long (time/now))]
(if (pos? ext-position)
(str (.substring filename 0 ext-position)
"-" timestamp (.substring filename ext-position))
(str filename "-" timestamp))))
(defn handle-upload [filename]
(upload-file (gallery-path) (add-timestamp (str filename)))
(resp/redirect "/upload"))
(defroutes myapp-routes
(GET "/upload" [info] (upload-page {:info info}))
(POST "/upload" [file] (handle-upload file)))
I changed filename to (str filename) in the handle-upload function, but still no dice :(
The :refer [galleries gallery-path thumb-uri thumb-prefix unique-prefix]]) are just references to file paths in util.clj vs hardcoding them.
I got productive with clojure ref routes, sessions, views with selmer and queries either raw or with korma all easy peasy. However this one thing, this file upload issue has thrown me for a loop for 3 days now :) It is the bane of my existence right now :)
If I can get a handle on it, am considering creating a library to make this easier akin to file upload plugins in ruby world like carrier-wave. Thanks for any pointers on what I'm doing wrong with the above code.