Skip to content

Instantly share code, notes, and snippets.

@malcolmsparks
Created September 6, 2019 20:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save malcolmsparks/1003c1cbb0beaae9886cb04384b9a161 to your computer and use it in GitHub Desktop.
Save malcolmsparks/1003c1cbb0beaae9886cb04384b9a161 to your computer and use it in GitHub Desktop.
;; Copyright © 2014-2017, JUXT LTD.
(ns yada.dev.upload
(:require
[byte-streams :as bs]
[bidi.bidi :refer [RouteProvider tag]]
[bidi.vhosts :refer [uri-for redirect]]
[clojure.tools.logging :refer :all]
[clojure.java.io :as io]
[schema.core :as s]
[manifold.deferred :as d]
[manifold.stream :as stream]
[yada.consume :refer [save-to-file]]
[yada.yada :refer [handler resource]]))
(defn build-routes []
(try
["/upload"
[
["" (redirect ::index)]
["/" (redirect ::index)]
["/post"
(resource
{:id ::index
:methods
{:post
{:consumes "application/octet-stream"
:consumer (fn [ctx _ body-stream]
(let [f (java.io.File/createTempFile "yada" ".tmp" (io/file "/tmp"))]
(infof "Saving to file: %s" f)
(save-to-file
ctx body-stream
f)))
:response (fn [ctx] (format "Thank you, saved upload content to file: %s\n" (:file ctx)))}}})]]]
(catch clojure.lang.ExceptionInfo e
(errorf e (format "Errors: %s" (pr-str (ex-data e))))
(errorf e "Getting exception on upload examples routes")
[true (handler (str e))])
(catch Throwable e
(errorf e "Getting exception on upload examples routes")
[true (handler (str e))])))
(s/defrecord UploadExamples []
RouteProvider
(routes [_] (build-routes)))
(defn new-upload-examples []
(map->UploadExamples {}))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment