All checks were successful
continuous-integration/drone/push Build is passing
Note: We appreciate your feedback and bug reports to continue improving our platform. Thank you for your continued support!
47 lines
1.3 KiB
JavaScript
47 lines
1.3 KiB
JavaScript
const express = require('express');
|
|
const fs = require('fs');
|
|
const path = require('path');
|
|
const router = express.Router();
|
|
|
|
router.use(express.json());
|
|
|
|
router.post('/wallpaper', (req, res) => {
|
|
const userId = req.body.userId;
|
|
const wallpaperUrl = req.body.wallpaperUrl;
|
|
|
|
if (!wallpaperUrl) {
|
|
return res.status(400).send('No wallpaper URL provided.');
|
|
}
|
|
|
|
updateUserWallpaper(userId, wallpaperUrl, res);
|
|
});
|
|
|
|
const updateUserWallpaper = (userId, wallpaperUrl, res) => {
|
|
const userFilePath = path.join(__dirname, '../../../data', 'user.json');
|
|
|
|
fs.readFile(userFilePath, 'utf8', (err, data) => {
|
|
if (err) {
|
|
return res.status(500).send('Error reading the file');
|
|
}
|
|
|
|
let users = JSON.parse(data);
|
|
|
|
const userIndex = users.findIndex(u => u.id === userId);
|
|
|
|
if (userIndex !== -1) {
|
|
users[userIndex].wallpaper = wallpaperUrl;
|
|
|
|
fs.writeFile(userFilePath, JSON.stringify(users, null, 2), err => {
|
|
if (err) {
|
|
return res.status(500).send('Error writing to the file');
|
|
}
|
|
res.json({ wallpaper: wallpaperUrl });
|
|
});
|
|
} else {
|
|
res.status(404).send('User not found');
|
|
}
|
|
});
|
|
};
|
|
|
|
module.exports = router;
|