Regenerate stale order.json when media file set changes
This commit is contained in:
parent
9866524ad0
commit
617f205181
1 changed files with 33 additions and 21 deletions
54
new.php
54
new.php
|
|
@ -172,27 +172,6 @@ function list_files($dir){
|
|||
$files = array_values(array_filter(scandir($dir), fn($f)=>editable_file($f) && is_file($dir.'/'.$f)));
|
||||
$orderFile = $dir . '/order.json';
|
||||
|
||||
// Priority 1: explicit order file
|
||||
if (is_file($orderFile)) {
|
||||
$order = json_decode((string)file_get_contents($orderFile), true);
|
||||
if (is_array($order)) {
|
||||
$remaining = array_flip($files);
|
||||
$sorted = [];
|
||||
foreach ($order as $name) {
|
||||
if (isset($remaining[$name])) {
|
||||
$sorted[] = $name;
|
||||
unset($remaining[$name]);
|
||||
}
|
||||
}
|
||||
// append anything not listed (no filename sort)
|
||||
foreach ($files as $name) {
|
||||
if (isset($remaining[$name])) $sorted[] = $name;
|
||||
}
|
||||
return $sorted;
|
||||
}
|
||||
}
|
||||
|
||||
// No order.json: sort by datetime only and create it
|
||||
$media = [];
|
||||
$other = [];
|
||||
foreach ($files as $name) {
|
||||
|
|
@ -200,6 +179,39 @@ function list_files($dir){
|
|||
else $other[] = $name;
|
||||
}
|
||||
|
||||
// Priority 1: explicit order file, but regenerate if file set changed.
|
||||
if (is_file($orderFile)) {
|
||||
$order = json_decode((string)file_get_contents($orderFile), true);
|
||||
if (is_array($order)) {
|
||||
$mediaSet = array_fill_keys($media, true);
|
||||
$orderSet = [];
|
||||
$sortedMedia = [];
|
||||
|
||||
foreach ($order as $name) {
|
||||
if (!is_string($name) || !is_visual_media_file($name)) continue;
|
||||
if (isset($mediaSet[$name]) && !isset($orderSet[$name])) {
|
||||
$orderSet[$name] = true;
|
||||
$sortedMedia[] = $name;
|
||||
}
|
||||
}
|
||||
|
||||
$sameSet = (count($orderSet) === count($mediaSet));
|
||||
if ($sameSet) {
|
||||
foreach ($mediaSet as $name => $_) {
|
||||
if (!isset($orderSet[$name])) {
|
||||
$sameSet = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ($sameSet) {
|
||||
return array_merge($sortedMedia, $other);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Missing/invalid/stale order.json: sort by datetime and recreate order.json
|
||||
usort($media, function($a, $b) use ($dir) {
|
||||
$da = media_sort_datetime($dir . '/' . $a);
|
||||
$db = media_sort_datetime($dir . '/' . $b);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue