Created
January 20, 2011 16:33
-
-
Save tgk/788135 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns dragndrop.core | |
(:use ring.adapter.jetty | |
ring.util.response | |
[ring.middleware file multipart-params stacktrace] | |
[compojure core response] | |
[clojure.contrib.io :only [to-byte-array file]] | |
clojure.pprint) | |
(:import java.io.ByteArrayInputStream)) | |
; UUID generator | |
(def numbers [\0 \1 \2 \3 \4 \5 \6 \7 \8 \9]) | |
(def alphabet [\a \b \c \d \e \f \g \h \i \j \k \l \m \n \o \p]) | |
(def Alphabet (map #(Character/toUpperCase %) alphabet)) | |
(def possible-chars (concat numbers alphabet Alphabet)) | |
(defn new-secret [] (apply str (for [i (range 7)] (rand-nth possible-chars)))) | |
; Database and uploading | |
(def db (ref {})) | |
(defn upload-using-request [req] | |
(let [secret (new-secret) | |
data (-> req :body to-byte-array)] | |
(dosync | |
(alter db assoc secret data)) | |
(format "/data/%s" secret))) | |
; Routes | |
(extend-type (Class/forName "[B") | |
Renderable | |
(render [this request] (render (ByteArrayInputStream. this) request))) | |
(defroutes main-routes | |
(GET "/" [] (slurp "static/test.html")) | |
(GET "/data/:id" [id] (@db id)) | |
(POST "/upload" req (upload-using-request req))) | |
; Wrapping | |
(defn wrap-dump-req [app] | |
(fn [req] | |
(spit "req.log" (with-out-str (pprint req))) | |
(app req))) | |
(def app | |
(-> #'main-routes | |
wrap-dump-req | |
wrap-stacktrace | |
(wrap-file "static"))) | |
; Server starter | |
(defn start-server [port] | |
(future (run-jetty (var app) {:port port}))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defproject dragndrop "0.0.1-SNAPSHOT" | |
:description "Simple uploading of single files to a web page." | |
:dependencies [[org.clojure/clojure "1.2.0"] | |
[org.clojure/clojure-contrib "1.2.0"] | |
[ring "0.3.1"] | |
[compojure "0.5.2"] | |
[hiccup "0.2.7"]] | |
:dev-dependencies [[swank-clojure "1.3.0-SNAPSHOT"]]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN"> | |
<html> | |
<head> | |
<title>Uploader</title> | |
<style> | |
#dropbox { | |
width: 228px; | |
height: 300px; | |
margin-left: auto; | |
margin-right: auto; | |
} | |
</style> | |
<script type="text/javascript"> | |
var Engine = Engine || {}; | |
(function(){ | |
var dropbox; | |
var doNothing = function(event) { | |
event.stopPropagation(); | |
event.preventDefault(); | |
}; | |
Engine.setup = function() { | |
dropbox = document.getElementById("dropbox"); | |
//dropbox.addEventListener("dragenter", doNothing, false); | |
//dropbox.addEventListener("dragover", doNothing, false); | |
dropbox.addEventListener("drop", Engine.handleDrop, false); | |
}; | |
Engine.handleDrop = function(event) { | |
var files = event.dataTransfer.files; | |
doNothing(event); | |
for (var i = 0; i < files.length; i++) { | |
var xmlHttpRequest = new XMLHttpRequest(); | |
// Firefox version: | |
xmlHttpRequest.open("POST", "/upload", false); | |
xmlHttpRequest.overrideMimeType('text/plain; charset=x-user-defined-binary'); | |
xmlHttpRequest.sendAsBinary(files[i].getAsBinary()); | |
var dataPage = xmlHttpRequest.responseText; | |
window.location = dataPage; | |
} | |
}; | |
window.addEventListener("load", Engine.setup, false); | |
})(); | |
</script> | |
</head> | |
<body> | |
<div id="dropbox"><img src="arrow.png" /></div> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment