Repository navigation
Expand file tree
/
Copy pathserver.js
More file actions
103 lines (93 loc) · 3.17 KB
/
Copy pathserver.js
File metadata and controls
103 lines (93 loc) · 3.17 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
const fs = require('fs');
const express = require('express');
const multer = require('multer');
const bodyParser = require('body-parser');
const ipfsService = require('./ipfs/ipfsService');
const ethereumeStore = require('./storage/ethereum');
const upload = multer({
dest: 'uploads/',
storage: multer.memoryStorage(),
limits: { fileSize: 5 * 1024 * 1024 }, //5 MB limit
storage: multer.diskStorage({
destination: function (req, file, cb) {
const dir = `${__dirname}/uploads`;
cb(null, dir)
},
filename: function (req, file, cb) {
const namespace = 'sagivo'; // TODO: GET NAMESPACE FROM MIDDLEWARE
cb(null, `${namespace}-${file.originalname}`);
}
})
});
module.exports = class Server {
constructor(params = {}) {
this.app = express()
if (params.middleware) {
this.app.use(params.middleware);
}
}
start(params = {}) {
const port = params.port || 3000;
this.app.get('/', (req, res) => res.send('ok'));
this.app.post('/put', upload.single('file'), async (req, res) => {
const { file } = req;
if (!file || !req.body.key) {
if (file) fs.unlinkSync(file.path);
return res.send({error: 'no file or key name was provided'});
}
// TODO: GET NS FROM DB HERE
const ns = 'sagivo';
// ----
if (file) {
const readableStream = fs.createReadStream(file.path);
const ipfsHash = await ipfsService.put(readableStream);
const blockTx = await ethereumeStore.set(ns, req.body.key, ipfsHash);
// TODO: store in other services here
fs.unlinkSync(file.path);
res.json({ ipfsHash, blockTx });
}
});
this.app.get('/pull/:key', async (req, res) => {
if (!req.params.key) {
return res.json({error: 'no key'});
}
// TODO: GET NS FROM DB HERE
const ns = 'sagivo';
// ----
const ipfsHash = await ethereumeStore.get(ns, req.params.key);
if (!ipfsHash.length) return res.json({error: 'no matching namespace/value'});
const fileStream = await ipfsService.pull(ipfsHash);
fileStream.pipe(res);
});
this.app.post('/set', bodyParser.json(), async (req, res) => {
if (!req.body.key || !req.body.value) {
return res.json({error: 'no key or value params'});
}
// TODO: GET NS FROM DB HERE
const ns = 'sagivo';
// ----
const ipfsHash = await ipfsService.set(req.body.value);
const blockTx = await ethereumeStore.set(ns, req.body.key, ipfsHash);
res.json({ ipfsHash, blockTx });
});
this.app.get('/get/:key', async (req, res) => {
if (!req.params.key) {
return res.json({error: 'no key'});
}
// TODO: GET NS FROM DB HERE
const ns = 'sagivo';
// ----
const ipfsHash = await ethereumeStore.get(ns, req.params.key);
if (!ipfsHash.length) return res.json({error: 'no matching namespace/value'});
const val = await ipfsService.get(ipfsHash);
res.send(val);
});
this.server = this.app.listen(port, () => {
console.log(`Example this.app listening on port ${ port }!`);
});
}
async exit() {
await ipfsService.stop();
await this.server.close();
}
}