Fix 500 error on fetching an empty folder

This commit is contained in:
Cohee 2023-08-21 23:06:27 +03:00
parent 189895bd01
commit cf796af950
1 changed files with 6 additions and 2 deletions

View File

@ -2680,8 +2680,12 @@ app.post('/uploadimage', jsonParser, async (request, response) => {
});
app.get('/listimgfiles/:folder', (req, res) => {
const directoryPath = path.join(__dirname, 'public/user/images/', req.params.folder);
console.log(directoryPath);
const directoryPath = path.join(process.cwd(), 'public/user/images/', req.params.folder);
if (!fs.existsSync(directoryPath)) {
fs.mkdirSync(directoryPath, { recursive: true });
}
fs.readdir(directoryPath, (err, files) => {
if (err) {
return res.status(500).send({ error: "Unable to retrieve files" });