-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
59 lines (53 loc) · 1.56 KB
/
app.js
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
var express = require("express");
var query = require("./models/image.js");
var app = express();
var fs = require("fs");
var expressLayouts = require('express-ejs-layouts')
var image_dir = __dirname + "/public/img/";
app.configure(function(){
app.use(express.limit('5mb'));
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
app.set('layout', 'layout');
app.use(expressLayouts);
app.use(express.bodyParser());
app.use(app.router);
app.use(express.static(__dirname + '/public'));
});
app.get("/", function(req, res) {
res.render("index.ejs");
});
app.post("/upload", function(req, res) {
var data = req.body.data;
var contentType = req.body.contentType;
var name = req.body.name;
var buf = new Buffer(data, "base64");
if (!contentType && name) {
contentType = "application/octet-stream";
}
var image = new query.Image(name, contentType);
query.addImage(image, function(img) {
fs.writeFile(image_dir + img.lookup, buf, function() {
res.send(img.lookup);
});
});
});
app.get('/view/:id', function(req, res) {
query.getImage(req.params.id, function(img) {
res.render("view.ejs", { image: img });
});
});
app.get("/img/:id", function(req, res, next) {
query.getImage(req.params.id, function(img) {
if (img) {
img.addHit(function() {
res.contentType(img.contentType);
next();
});
}
else {
res.send("Not found", 404);
}
});
});
app.listen(3001);