-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
85 lines (74 loc) · 2.22 KB
/
server.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
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
const uuid = require('uuid');
const Promise = require('bluebird');
const redis = Promise.promisifyAll(require('redis').createClient({
host: '0.0.0.0',
port: 6378,
db:2,
return_buffers:true
}));
const previewManager = require('./preview-manager');
const express = require('express');
const bodyParser = require('body-parser');
const fileUpload = require('express-fileupload');
const db = require('sqlite');
Promise.resolve()
.then(() => db.open(__dirname + '/db.sqlite', {Promise}))
.catch(err => console.error(error.stack))
.finally(() => app.listen(80))
.finally(() => console.log(`server ready for request on 8000`));
const app = express();
app.use(express.static('.'));
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(fileUpload());
app.post('/image', async (req, res) => {
if (!req.files) res.send('file not send');
const id = uuid();
const image = req.files.image;
const name = req.body.name;
try {
await db.run(`insert into files values('${id}', '${image.name}', '${name}')`);
await redis.hmsetAsync(id, 'data', image.data, 'name', image.name);
} catch (err) {
res.send('fail file save');
}
});
app.get('/image', async (req, res) => {
try {
const reply = await db.all('select * from files');
res.json(reply);
} catch (err) {
res.json(err);
}
});
app.get('/image/:id/:name', async (req, res) => {
try {
const image = await redis.hgetAsync(req.params.id, req.params.name);
const buffer = new Buffer(image,'base64');
res.writeHead(200, {"Content-Type": "image/png"});
res.end(image)
} catch (err) {
res.json(err);
}
});
app.get('/shape/:id', async (req, res) => {
try {
const reply = await db.all(`select * from shapes where id = '${req.params.id}'`);
reply.forEach(item => {
item.points = JSON.parse(item.points);
});
res.json(reply);
} catch (err) {
res.json(err);
}
});
app.post('/shape', async (req, res) => {
const { uuid, detail, points } = req.body;
try {
await previewManager.createPreview(uuid, detail, points);
await db.run(`insert into shapes values('${uuid}', '${detail}', '${JSON.stringify(points)}')`);
res.end();
} catch (err) {
res.status(500).json(err);
}
});