Permalink
Browse files

first commit

  • Loading branch information...
0 parents commit b174bdb5a7727f97c695920b0eadb614d2bb1422 @daleharvey committed Aug 8, 2010
Showing with 393 additions and 0 deletions.
  1. +109 −0 docroot/client.dropup.js
  2. +35 −0 docroot/img.html
  3. +16 −0 docroot/index.html
  4. +63 −0 docroot/screen.css
  5. +131 −0 node.dropup.js
  6. +28 −0 node.routes.js
  7. +11 −0 start.sh
@@ -0,0 +1,109 @@
+var DropUp = (function() {
+
+ var target = document.getElementById("target");
+
+ function startUpload(file, bin, li, desc, progress) {
+
+ var xhr = new XMLHttpRequest(),
+ upload = xhr.upload;
+
+ upload.addEventListener("progress", function(e) {
+ if (e.lengthComputable) {
+ var percentage = Math.round((e.loaded * 100) / e.total);
+ if (percentage < 100) {
+ progress.style.width = percentage + "px";
+ }
+ }
+ }, false);
+
+ upload.addEventListener("load", function(event) {
+ li.className = "loaded";
+ console.log("f", xhr.responseText);
+ }, false);
+
+ upload.addEventListener("error", function (error) {
+ console.log("error: " + error.code);
+ }, false);
+
+ xhr.onload = function(event) {
+ desc.innerHTML = "<a href='/" + xhr.responseText + ".html'>" +
+ xhr.responseText + "</a>";
+ };
+
+ xhr.open("POST", "/upload");
+ xhr.overrideMimeType('text/plain; charset=x-user-defined-binary');
+ xhr.sendAsBinary(bin)
+ };
+
+ function fileLoaded(event) {
+
+ var li = document.createElement('li'),
+ div = document.createElement('div'),
+ img = document.createElement('img'),
+ loading = document.createElement('div'),
+ progress = document.createElement('div'),
+ desc = document.createElement('p'),
+ file = event.target.file,
+ getBinaryDataReader = new FileReader();
+
+ li.className = "uploading";
+ loading.className = "loading";
+ div.className = "wrapper";
+
+ desc.innerHTML = "uploading...";
+
+ img.src = event.target.result;
+ div.appendChild(img);
+ li.appendChild(div);
+ li.appendChild(loading);
+ li.appendChild(desc);
+ loading.appendChild(progress);
+ target.appendChild(li);
+
+ getBinaryDataReader.addEventListener("loadend", function(evt) {
+ startUpload(file, evt.target.result, li, desc, progress);
+ }, false);
+ getBinaryDataReader.readAsBinaryString(file);
+ };
+
+ function drop(e) {
+
+ var i, len, files;
+
+ e.stopPropagation();
+ e.preventDefault();
+
+ files = e.dataTransfer.files;
+
+ for (i = 0; i < files.length; i++) {
+ if(files[i].size < 1048576) {
+ reader = new FileReader();
+ reader.index = i;
+ reader.file = files[i];
+
+ reader.addEventListener("loadend", fileLoaded, false);
+ reader.readAsDataURL(files[i]);
+ } else {
+ alert("file is too big, needs to be below 1mb");
+ }
+ }
+ };
+
+ function doNothing(e) {
+ e.stopPropagation();
+ e.preventDefault();
+ };
+
+ function init() {
+ document.addEventListener("dragenter", doNothing, false);
+ document.addEventListener("dragover", doNothing, false);
+ document.addEventListener("drop", drop, false);
+ };
+
+ return {
+ "init":init
+ };
+})();
+
+
+DropUp.init();
@@ -0,0 +1,35 @@
+<!DOCTYPE html>
+<html>
+ <head>
+ <meta charset="utf-8" />
+ <title>dropup.net</title>
+ <link rel="stylesheet" href="/screen.css" />
+ </head>
+
+ <body>
+
+ <div style="margin:10px;overflow:auto;">
+
+ <a href="<%= imgSrc %>" style="float:left">View Original</a>
+
+ <div class="label">
+ <label for="embed">embed:</label>
+ <input type="text" id="embed" />
+ </div>
+
+ </div>
+
+ <hr />
+
+ <div style="margin-left:10px;">
+ <img src="<%= imgSrc %>" />
+ </div>
+
+ <script>
+ document.getElementById("embed").setAttribute("value",
+ "<img src=\"" + document.location.protocol + "//" + document.location.host +
+ "<%= imgSrc %>\" />");
+ </script>
+
+ </body>
+</html>
@@ -0,0 +1,16 @@
+<!DOCTYPE html>
+<html>
+ <head>
+ <meta charset="utf-8" />
+ <title>dropup</title>
+ <link rel="stylesheet" href="/screen.css" />
+ </head>
+
+ <body>
+
+ <ul id="target"></ul>
+
+ <script src="client.dropup.js"></script>
+
+ </body>
+</html>
@@ -0,0 +1,63 @@
+* {
+ margin:0px;
+ padding:0px;
+ border:0px;
+}
+html, body {
+ height:100%;
+}
+body {
+ font:14px/1.7 Verdana,Tahoma,sans-serif;
+ background:#000;
+ /*background-image: -moz-radial-gradient(center 0deg, circle cover, #150e5b 0%, #000 100%);*/
+ color:#eee;
+}
+ul {
+ list-style-type:none;
+}
+.uploading, .loaded {
+ border:1px solid #666;
+ padding:5px;
+ background:#111;
+ float:left;
+ margin:5px;
+ position:relative;
+}
+.uploading img, .loaded img {
+ max-height:200px;
+ max-width:300px;
+ opacity: 0.1;
+}
+
+.wrapper {
+ text-align:center;
+ width:300px;
+ height: 200px;
+ overflow:hidden;
+ }
+.loading { background:#000; width: 280px; margin:0px 10px;
+ position:absolute; bottom: 120px; height:5px; }
+
+.loaded img { opacity:1; }
+.loaded .loading { display:none; }
+
+a { text-decoration:none; color:orange; font-size:90%; }
+
+.label {
+ float:left;
+ margin: 0px 10px;
+}
+.label label {
+ display:block;
+ font-size:70%;
+ color:#555;
+}
+.label input {
+ background:#333;
+ color:white;
+ padding: 4px;
+ font-size:70%;
+}
+
+#embed { width: 300px; }
+
@@ -0,0 +1,131 @@
+var http = require("http"),
+ url = require("url"),
+ path = require("path"),
+ fs = require("fs"),
+ sys = require("sys");
+
+var formidable = require('formidable'),
+ routes = require('./node.routes'),
+ ejs = require('ejs');
+
+var Util = {};
+
+Util.inArray = function(item, array) {
+ for (var i = 0, len = array.length; i < len; i++) {
+ if (array[i] === item) {
+ return true;
+ }
+ }
+ return false;
+};
+
+Util.randStr = function() {
+ return Math.floor(Math.random() * 2147483648).toString(36) +
+ (Math.floor(Math.random() * 2147483648) ^
+ (new Date().getTime())).toString(36);
+};
+
+var DropUp = (function() {
+
+ var imgTpl = null,
+ root = process.cwd(),
+ ip = "127.0.0.1",
+ port = 8124;
+
+ function out(req, res) {
+ routes.route(req, res, [
+ ["^/$", function() { serveFile(res, "/index.html"); }],
+ ["^(/[a-z]*.(js|css))$", serveStatic],
+ ["^/upload$", uploadFile],
+ ["^/([a-z0-9]{12}.png.html)$", serveImgPage],
+ ["^/([a-z0-9]{12}).png$", serveImg],
+ ["[\w\W]*", serve404]
+ ]);
+ };
+
+ function serveStatic(req, res, rest) {
+ serveFile(res, rest);
+ };
+
+ function serveImg(req, res) {
+ var filename = path.join(root, "uploads", req.url);
+ fs.readFile(filename, "binary", function(err, file) {
+ serveBin(res, file);
+ });
+ };
+
+ function serveImgPage(req, res, path) {
+ path = path.split(".");
+ serveBin(res, ejs.render(imgTpl, {
+ "locals": {
+ "imgId" : path[0],
+ "imgSrc": path[0] + "." + path[1]
+ }
+ }));
+ };
+
+ function imgExt() {
+ return "png";
+ };
+
+ function serveBin(res, bin) {
+ res.writeHead(200);
+ res.write(bin, "binary");
+ res.end();
+ };
+
+ function isImage(path) {
+ return true;
+ };
+
+ function uploadFile(req, res) {
+
+ var name = Util.randStr() + ".png",
+ dest = path.join(root, "uploads", name),
+ content = '';
+
+ req.setEncoding("binary");
+
+ req.addListener('data', function(chunk) {
+ content += chunk;
+ });
+
+ req.addListener('end', function() {
+ fs.writeFile(dest, content, "binary", function (err) {
+ res.writeHead(200, {'content-type': 'text/plain'});
+ res.end(name);
+ });
+ });
+ };
+
+ function serve404(req, res) {
+ res.writeHead(404, {"Content-Type": "text/plain"});
+ res.write("404 Not Found\n");
+ res.end();
+ };
+
+ function serveFile(res, uri) {
+ var filename = path.join(root, "docroot", uri);
+ fs.readFile(filename, "binary", function(err, file) {
+ res.writeHead(200);
+ res.write(file, "binary");
+ res.end();
+ });
+ };
+
+ function init() {
+
+ var tpl = path.join(root, "docroot", "img.html");
+
+ fs.readFile(tpl, "binary", function(err, file) {
+ imgTpl = file;
+ });
+
+ console.log("Starting Server on http://" + ip + ":" + port + "/");
+ http.createServer(out).listen(port, ip);
+ };
+
+ return {"init": init};
+})();
+
+DropUp.init();
@@ -0,0 +1,28 @@
+
+var sys = require('sys');
+var parseURL = require('url').parse;
+
+var route = function(req, res, urls, passed_args){
+ for (var i=0;i<urls.length;i++){
+ var args = new RegExp(urls[i][0]).exec(parseURL(req.url).pathname);
+ if (args !== null){
+ args.shift();
+ args.unshift(req, res);
+ if (typeof passed_args == 'array')
+ args.concat(passed_args);
+ urls[i][1].apply(this, args);
+ break;
+ }
+ }
+};
+
+var include = function(urls){
+ return function(req, res){
+ route(req, res, urls, Array.prototype.slice.call(arguments, 2));
+ };
+};
+
+exports.route = route;
+exports.include = include
+
+
Oops, something went wrong.

0 comments on commit b174bdb

Please sign in to comment.