-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
233 lines (209 loc) · 7.94 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
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
import express from "express"
import cors from 'cors'
import dotenv from 'dotenv'
import fileUpload from 'express-fileupload'
import bodyParser from 'body-parser'
import path from 'path'
import {generatePrepData, doUpload} from './prepdata.js'
import moment from "moment"
import fs from 'fs'
import {dbcrud} from './web3db.js'
const app = express()
app.use(cors())
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json())
app.use(fileUpload({createParentPath: true}))
dotenv.config()
const currentDate = new moment()
const port = process.env.PORT || 5000
app.get("/", (req, res) => {
res.send("OK!");
})
app.post('/send', async (req, res) => {
if (!req.files){
return res.status(400).send('No files were uploaded...')
}
const file = req.files.uploadedFile
const path = process.cwd() + '/files/' + String(file.name).replace(/\s/g, '')
console.log(path)
const obj = req.body.signer
console.log(obj)
await file.mv(path, (err) => {
if (err) {
return res.status(500).send(err)
} else {
generatePrepData(path)
.then(async (response) => {
response.signer = obj
console.log(response)
//await doDeal(response)
//fs.unlinkSync(path)
return res.send({
status: 'success',
datetime: moment(),
permissions: "",
link: response.carlink,
response
})
})
.catch((e) => {
return res.send(e)
})
}
})
})
app.get('/upload', async (req, res) => {
if (!req.query.filename){
return res.status(400).send('No files were sent...')
}
const toUpload = req.query.filename
if (toUpload) {
const path = './files/' + toUpload.replace(/\s/g, '')
console.log(path)
if (fs.existsSync(path) && fs.statSync(path).isFile()) {
try {
const ipfsUrl = await doUpload(path)
if(ipfsUrl) {
console.log(`IPFS URL : ${ipfsUrl}`)
res.send({
status: 'uploaded',
url: ipfsUrl
})
}
} catch (e) {
console.error("Something went wrong " + e.name + " " + e.message)
}
} else res.send({status: 'failure'})
} else {
console.error('File was not found...')
res.send({status: 'failure'})
}
})
app.post('/update', async (req, res) => {
console.log(req.body)
try {
if ( req.body) {
const crud = new dbcrud()
//const data = JSON.parse(req.body)
const data = req.body
//const key = Object.keys(data)
if ( data[0].function === 'addFile') {
try {
crud.addFile(
data[1].id,
data[1].owner,
data[1].filename,
data[1].dateTime,
data[1].ipfsurl
)
.then(() => {
console.log(`Record with id: ${data[1].id} created`)
res.send({status: 'success'})
})
.catch((e) => {
console.error(e)
res.send({status: 'error'})
})
} catch (e) {
console.error(e)
res.send({status: 'error'})
}
} else if ( data[0].function === 'getFilesByOwner') {
try {
crud.getFilesByOwner(data[1].owner)
.then((result) => {
console.log(result)
res.send({
status: 'success',
records: result
})
.catch((e) => {
console.error(e)
res.send({status: 'error'})
})
})
} catch (e) {
console.error(e)
res.send({status: 'error'})
}
} else if ( data[0].function === 'deleteFile') {
try {
crud.deleteFile(data[1].id, data[1].ipfsurl)
.then(() => {
res.send({status: 'success'})
})
.catch ((e) => {
console.error(e)
res.send({status: 'error'})
})
} catch (e) {
console.error(e)
res.send({status: 'error'})
}
} else if ( data[0].function === 'giveAccess') {
try {
crud.giveAccess(
data[1].id,
data[1].owner,
data[1].filename,
data[1].ipfsurl,
data[1].username,
data[1].userwallet
)
.then(() => {
console.log(`Record with ${data[1].id} created`)
res.send({status: 'success'})
})
.catch((e) => {
console.error(e)
res.send({status: 'error'})
})
} catch (e) {
console.error(e)
res.send({status: 'error'})
}
} else if ( data[0].function === 'getSharedFiles') {
try {
crud.getSharedFiles(data[1].userwallet)
.then((result) => {
console.log(result)
res.send({
status: 'success',
records: result
})
})
.catch ((e) => {
console.error(e)
res.send({status: 'error'})
})
} catch (e) {
console.error(e)
res.send({status: 'error'})
}
} else if ( data[0].function === 'revokeAccess') {
try {
crud.revokeAccess(data[1].id)
.then(() => {
console.log(`Deleted record ${data[1].id}...`)
res.send({status: 'success'})
})
.catch ((e) => {
console.error(e)
res.send({status: 'error'})
})
} catch (e) {
console.error(e)
res.send({status: 'error'})
}
} else {
throw new Error('Unrecognized command...')
}
} else {
throw new Error('Incorrect post data...')
}
} catch (e) {
console.error (`Something went wrong ${e.name} ${e.message}`)
res.send({status: 'error'})
}
})
app.listen(port, console.log(`App running on port ${port}...`));