-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
234 lines (137 loc) · 4.32 KB
/
index.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
234
var express = require('express');
var cors = require('cors')
const fetch = (...args) => import('node-fetch').then(({ default: fetch }) => fetch(...args));
const multer = require('multer')
var fs = require('fs');
// const upload = multer({ dest: './public/temp/uploads/' })
const storage = multer.memoryStorage()
const upload = multer({ storage: storage })
const fleek = require('@fleekhq/fleek-storage-js');
require('dotenv').config()
const apiKey = process.env.FLEEK_API_KEY;
const apiSecret = process.env.FLEEK_API_SECRET;
var app = express();
// app.use(function(req, res, next) {
// res.header("Access-Control-Allow-Origin", "*");
// res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
// next();
// });
app.use(cors())
app.get('/', function (req, res) {
res.send('hello, this is just a backend. The link for the frontend will be added here soon')
})
const uploadFunction = async (data) => {
const date = new Date();
const timestamp = date.getTime();
const input = {
apiKey,
apiSecret,
key: `${timestamp}.txt`,
data,
};
try {
const result = await fleek.upload(input);
console.log(result);
} catch (e) {
console.log('error', e);
}
}
const uploadPicFunction = async (data, ContentType, key) => {
const input = {
apiKey,
apiSecret,
key,
data,
ContentType
};
try {
const result = await fleek.upload(input);
console.log(result);
} catch (e) {
console.log('error', e);
}
}
async function deleteAllData() {
try {
const result = await fleek.deleteFile({
apiKey,
apiSecret,
key: 'alldata.json',
bucket: '1e4f9433-e9a2-4412-a561-9a1ddf54e93c-bucket',
});
} catch (e) {
console.log('error', e);
}
}
async function alldataUpload(data) {
const input = {
apiKey,
apiSecret,
key: 'alldata.json',
data,
};
try {
const result = await fleek.upload(input);
} catch (e) {
console.log('error', e);
}
}
app.post('/createComplaint', upload.single('upfile'), (req, res) => {
const fileName = req.file.originalname;
const fileType = req.file.mimetype;
const fileSize = req.file.size;
const theFile = req.file.buffer;
var userName = req.body.name;
var userEmail = req.body.email;
var location = req.body.location;
var description = req.body.description;
const date = new Date();
const PicKey = date.getTime() + '.png';
var theElement = {
userName,
userEmail,
location,
description,
PicKey,
status: 0
}
async function getDB() {
const res = await fetch('https://storageapi.fleek.co/1e4f9433-e9a2-4412-a561-9a1ddf54e93c-bucket/alldata.json')
alldata = await res.json();
// alldata.push(theElement);
alldata['issues'].push(theElement)
// delte old file and create new
await deleteAllData()
// upload alldata
await alldataUpload(JSON.stringify(alldata));
await uploadPicFunction(theFile, fileType, PicKey);
}
getDB();
res.redirect('https://black-hill-6592.on.fleek.co/success.html');
// uploadFunction(userName);
// uploadFunction(userEmail);
// uploadFunction(location);
// uploadFunction(description);
})
app.post('/updateStatus', function (req, res) {
const id = req.body.idbtn;
const status = req.body.submitButton;
// alldata = JSON.parse(alldata)
async function getDB() {
const res = await fetch('https://storageapi.fleek.co/1e4f9433-e9a2-4412-a561-9a1ddf54e93c-bucket/alldata.json')
alldata = await res.json();
if (status == 'Close') {
alldata['issues'][id].status = 2;
} else if (status == 'Investigating') {
alldata['issues'][id].status = 1;
}
await deleteAllData()
await alldataUpload(JSON.stringify(alldata));
}
getDB()
res.redirect('https://black-hill-6592.on.fleek.co/success.html');
})
const port = process.env.PORT || 3000;
app.listen(port, function () {
console.log('Your app is listening on port \n' + 'http://localhost:' + port)
});