-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.js
More file actions
180 lines (161 loc) · 4.83 KB
/
utils.js
File metadata and controls
180 lines (161 loc) · 4.83 KB
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
const expressValidator = require("express-validator");
const { User } = require("./db/models");
const { check, validationResult } = expressValidator;
// const multer = require("multer");
// const multerS3 = require("multer-s3");
// const AWS = require("aws-sdk");
// const { awsKeys } = require('./config');
const asyncHandler = (handler) => (req, res, next) => {
return handler(req, res, next).catch(next);
};
const handleValidationErrors = (req, res, next) => {
const validationErrors = validationResult(req);
if (!validationErrors.isEmpty()) {
const errors = validationErrors.array().map((error) => error.msg);
res.ok = false;
res.status(401);
res.json(errors);
return;
// const err = Error("BAD request.");
// err.errors = errors;
// err.status = 400;
// err.title = "BAD request.";
// return next(err);
}
next();
};
const modelNotFound = (model) => {
return () => {
const err = new Error(`The Specified ${model} could not be found`);
err.title = `${model} not found`;
err.status = 404;
return err;
};
};
// const loginValidation = [
// check('username')
// .notEmpty().withMessage('PLease enter a username')
// ]
const signUpValidation = [
check("username")
.notEmpty()
.withMessage("Username must be at least 1 character.")
.isLength({ max: 100 })
.withMessage("Username must be less than 100 characters")
.custom((value) => !/\s/.test(value))
.withMessage("No spaces are allowed in the username")
.custom(async (value) => {
const inUse = await User.findAll({
where: {
username: value,
},
});
if (inUse.length > 0) {
throw new Error("That username is already in use.");
} else return true;
}),
check("email")
.isEmail()
.withMessage("Not a valid email address")
.isLength({ max: 255 })
.withMessage("Email must be less than 255 characters")
.custom(async (value) => {
const inUse = await User.findAll({
where: {
email: value,
},
});
if (inUse.length > 0) {
throw new Error("That email is already in use.");
} else return true;
}),
check("password")
.notEmpty()
.withMessage("Password must be at least 1 character.")
.custom((value, { req }) => {
if (req.body.confirmPassword !== value) {
throw new Error("The password and confirmed password must match");
} else return true;
}),
];
const editUserValidations = [
check("username")
.notEmpty()
.withMessage("Username must be at least 1 character.")
.isLength({ max: 100 })
.withMessage("Username must be less than 100 characters")
.custom(async (value, { req }) => {
const inUse = await User.findAll({
where: {
username: value,
},
});
console.log(req.user.username);
if (req.user.username !== value && inUse.length > 0) {
throw new Error("That username is already in use.");
} else return true;
}),
check("email")
.isEmail()
.withMessage("Not a valid email address")
.isLength({ max: 255 })
.withMessage("Email must be less than 255 characters")
.custom(async (value, { req }) => {
const inUse = await User.findAll({
where: {
email: value,
},
});
if (req.user.email !== value && inUse.length > 0) {
throw new Error("That email is already in use.");
} else return true;
}),
check("password")
.notEmpty()
.withMessage("Password must be at least 1 character.")
.custom((value, { req }) => {
if (req.body.confirmPassword !== value) {
throw new Error("The password and confirmed password must match");
} else return true;
}),
];
songCheck = [
check("title").notEmpty().withMessage("Title cannot be empty"),
check("songUrl"),
//we want to check filetype, ask warren for help];
];
const AWS = require("aws-sdk");
const { awsKeys } = require("./config");
//setting AWS credentials and initializing aws-sdk object instance
// remember to import keys from config: const { awsKeys } = require('./config');
AWS.config = new AWS.Config();
AWS.config.accessKeyId = awsKeys.IAM_ACCESS_ID;
AWS.config.secretAccessKey = awsKeys.IAM_SECRET;
const S3 = new AWS.S3();
const getS3Url = async (key) => {
return S3.getSignedUrl("getObject", {
Bucket: "noisewave",
Key: key,
});
};
const createLocalPath = (songTitle) => {
const chars = songTitle.split("");
const bannedUrlChars = "!@#$%^&*()`~{}[]\\|;:'=+\"_,<.>/?*";
const path = chars.map((char) => {
if (bannedUrlChars.includes(char)) {
return "";
} else if (char === " ") {
return "-";
} else return char;
});
return path.join("");
};
module.exports = {
getS3Url,
asyncHandler,
handleValidationErrors,
modelNotFound,
signUpValidation,
editUserValidations,
createLocalPath,
};