generated from lucien/api-template
add: added emojis
This commit is contained in:
parent
fafbceb6a2
commit
f326555c59
14 changed files with 523 additions and 2 deletions
102
back/api/emojis.js
Normal file
102
back/api/emojis.js
Normal file
|
@ -0,0 +1,102 @@
|
|||
const express = require('express');
|
||||
const { getConnection, getEmojis, addEmoji, getEmojiByName, deleteEmoji } = require('../libs/mysql');
|
||||
const { checkAuth } = require("../libs/middlewares")
|
||||
const multer = require('multer');
|
||||
const fs = require('node:fs');
|
||||
const path = require('node:path');
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
const upload = multer({ dest: 'data/emojis/' })
|
||||
upload.limits = {
|
||||
fileSize: 1024 * 1024 * 1,
|
||||
files: 1,
|
||||
};
|
||||
|
||||
router.get('/', async (req, res) => {
|
||||
const connection = await getConnection();
|
||||
const emojis = await getEmojis(connection);
|
||||
connection.end();
|
||||
res.send(emojis);
|
||||
});
|
||||
|
||||
router.post('/add', upload.single("emoji"), checkAuth, async (req, res) => {
|
||||
const { name } = req.body;
|
||||
const file = req.file;
|
||||
|
||||
if (!name || !file) {
|
||||
return res.status(400).send({ error: 'Invalid name or file' });
|
||||
}
|
||||
|
||||
const connection = await getConnection();
|
||||
|
||||
const emoji = await getEmojiByName(connection, name);
|
||||
|
||||
if (emoji[0]) {
|
||||
connection.end();
|
||||
fs.unlinkSync(`data/emojis/${file.filename}`);
|
||||
return res.status(400).send({ error: 'Emoji already exists' });
|
||||
}
|
||||
|
||||
if (!/^[a-zA-Z0-9-_]+$/.test(name)) {
|
||||
connection.end();
|
||||
fs.unlinkSync(`data/emojis/${file.filename}`);
|
||||
return res.status(400).send({ error: 'Invalid name' });
|
||||
}
|
||||
|
||||
await addEmoji(connection, name, file.filename);
|
||||
connection.end();
|
||||
|
||||
req.sockets.emit({
|
||||
type: 'new_emoji'
|
||||
});
|
||||
|
||||
res.send({ message: "Emoji added" });
|
||||
});
|
||||
|
||||
router.get('/:name', async (req, res) => {
|
||||
const { name } = req.params;
|
||||
|
||||
const connection = await getConnection();
|
||||
const emoji = await getEmojiByName(connection, name);
|
||||
connection.end();
|
||||
|
||||
if (!emoji[0]) {
|
||||
return res.status(404).send({ error: 'Emoji not found' });
|
||||
}
|
||||
|
||||
res.sendFile(path.join(__dirname, `../data/emojis/${emoji[0].file}`), { headers: { 'Content-Type': 'image' } });
|
||||
});
|
||||
|
||||
router.post('/:name/delete', checkAuth, async (req, res) => {
|
||||
const { name } = req.params;
|
||||
const user = req.user;
|
||||
|
||||
const connection = await getConnection();
|
||||
const emoji = await getEmojiByName(connection, name);
|
||||
|
||||
if (!emoji[0]) {
|
||||
connection.end();
|
||||
return res.status(404).send({ error: 'Emoji not found' });
|
||||
}
|
||||
|
||||
if (user.admin !== 1) {
|
||||
connection.end();
|
||||
return res.status(401).send({ error: 'Unauthorized' });
|
||||
}
|
||||
|
||||
if (fs.existsSync(`data/emojis/${emoji[0].file}`)) {
|
||||
fs.unlinkSync(`data/emojis/${emoji[0].file}`);
|
||||
}
|
||||
|
||||
await deleteEmoji(connection, emoji[0].id);
|
||||
connection.end();
|
||||
|
||||
req.sockets.emit({
|
||||
type: 'delete_emoji',
|
||||
});
|
||||
|
||||
res.send({ message: "Emoji deleted" });
|
||||
});
|
||||
|
||||
module.exports = router;
|
Loading…
Add table
Add a link
Reference in a new issue