mirror of
https://github.com/SillyTavern/SillyTavern.git
synced 2025-06-05 21:59:27 +02:00
Sort gallery images by date
This commit is contained in:
@ -82,7 +82,7 @@ router.post('/list/:folder', (request, response) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const images = getImages(directoryPath);
|
const images = getImages(directoryPath, 'date');
|
||||||
return response.send(images);
|
return response.send(images);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
|
23
src/util.js
23
src/util.js
@ -382,14 +382,31 @@ function removeOldBackups(directory, prefix) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function getImages(path) {
|
/**
|
||||||
|
* Get a list of images in a directory.
|
||||||
|
* @param {string} directoryPath Path to the directory containing the images
|
||||||
|
* @param {'name' | 'date'} sortBy Sort images by name or date
|
||||||
|
* @returns {string[]} List of image file names
|
||||||
|
*/
|
||||||
|
function getImages(directoryPath, sortBy = 'name') {
|
||||||
|
function getSortFunction() {
|
||||||
|
switch (sortBy) {
|
||||||
|
case 'name':
|
||||||
|
return Intl.Collator().compare;
|
||||||
|
case 'date':
|
||||||
|
return (a, b) => fs.statSync(path.join(directoryPath, a)).mtimeMs - fs.statSync(path.join(directoryPath, b)).mtimeMs;
|
||||||
|
default:
|
||||||
|
return (_a, _b) => 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return fs
|
return fs
|
||||||
.readdirSync(path)
|
.readdirSync(directoryPath)
|
||||||
.filter(file => {
|
.filter(file => {
|
||||||
const type = mime.lookup(file);
|
const type = mime.lookup(file);
|
||||||
return type && type.startsWith('image/');
|
return type && type.startsWith('image/');
|
||||||
})
|
})
|
||||||
.sort(Intl.Collator().compare);
|
.sort(getSortFunction());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Reference in New Issue
Block a user