error handling

This commit is contained in:
leca 2025-07-23 18:09:54 +03:00
parent 11bf2e6807
commit 19bb1942d8
1 changed files with 29 additions and 20 deletions

View File

@ -30,20 +30,20 @@ class UserController {
utils.removeFromFile('./inviteTokens.txt', req.body.inviteToken);
}
const token = jwt.sign({ username }, config.secret, {expiresIn: "1y"});
const token = jwt.sign({ username }, config.secret, { expiresIn: "1y" });
res.cookie("jwt", token);
return res.status(200).send("Ok");
}
async login(req, res) {
const {username, password} = req.body;
const { username, password } = req.body;
const storedPassword = await UserService.getPassword(username);
if (!bcrypt.compareSync(password, storedPassword)) {
return res.status(403).send("Password is not correct");
}
const token = jwt.sign({ username }, config.secret, {expiresIn: "1y"});
const token = jwt.sign({ username }, config.secret, { expiresIn: "1y" });
res.cookie("jwt", token);
return res.status(200).send("Ok");
}
@ -81,19 +81,23 @@ class UserController {
if (path.extname(req.file.originalname).toLowerCase() !== ".png") {
return res.status(400).send("Only .png files are allowed!");
}
const image = await Jimp.read(tempPath);
const allowedWidths = [32, 64, 128];
const allowedHeights = [32, 64];
try {
const image = await Jimp.read(tempPath);
} catch (err) {
console.log(`Error during cape installation, ${err}`)
return res.status(500).send("Internal server error. Please, report to the developer.")
}
const allowedWidths = [32, 64, 128];
const allowedHeights = [32, 64];
if (!allowedWidths.includes(image.bitmap.width) || !allowedHeights.includes(image.bitmap.height)) {
return res.status(400).send('This does not look like a minecraft skin.');
}
try {
await fs.promises.cp(tempPath, targetPath);
await fs.promises.unlink(tempPath);
} catch (err) {
return res.status(500).send("Ooops! Something went wrong! Please, report to the developer.");
}
try {
await fs.promises.cp(tempPath, targetPath);
await fs.promises.unlink(tempPath);
} catch (err) {
return res.status(500).send("Ooops! Something went wrong! Please, report to the developer.");
}
return res.status(200).send("Skin uploaded!");
}
@ -106,18 +110,23 @@ class UserController {
if (path.extname(req.file.originalname).toLowerCase() !== ".png") {
return res.status(400).send("Only .png files are allowed!");
}
const image = await Jimp.read(tempPath);
try {
const image = await Jimp.read(tempPath);
}
catch (err) {
console.log(`Error during cape installation, ${err}`)
return res.status(500).send("Internal server error. Please, report to the developer.")
}
if ((image.bitmap.width != 64 || image.bitmap.height != 32) && (image.bitmap.width != 128 || image.bitmap.height != 64)) {
return res.status(400).send('This does not look like a minecraft cape.');
}
try {
await fs.promises.cp(tempPath, targetPath);
await fs.promises.unlink(tempPath);
} catch (err) {
return res.status(500).send("Ooops! Something went wrong! Please, report to the developer.");
}
await fs.promises.cp(tempPath, targetPath);
await fs.promises.unlink(tempPath);
} catch (err) {
return res.status(500).send("Ooops! Something went wrong! Please, report to the developer.");
}
return res.status(200).send("Cape uploaded!");
}