A pigallery2 extension to sort directories based on their last modification date only.
This extension is built on top of the pigallery2-sample-extension template.
I prefer the folders in my gallery to be sorted by their last modification date ( like ls -ltr on Linux ). Although pigallery2 provides a "Sort directories by date" option in the settings page and even states that:
Directory date is the last modification time of that directory not the creation date of the oldest photo.
However, the actual behavior still uses the oldest media ( if available ) in the folder to determine the sorting order, which is not what I wanted. Therefore I developed an extension that sorts directories purely based on their last modification date.
Important
For sorting by a directory's "last modification date", the extension uses the directory's mtime, making it behave just like ls -ltr on Linux.
There are two ways to install the extension.
- Goto the release page and download
sort-dir-lastmod.zip. - Extract the files into the
sort-dir-lastmoddirectory. - Put the
sort-dir-lastmoddirectory under the<path to the extension folder>, as mentioned in the README of pigallery2 Extension.- If you're using docker to run pigallery2,
<path to the extension folder>isconfig/extensions( note the 's' at the end ! )
- If you're using docker to run pigallery2,
- Goto the settings page in pigallery2 -> Extensions section, and make sure the extension is installed and enabled.
- Goto the settings page in pigallery2 -> Extensions section.
- Edit the Repository url field into:
https://gist.githubusercontent.com/bruce30262/bbf39d12f058764014fe29e86cb0c56e/raw/pigallery2_extensions_repo.md
The gist is a markdown file for installing the extension. It has the same format as the one in the pigallery2 repository.
- You will need to reload the settings page. After that, goto the Extensions section again, and click the Install extensions button.
- If the Repository url is set correctly, a modal will appear showing the extension "Sort Dir by lastModified". Click Install to add the extension.
- If all went well, the extension will be installed and enabled automatically.
- Enable the "Sort directories by date" option in the settings page.
- Sort the directories by date. You can compare the results with the output of
ls -lrtto see if it's working correctly.
Note
This section simply describes how I implemented this extension.
By default, pigallery2 sorts directories by date using the following logic ( ref ):
c.directories.sort(
(a, b) => (a.cache.oldestMedia || a.lastModified) - (b.cache.oldestMedia || b.lastModified)
);As you can see, to sort directories purely by lastModified, cache.oldestMedia needs to be set to null -- which is one of the things this extension does.
However, lastModified in pigallery2 is calculated as max(ctime, mtime) ( ref ). This causes the sorting to behave more like ls -lcrt, which didn't suit me needs. To achieve the desired behavior, the extension overwrites lastModified with the directory's mtime.
With these changes, the final behavior effectively matches ls -lrt.
- bpatrik, the author of pigallery2, for creating the extension feature and providing advice and guidance on developing this extension.
- Claude and ChatGPT for helping me write code and documentation 😬.