| 382 | } | 382 | } |
| 383 | } | 383 | } |
| 384 | | 384 | |
| 385 | function 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 | */ |
| | 391 | function 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 fs | 403 | 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 | } |
| 394 | | 411 | |
| 395 | /** | 412 | /** |