-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
192 lines (156 loc) · 5.27 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
require("dotenv").config();
const port = process.env.PORT;
const cors = require("cors");
const mysql = require('mysql2');
const { S3Client, GetObjectCommand, PutObjectCommand, DeleteObjectCommand } = require("@aws-sdk/client-s3");
const {getSignedUrl} = require("@aws-sdk/s3-request-presigner");
const express = require("express");
const {format} = require('date-fns');
const { MongoClient } = require('mongodb');
const uri = process.env.MONGODB;
const app = express();
app.use(express.json());
app.use(
cors({
"origin": "*",
"methods": "GET,HEAD,PUT,PATCH,POST,DELETE",
"preflightContinue": false,
"optionsSuccessStatus": 204
})
);
app.use((req, res, next) => {
res.setHeader("Access-Control-Allow-Origin", "*");
res.header(
"Access-Control-Allow-Headers",
"Origin, X-Requested-With, Content-Type, Accept"
);
next();
});
//database connection
const dbClient = new MongoClient(uri);
async function connect() {
try {
// Connect to the MongoDB cluster
await dbClient.connect();
console.log('Connected to the MongoDB cluster');
} catch (error) {
console.error('Error connecting to the MongoDB cluster', error);
}
}
connect().then(() => {
app.listen(port, () => {
console.log(`server is running on PORT ${port}`);
});
});
const db = dbClient.db('learning-platform');
//aws s3 connetion
const client = new S3Client({ region: "ap-south-1", credentials: {
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY
} });
async function getObjectUrl(key){
const command = new GetObjectCommand({
Bucket: `${process.env.AWS_S3_BUCKET_NAME}`,
Key: `teacher/upload/${key}`
});
const url = await getSignedUrl(client, command);
return url;
}
async function putObjectUrl(filename, contentType) {
const command = new PutObjectCommand({
Bucket: process.env.AWS_S3_BUCKET_NAME,
Key: `teacher/upload/${filename}`,
ContentType: contentType,
});
const url = await getSignedUrl(client,command);
return url;
}
async function deleteObject(filename) {
const command = new DeleteObjectCommand({
Bucket: process.env.AWS_S3_BUCKET_NAME,
Key: `teacher/upload/${filename.concat(".pdf")}`
});
const url = await client.send(command);
return url;
}
// Apis
app.get("/getting/pdf/", async (request, response) => {
const {name} = request.query;
if (name == undefined) {
response.status(400);
response.send("invalid file name");
}
const url = await getObjectUrl(name)
response.send(url);
});
app.get("/all/pdf/", async (request, response) => {
const {sort, date} = request.query;
const collection = db.collection('fileDetails');
if (sort === undefined && date === undefined) {
const result = await collection.find().toArray();
response.send(result);
}
else if (date !== "all") {
const result = await collection.find({created_at: {$eq: format(date, "dd-MM-yyyy")}}).toArray();
response.send(result);
}
else {
const result = await collection.find().sort({created_at: parseInt(sort)}).toArray();
response.send(result);
}
});
app.post("/upload/pdf", async (request, response) => {
const {filename, contentType, dateTime,tags, size, subject, marks, duration, description} = request.body;
const collection = db.collection('fileDetails');
const result = await collection.find({filename: filename.slice(0,-4)}).toArray();
if (result.length !== 0) {
response.status(400);
response.send("file already exist");
}
else if (filename == undefined) {
response.status(400);
response.send("invalid file name");
}
else if (tags.length === 0) {
response.status(400);
response.send("Insert tag name");
}
else if (size == undefined) {
response.status(400);
response.send("invalide size");
}
else if (marks == undefined) {
response.status(400);
response.send("invalide marks");
}
else if (duration == undefined) {
response.status(400);
response.send("invalide duration");
}
else if (description == undefined) {
response.status(400);
response.send("invalide description");
}
else {
const result = await collection.insertOne({filename: filename.slice(0,-4),created_at: dateTime, tags: tags, size: size, subject: subject, marks: marks, duration:duration, description: description });
response.send(await putObjectUrl(filename, contentType));
}
});
app.delete("/delete", async (req, res) => {
const {name} = req.query;
const collection = db.collection('fileDetails');
const result = await collection.find({filename: name}).toArray();
if (name === undefined) {
res.status(400);
res.send("Invalid filename");
}
else if (result.length == 0) {
res.status(400);
res.send("File Not Exist");
}
else {
await collection.deleteOne({filename: name});
await deleteObject(name);
res.send("Deleted Successfully");
}
})