All Downloads are FREE. Search and download functionalities are using the official Maven repository.

webapp.upload-webapp.venice Maven / Gradle / Ivy

The newest version!
;; -----------------------------------------------------------------------------
;; Demo REST Service
;; -----------------------------------------------------------------------------
;;

(load-module :tomcat ['tomcat :as 'tc])
(load-module :ring)



; ensure the Tomcat libs are on the classpath
(tc/check-required-libs)


;; -----------------------------------------------------------------------------
;; Ring handler
;; -----------------------------------------------------------------------------

(defn upload-file [dir request]
  (let [parts (ring-multipart/parts request)]
    ;; upload action: save all parts to the specified directory
    (doseq [p parts] (ring-multipart/save-part dir p true))
    { :status 200
      :headers { "Content-Type" "text/plain; charset=utf-8" }
      :body "File uploaded!" }))



;; -----------------------------------------------------------------------------
;; Middleware configuration
;; -----------------------------------------------------------------------------

(def landing-dir (io/file "/Users/juerg/Desktop/venice/tmp"))

(def routes [[:post "/upload"  (partial upload-file landing-dir)] ])


(defn upload-servlet []
  (ring/create-servlet (-> (ring/match-routes routes)     ; >--+
                                                          ;    |
                           (ring-mw/mw-dump-response)     ; ^  |
                           (ring-mw/mw-dump-request)      ; |  |
                           (ring-mw/mw-debug :on))))      ; +--+


;; Note: Tomcat's standard max post size is 2MB. A :max-post-size value of -1 
;;       specifies a indefinite upload size
(def tomcat-opts { :await?         false  ;; do not block - return after start
                   :base-dir       "."
                   :port           8080 
                   :max-post-size  -1 })

;; start Tomcat (wires Tomcat with the ring servlets)
(let [server (tc/start [ [ (upload-servlet) 
                           { :name                 "upload-servlet"  
                             :mapping              "/upload"
                             ;; options required for a file upload servlet
                             :file-upload          true
                             :location             "/Users/juerg/Desktop/venice/tmp"
                             :max-file-size        10_485_760  ;; 10MB
                             :max-request-size     10_485_760  ;; 10MB
                             :file-size-threshold  -1 } ] ]
                       tomcat-opts) ]
  (defn stop [] (tc/shutdown server)))


;; -----------------------------------------------------------------------------

(println "Tomcat started on port ~(:port tomcat-opts).")
(println "Stop it by calling:  (stop)")



;; -----------------------------------------------------------------------------
;; Venice HTTP Client examples
;; -----------------------------------------------------------------------------

;; Upload
(comment
  ;; run this Http client in another REPL.
  (do
    (load-module :http-client-j8 ['http-client-j8 :as 'hc])
    
    (let [response (hc/upload-file
                        (io/file "/Users/juerg/Desktop/image.png") 
                        "http://localhost:8080/upload" 
                        :headers { "Accept" "text/plain" }
                        :debug true)
          status   (:http-status response)]
      (println "Status:" status)))
)

;; Upload
(comment
  ;; run this Http client in another REPL.
  (do
    (load-module :http-client-j8 ['http-client-j8 :as 'hc])
    
    (let [response (hc/upload-multipart
                        { "Part-1" "xxxxxxxxxxx"
                          "Part-2" (io/file "/Users/juerg/Desktop/image.png") 
                          "Part-3" { :mimetype  "application/x-www-form-urlencoded"
                                     :charset   :utf-8
                                     :data      "color=blue" }
                          "Part-4" { :filename  "data.xml"
                                     :mimetype  "application/xml"
                                     :charset   :utf-8
                                     :data      "foo" } }
                        "http://localhost:8080/upload" 
                        :headers { "Accept" "text/plain" }
                        :debug true)
          status   (:http-status response)]
      (println "Status:" status)))
)




© 2015 - 2024 Weber Informatics LLC | Privacy Policy