Skip to content

Instantly share code, notes, and snippets.

@maxcountryman
Last active December 17, 2015 14:29
Show Gist options
  • Select an option

  • Save maxcountryman/5624689 to your computer and use it in GitHub Desktop.

Select an option

Save maxcountryman/5624689 to your computer and use it in GitHub Desktop.
(ns simpleupload.app
(:require [clasp.clasp :refer [defroute wrap-routes]]
[clojure.data.codec.base64 :as b64]
[clojure.java.io :as io]
[clojure.string :refer [split]]
[ring.middleware.multipart-params :refer [wrap-multipart-params]]
[ring.util.response :refer [response]]))
(def KEY "foo")
(def ALPHANUM (map char (mapcat #(apply range %)
['(48 58) '(65 91) '(97 123)])))
(def WWW-DIR-FMT "/Users/%s/Desktop")
(def UPLOAD-DIR "uploads")
(defn- xor-cipher
[p k]
(map #(char (bit-xor (int %1) (int %2))) p (cycle k)))
(defn- encode-ciphertext
[c]
(String. (b64/encode (.getBytes (apply str c)))))
(defn- decode-ciphertext
[c k]
(xor-cipher (map char (b64/decode (.getBytes c))) k))
(defn- decode-address
[a & {:keys [k] :or {k KEY}}]
(let [c ((split a #"\@") 0)]
(apply str (decode-ciphertext c k))))
(defn- safe-filename
[file]
(let [ext ((split (:filename file) #"\.") 1)
filename (apply str (take 10 (repeatedly #(rand-nth ALPHANUM))))]
(str filename "." ext)))
(defn- handle-mail
[req]
(let [user (decode-address (get-in req [:multipart-params "recipient"]))
www-dir (io/file (format WWW-DIR-FMT user))
upload-dir (io/file www-dir "uploads")
files (filter #(not (nil? (:tempfile %)))
(vals (:multipart-params req)))]
(when (.exists www-dir) ;; make sure the dir exists
(when-not (.exists upload-dir)
(.mkdir upload-dir)) ;; mkdir if not exists
(doseq [file files]
(io/copy (:tempfile file)
(io/file upload-dir (safe-filename file)))))
(response "")))
(defroute mail-receive "/mail/receive" [:post]
handle-mail)
(def create-app
(->
(partial wrap-routes 'simpleupload.app)
wrap-multipart-params))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment