-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
289 lines (231 loc) · 6.97 KB
/
server.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/* Requirements */
const express = require('express');
const multer = require('multer');
const mongoose = require("mongoose");
const mongodb = require('mongodb');
const MongoClient = require('mongodb').MongoClient;
const ObjectID = require('mongodb').ObjectID;
const cors = require("cors")
const auth = require("./middleware/auth");
const bcrypt = require('bcryptjs');
const jwt = require('jsonwebtoken');
require("dotenv").config();
require("./config/database").connect();
const { Readable } = require('stream');
/* Model Imports */
const User = require("./model/user");
const Track = require("./model/track");
/* Routes */
const trackRoute = express.Router();
const userRoute = express.Router();
const app = express();
app.use(cors())
/* Page that will only be accessible when logged in */
app.get('/', (req, res) => {
res.status(200).send("Sys check 🙌");
});
app.get('/welcome', cors(), auth, (req, res) => {
res.status(200).send("Welcome 🙌");
});
app.use(express.json());
// Register
app.post("/register", async (req, res) => {
try {
// Get user input
const { firstName, lastName, email, password } = req.body;
// Validate user input
if (!(email && password && firstName && lastName)) {
res.status(400).send("All input is required");
}
// check if user already exist
// Validate if user exist in our database
const oldUser = await User.findOne({ email });
if (oldUser) {
return res.status(409).send("User Already Exists. Please Login.");
}
//Encrypt user password
encryptedUserPassword = await bcrypt.hash(password, 10);
// Create user in our database
const user = await User.create({
first_name: firstName,
last_name: lastName,
email: email.toLowerCase(), // sanitize
password: encryptedUserPassword,
tracks: [],
});
// Create token
const token = jwt.sign(
{ user_id: user._id, email },
process.env.TOKEN_KEY,
{
expiresIn: "5h",
}
);
// save user token
user.token = token;
// return new user
res.status(201).json(user);
} catch (err) {
console.log(err);
}
});
// Login
app.post("/login", async (req, res) => {
try {
// Get user input
const { email, password } = req.body;
// Validate user input
if (!(email && password)) {
res.status(400).send("All input is required");
}
// Validate if user exist in our database
const user = await User.findOne({ email });
if (user && (await bcrypt.compare(password, user.password))) {
// Create token
const token = jwt.sign(
{ user_id: user._id, email },
process.env.TOKEN_KEY,
{
expiresIn: "5h",
}
);
// save user token
user.token = token;
// user
return res.status(200).json(user);
}
return res.status(400).send("Invalid Credentials");
}
catch{
return res.status(400).send("Invalid Credentials");
}
});
/* Creating Bucket to Save Tracks */
let db;
let bucket;
mongoose.connection.on("connected", () => {
var db = mongoose.connections[0].db;
bucket = new mongoose.mongo.GridFSBucket(db, {
bucketName: "newBucket"
});
console.log(bucket);
});
/* Common Route for Track Manipulation */
app.use('/tracks', trackRoute);
/* GET /tracks */
trackRoute.get('/', (req, res) => {
res.set('content-type', 'application/json');
Track.find({}, function(err, result)
{
if(err) throw err;
return res.send(result);
});
});
/* POST /tracks */
trackRoute.post('/', (req, res) => {
const storage = multer.memoryStorage()
const upload = multer({ storage: storage, limits: { fields: 2, fileSize: 6000000, files: 1, parts: 3 }});
upload.single('track')(req, res, (err) => {
if (err) {
return res.status(400).json({ message: "Upload Request Validation Failed" });
} else if(!req.body.name) {
return res.status(400).json({ message: "No track name in request body" });
}
let trackName = req.body.name;
let artistName = req.body.artist;
// Convert buffer to Readable Stream
const readableTrackStream = new Readable();
readableTrackStream.push(req.file.buffer);
readableTrackStream.push(null);
let uploadStream = bucket.openUploadStream(trackName);
let id = uploadStream.id;
readableTrackStream.pipe(uploadStream);
uploadStream.on('error', () => {
return res.status(500).json({ message: "Error uploading file" });
});
uploadStream.on('finish', async () => {
const track = await Track.create({
track_name: trackName,
artist_name: artistName,
play_id: id,
});
// return new track
res.status(201).json(track);
});
});
});
/* GET /tracks/:trackID */
trackRoute.get('/:trackID', (req, res) => {
try {
var trackID = new ObjectID(req.params.trackID);
} catch(err) {
return res.status(400).json({ message: "Invalid trackID in URL parameter. Must be a single String of 12 bytes or a string of 24 hex characters" });
}
res.set('content-type', 'audio/mp3');
res.set('accept-ranges', 'bytes');
let downloadStream = bucket.openDownloadStream(trackID);
downloadStream.on('data', (chunk) => {
res.write(chunk);
});
downloadStream.on('error', () => {
res.sendStatus(404);
});
downloadStream.on('end', () => {
res.end();
});
});
/* Common Route for User Manipulation */
app.use('/users', userRoute);
/* GET /users */
userRoute.get('/', (req, res) => {
res.set('content-type', 'application/json');
User.find({}, function(err, result)
{
if(err) throw err;
return res.send(result);
});
});
/* GET /user/:userID */
userRoute.get('/:userID', async (req, res) => {
try {
var userID = new ObjectID(req.params.userID);
} catch(err) {
return res.status(400).json({ message: "Invalid userID in URL parameter. Must be a single String of 12 bytes or a string of 24 hex characters" });
}
res.set('content-type', 'application/json');
const existingUser = await User.findOne({ userID });
if (existingUser) {
return res.status(201).send(existingUser);
}
});
/* POST /user/:userID */
userRoute.post("/:userID", async (req, res) => {
try {
// Get user input
const { firstName, lastName, email, password, tracks } = req.body;
// Validate user input
if (!(email && password && firstName && lastName && tracks)) {
res.status(400).send("All input is required");
}
// create a filter for a movie to update
const filter = { email: email };
// this option instructs the method to create a document if no documents match the filter
const options = { upsert: false };
// create a document that sets the plot of the movie
const updateDoc = {
$set: {
first_name: firstName,
last_name: lastName,
email: email,
password: password,
tracks: tracks
},
};
const result = await User.updateOne(filter, updateDoc, options);
// return new user
res.status(201).json(result);
} catch (err) {
console.log(err);
}
});
module.exports = app;