Fix gallery files caching, filter by mime type. Use fetch instead of Jquery Ajax

This commit is contained in:
Cohee
2023-08-31 00:55:17 +03:00
parent 194278d171
commit 218cfb43d8
2 changed files with 35 additions and 31 deletions

View File

@@ -26,16 +26,19 @@ let firstTime = true;
* @returns {Promise<Array>} - Resolves with an array of gallery item objects, rejects on error. * @returns {Promise<Array>} - Resolves with an array of gallery item objects, rejects on error.
*/ */
async function getGalleryItems(url) { async function getGalleryItems(url) {
return new Promise((resolve, reject) => { const response = await fetch(`/listimgfiles/${url}`, {
$.get(`/listimgfiles/${url}`, function (files) { method: 'POST',
const items = files.map((file) => ({ headers: getRequestHeaders(),
});
const data = await response.json();
const items = data.map((file) => ({
src: `user/images/${url}/${file}`, src: `user/images/${url}/${file}`,
srct: `user/images/${url}/${file}`, srct: `user/images/${url}/${file}`,
title: "", // Optional title for each item title: "", // Optional title for each item
})); }));
resolve(items);
}).fail(reject); return items;
});
} }
/** /**

View File

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