-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
141 lines (111 loc) · 3.55 KB
/
app.js
File metadata and controls
141 lines (111 loc) · 3.55 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
const { create } = require('ipfs-http-client')
const express = require('express')
const bodyParser = require('body-parser')
const fileUpload = require('express-fileupload')
const fs = require('fs')
const sdk = require('matrix-js-sdk')
// const ipfs = create('/ip4/127.0.0.1/tcp/5001')
const ipfs = create({
url: '/ip4/127.0.0.1/tcp/5001',//need to change to public
emitSelf: true,
pubsub: { emitSelf: true },
config: {
pubsub: { emitSelf: true }
}
})
const app = express()
app.set('view engine', 'ejs')
app.use(bodyParser.urlencoded({ extended: true }))
app.use(fileUpload())
const topic = 'helloAimanzaethbq785gjb'
const translateUint8Array = (data) => {
var textEncoding = require('text-encoding');
var TextDecoder = textEncoding.TextDecoder;
var uint8array = new TextEncoder().encode("¢");
var theOutput = new TextDecoder().decode(data);
return JSON.parse(theOutput)
}
app.get('/', async (req, res) => {
res.render('home')
})
app.post('/subscribe', async (req, res) => {
console.log("SUBSCRIBING")
await ipfs.pubsub.subscribe(
topic,
(msg) => {
console.log('GOT MESSAGE')
// console.log(msg)
console.log(translateUint8Array(msg?.data))
}
)
})
app.post('/sendMessage', async (req, res) => {
console.log("GOING THROUGH FILE")
await ipfs.pubsub.publish(
topic,
JSON.stringify({
thisMessag1: 'Message is received!',
thisMessag10: {
thisMessag1: 'Message is received!',
thisMessag10: [
{
thisMessag1: 'Message is received!',
thisMessag10: 'Message is received!',
},
{
thisMessag1: 'Message is received!',
thisMessag10: 'Message is received!',
},
],
},
})
)
})
app.post('/upload', async (req, res) => {
// console.log("Checkpoint 1")
// console.log(req.body)
const file = req.files.file
const fileName = req.body.filename
const filePath = 'files/' + fileName
// console.log("Checkpoint 2")
// console.log(file)
// console.log(fileName)
// console.log(filePath)
// console.log("Checkpoint 2.1")
file.mv(filePath, async (err) => {
// console.log("Checkpoint 3")
if (err) {
// console.log('Error: failed to download the file')
return res.status(500).send(err)
}
// console.log("Checkpoint 4")
// console.log(fileName)
// console.log(filePath)
const fileHash = await addFile(fileName, filePath)
// console.log("Checkpoint 5")
fs.unlink(filePath, (err) => {
if (err) console.log('Error: failed to unlink the file')
})
// console.log("Checkpoint 6")
res.render('upload', {
fileName, fileHash
})
// console.log("Checkpoint 7")
})
})
const addFile = async (fileName, filePath) => {
// console.log("Checkpoint 4.1")
const file = fs.readFileSync(filePath)
// console.log("Checkpoint 4.2")
// console.log(fileName)
// console.log(file)
const fileAdded = await ipfs.add({ path: fileName, content: file })
// console.log("Checkpoint 4.3")
// console.log(fileAdded)
const fileHash = fileAdded.cid
// console.log("Checkpoint 4.4")
return fileHash
}
app.listen(3000, () => {
console.log('Server started on port 3000, http://localhost:3000')
})