-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathnode.dropup.js
More file actions
141 lines (111 loc) · 3.88 KB
/
node.dropup.js
File metadata and controls
141 lines (111 loc) · 3.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
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'),
mime = require("./mime.js"),
Util = require("./util.js").Util;
var DropUp = (function() {
var imgTpl = null,
root = process.cwd(),
ip = "127.0.0.1",
port = 8124;
function out(req, res) {
if (req.method === "HEAD") {
res.write = function () {};
}
routes.route(req, res, [
["^/$", function() { serveFile(req, res, "/index.html"); }],
["^/drop.png$", function() { serveFile(req, res, "/drop.png"); }],
["^/upload$", uploadFile],
["^/([a-zA-Z0-9-]*.(png|jpg).html)$", serveImgPage],
["^/([a-zA-Z0-9-]*).(png|jpg)$", serveImg],
["[\w\W]*", serveStatic]
]);
};
function serveStatic(req, res, rest) {
serveFile(req, res, req.url);
};
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 serveBin(res, bin) {
res.writeHead(200);
res.write(bin, "binary");
res.end();
};
function isImage(path) {
return true;
};
function imgExt(str) {
return (str[1] + str[2] + str[3]) === "PNG" ? "png" : "jpg";
};
function uploadFile(req, res) {
var content = '';
req.setEncoding("binary");
req.addListener('data', function(chunk) {
content += chunk;
});
req.addListener('end', function() {
var name = Util.randStr() + "." + imgExt(content),
dest = path.join(root, "uploads", name);
fs.writeFile(dest, content, "binary", function (err) {
res.writeHead(200, {'content-type': 'text/plain'});
res.end(name);
});
});
};
function serve503(req, res) {
res.writeHead(503, {"Content-Type": "text/plain"});
res.write("Server Encountered an error\n");
res.end();
};
function serve404(req, res) {
res.writeHead(404, {"Content-Type": "text/plain"});
res.write("404 Not Found\n");
res.end();
};
function serveFile(req, res, uri) {
var filename = path.join(root, "docroot", uri);
path.exists(filename, function (exists) {
if (exists) {
fs.readFile(filename, "binary", function(err, file) {
var headers = {"Content-Type": mime.lookup(filename)};
res.writeHead(200, headers);
res.write(file, "binary");
res.end();
});
} else {
serve404(req, res);
}
});
};
function init() {
process.on('uncaughtException', function (err) {
console.log('Caught exception: ' + err);
});
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();