Sort gallery images by date

a08b3ec7fc63c5d4301a91d07717c53dce016c89

Cohee <18619528+Cohee1207@users.noreply.github.com>

2 files changed, +21 -4Ignore whitespace
src/endpoints/images.js+1 -1
@@ -82,7 +82,7 @@ router.post('/list/:folder', (request, response) => {
82 }82 }
8383
84 try {84 try {
85 const images = getImages(directoryPath);85 const images = getImages(directoryPath, 'date');
86 return response.send(images);86 return response.send(images);
87 } catch (error) {87 } catch (error) {
88 console.error(error);88 console.error(error);
src/util.js+20 -3
@@ -382,14 +382,31 @@ function removeOldBackups(directory, prefix) {
382 }382 }
383}383}
384384
385function getImages(path) {385/**
386 * Get a list of images in a directory.
387 * @param {string} directoryPath Path to the directory containing the images
388 * @param {'name' | 'date'} sortBy Sort images by name or date
389 * @returns {string[]} List of image file names
390 */
391function getImages(directoryPath, sortBy = 'name') {
392 function getSortFunction() {
393 switch (sortBy) {
394 case 'name':
395 return Intl.Collator().compare;
396 case 'date':
397 return (a, b) => fs.statSync(path.join(directoryPath, a)).mtimeMs - fs.statSync(path.join(directoryPath, b)).mtimeMs;
398 default:
399 return (_a, _b) => 0;
400 }
401 }
402
386 return fs403 return fs
387 .readdirSync(path)404 .readdirSync(directoryPath)
388 .filter(file => {405 .filter(file => {
389 const type = mime.lookup(file);406 const type = mime.lookup(file);
390 return type && type.startsWith('image/');407 return type && type.startsWith('image/');
391 })408 })
392 .sort(Intl.Collator().compare);409 .sort(getSortFunction());
393}410}
394411
395/**412/**