-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
218 lines (202 loc) · 6.15 KB
/
test.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
const express = require("express");
require("dotenv").config();
const app = express();
const aws = require('aws-sdk');
const multer = require('multer');
const multerS3 = require('multer-s3');
let mysql = require('mysql2');
const {
User,
Document
} = require("./models");
User.sequelize.sync();
Document.sequelize.sync();
const bcrypt = require("bcrypt");
const { response } = require("express");
// const basicAuth = require("express-basic-auth");
// app.use(basicAuth);
// Configuring the AWS environment
aws.config.update({
secretAccessKey: process.env.ACCESS_SECRET,
accessKeyId: process.env.ACCESS_KEY,
region: process.env.REGION,
});
// configuring the Bucket
const BUCKET = process.env.BUCKET
const s3 = new aws.S3();
const upload = multer({
storage: multerS3({
s3: s3,
acl: "public-read",
bucket: BUCKET,
key: function (req, file, cb) {
console.log(file);
cb(null, file.originalname)
}
})
});
// Checking the End Point
app.get("/healthz", async (req, res) => {
res.send({ message: "The server is running" });
res.status(200);
});
// Testing the post Method
app.post('/upload', upload.single('file'), (req, res)=> {
console.log(req.file);
res.send("File Uploaded"+ req.file.location+ ' Location!');
});
// Implementing the Post Method
const Ufile = upload.single("file");
app.post("/v1/document", (req, res) => {
try {
const EncodedUserPass = req.headers.authorization.split(" ")[1];
const DecodedUserPass = Buffer.from(EncodedUserPass, "base64").toString(
"ascii"
);
const DecodedUser = DecodedUserPass.split(":")[0];
const DecodedPass = DecodedUserPass.split(":")[1];
User.findOne({ where: { EmailId: DecodedUser} }).then(
(result) => {
if (result === null) {
res.status(200);
res.send({ Output: "Client Not Found" });
} else {
bcrypt.compare(
DecodedPass,
result.dataValues.Password,
function (err, authenticated) {
if (authenticated) {
console.log("Authenticated........................>", authenticated);
Ufile(req, res, async (err) => {
if (err) {
res.status(400).send("Bad Request");
}
const docx = await Document.create({
user_id: result.dataValues.id,
name: req.file.key,
s3_bucket_path: req.file.location
});
res.status(201).send(docx);
});
} else {
res.status(200);
res.send({ Output: "Wrong Password" });
}
}
);
}
}
);
} catch (err) {
return res.status(400).send("Client Not Found");
}
});
app.get("/v1/document/:doc_id", (req, res) => {
try {
const DocId = req.params.doc_id;
const EncodedUserPass = req.headers.authorization.split(" ")[1];
const DecodedUserPass = Buffer.from(EncodedUserPass, "base64").toString(
"ascii"
);
const DecodedUser = DecodedUserPass.split(":")[0];
const DecodedPass = DecodedUserPass.split(":")[1];
User.findOne({ where: { EmailId: DecodedUser} }).then(
(result) => {
if (result === null) {
res.status(200);
res.send({ Output: "Client Not Found" });
} else {
bcrypt.compare(
DecodedPass,
result.dataValues.Password,
function (err, authenticated) {
if (authenticated) {
console.log("Authenticated........................>", authenticated);
// const DocId = DocID;
Document.findOne({
where: {
user_id: result.dataValues.id,
doc_id: DocId
},
}).then(
(DocResult)=>{
// console.log(DocResult, "...............>>>>>id");
if(DocResult){
res.status(200).send(DocResult);
}
else{
return res.status(403).send("Forbidden");
}
}
)
} else {
res.status(200);
res.send({ Output: "Wrong Password" });
}
}
);
}
}
);
} catch (err) {
return res.status(400).send("Client Not Found");
}
});
app.delete("/v1/document/:doc_id", async (req, res) => {
try{
// const DocId = req.params.doc_id;
const EncodedUserPass = req.headers.authorization.split(" ")[1];
const DecodedUserPass = Buffer.from(EncodedUserPass, "base64").toString(
"ascii"
);
const DecodedUser = DecodedUserPass.split(":")[0];
const DecodedPass = DecodedUserPass.split(":")[1];
console.log(DecodedUser, "pass user");
console.log(DecodedPass, "pass pass");
const Client = await User.findOne({
where: {
EmailId: DecodedUser,
},
});
if (Client) {
const CorrectPass = bcrypt.compareSync(DecodedPass, Client.Password);
if (CorrectPass) {
const document_id = req.params.doc_id;
const docx = await Document.findOne({
where: {
user_id: Client.id,
doc_id:document_id,
},
});
if (docx) {
await s3.deleteObject({Bucket:BUCKET,Key: docx.name}).promise();
const del = await Document.destroy({
where: {
user_id: Client.id,
doc_id: document_id,
},
});
if(del){
res.status(200).send("Document Deleted");
}
}
else{
return res.status(404).send("Not Found");
}
} else {
return res.status(401).send("Unauthorized");
}
} else {
return res.status(401).send("Unauthorized");
}
}
catch(err) {
console.log(err);
return res.status(400).send("Bad Request");
}
});
User.sequelize.sync().then((req) => {
app.listen(3080, () => {
console.log(`Server listening on the port:::::: 3080`);
});
});