You need to change your timestamp function to this, my change in bold:

(defn add-timestamp [filename]
 (let [ext-position (.lastIndexOf *(:filename 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))))

Do you see why?  The original error you got was telling you that: 
.lasIndexOf is not a method you can call on (what was at the time) a 
clojure map.  Now you are extracting the filename from upload map by using 
the fact that maps are functions of their keys.  .lastIndexOf will now 
operate on a string successfully.

Best,
Jarrod

On Saturday, January 25, 2014 12:36:45 AM UTC-5, The Dude (Abides) wrote:
>
> Hi Jarrod, you're exactly right, filename feeds the entire map as:
>
> {:size 17401, :tempfile #, :content-type "image/jpeg", :filename 
> "AngryBaby4.jpg"}
>
> How can feed it just the :filename portion of the map as string to the 
> add-timestamp function? Checking that link right now.
>
> On Friday, January 24, 2014 9:05:35 PM UTC-8, Jarrod Swart wrote:
>>
>> Well that isn't quite what I meant.  In that case you are just casting 
>> what is likely the map of upload data to a string.
>>
>> Try this:
>>
>> (defn handle-upload [filename]
>>    (str filename))
>>
>> Why?  This will show you what type of data you receiving on upload.  My 
>> guess is that it is a map containing all the data about the file upload.
>>
>> Following this: 
>> http://www.luminusweb.net/docs/static_resources.md#handling_file_uploads 
>> should 
>> help.
>>
>> In short: you still don't have the right kind of data going into your 
>> function.  Change your handle-upload to the above and verify that you are 
>> getting the filename alone in your (let [filename (...)] ...) binding.
>>
>> Hope that helps.
>>
>> On Friday, January 24, 2014 11:28:00 PM UTC-5, The Dude (Abides) wrote:
>>>
>>> Hi Jarrod, I tried changing filename to string as follows 
>>>
>>> (defn handle-upload [filename]
>>>  (upload-file (gallery-path) (add-timestamp (str filename)))
>>>  (resp/redirect "/upload"))
>>>
>>> and still got an error as:
>>>
>>> java.lang.NullPointerException
>>>
>>>
>>> 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.
>>>
>>

-- 
-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to