-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
57 lines (51 loc) · 1.63 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
const express = require('express');
const ipfsClient = require('ipfs-http-client');
const bodyparser = require('body-parser');
const fileUpload = require('express-fileupload');
const fs = require('fs');
const ejs = require('ejs');
const ipfs = ipfsClient({host : 'localhost',port: '5001',protocol:'http'});
const app = express();
app.set('view engine','ejs');
app.use(bodyparser.urlencoded({extended: true}));
app.use(fileUpload());
app.use(express.json());
app.get('/',(req,res)=>{
res.render('home');
});
app.post('/upload-file',(req,res)=>{
try {
let file = null
for (let k in req.files) {
file = req.files[k]
}
if (file!==null) {
const fileName = file.name;
const filePath = 'files/'+fileName;
file.mv(filePath,async(err)=>{
if(err){
console.log("error : while uploading file");
return res.status(500).send(err);
}
const fileHash = await addIpfsFile (fileName,filePath);
fs.unlink(filePath,(err)=>{
if(err) console.log(err);
})
res.render('upload',{fileName,fileHash});
})
} else {
res.status(500).send("no uploaded file");
}
} catch (error) {
res.status(500).send(error);
}
});
const addIpfsFile = async (fileName,filePath)=>{
const file = fs.readFileSync(filePath);
const fileAdded = await ipfs.add({path: fileName,content:file});
const {cid} = fileAdded;
return cid;
}
app.listen(80,()=>{
console.log('Server listening on port 80');
});