Use per-file caption fields with previews
This commit is contained in:
parent
251d6b298c
commit
7718e97d68
2 changed files with 23 additions and 14 deletions
35
new.php
35
new.php
|
|
@ -7,21 +7,24 @@ function rrmdir($dir){ if(!is_dir($dir)) return; foreach(scandir($dir) as $f){ i
|
|||
function input_dirs($base){ $dirs = glob($base.'/*', GLOB_ONLYDIR) ?: []; usort($dirs, fn($a,$b)=>filemtime($b)<=>filemtime($a)); return $dirs; }
|
||||
function safe_input_dir($base, $name){ $name = basename((string)$name); $path = realpath($base . '/' . $name); $root = realpath($base); if (!$path || !$root || !str_starts_with($path, $root . DIRECTORY_SEPARATOR) || !is_dir($path)) throw new RuntimeException('Invalid input directory.'); return $path; }
|
||||
function read_data($dir){
|
||||
$data = ['title'=>'','date'=>'','place'=>'','description'=>'','captions'=>'']; $file = $dir . '/data.txt';
|
||||
$data = ['title'=>'','date'=>'','place'=>'','description'=>'','captions'=>[]]; $file = $dir . '/data.txt';
|
||||
if (!is_file($file)) return $data;
|
||||
$header = []; $description = []; $captions = []; $inDescription = false;
|
||||
$header = []; $description = []; $inDescription = false;
|
||||
foreach (file($file, FILE_IGNORE_NEW_LINES) as $line) {
|
||||
$line = trim($line);
|
||||
if ($line === '' && ($header || $inDescription)) { $inDescription = true; continue; }
|
||||
if ($line === '' || str_starts_with($line, '#')) continue;
|
||||
if (str_contains($line, ':')) { $captions[] = $line; continue; } // key:value is only for per-file captions
|
||||
if (str_contains($line, ':')) {
|
||||
[$fileName, $caption] = array_map('trim', explode(':', $line, 2));
|
||||
if ($fileName !== '') $data['captions'][$fileName] = $caption;
|
||||
continue;
|
||||
}
|
||||
if ($inDescription) $description[] = $line; else $header[] = $line;
|
||||
}
|
||||
if ($header) $data['title'] = $header[0];
|
||||
if (count($header) > 1) $data['place'] = $header[1];
|
||||
if (count($header) > 2) $data['date'] = $header[2];
|
||||
if ($description) $data['description'] = implode("\n", $description);
|
||||
if ($captions) $data['captions'] = implode("\n", $captions);
|
||||
return $data;
|
||||
}
|
||||
function write_data($dir, $post){
|
||||
|
|
@ -29,18 +32,24 @@ function write_data($dir, $post){
|
|||
$place = trim($post['place'] ?? '');
|
||||
$date = trim($post['date'] ?? '');
|
||||
$description = trim($post['description'] ?? '');
|
||||
$captions = trim($post['captions'] ?? '');
|
||||
if (function_exists('mb_substr')) $description = mb_substr($description, 0, 5000, 'UTF-8'); else $description = substr($description, 0, 5000);
|
||||
$lines = [];
|
||||
if ($title !== '') $lines[] = $title;
|
||||
if ($place !== '') $lines[] = $place;
|
||||
if ($place !== '' && $date !== '') $lines[] = $date;
|
||||
if ($description !== '') { $lines[] = ''; $lines[] = $description; }
|
||||
if ($captions !== '') {
|
||||
if ($description === '') $lines[] = '';
|
||||
foreach (preg_split('/\R/', $captions) as $captionLine) {
|
||||
$captionLine = trim($captionLine);
|
||||
if ($captionLine !== '' && str_contains($captionLine, ':')) $lines[] = $captionLine;
|
||||
$captionFiles = $post['caption_files'] ?? [];
|
||||
$captions = $post['captions'] ?? [];
|
||||
if (is_array($captionFiles) && is_array($captions)) {
|
||||
$captionLines = [];
|
||||
foreach ($captionFiles as $i => $fileName) {
|
||||
$fileName = basename((string)$fileName);
|
||||
$caption = trim((string)($captions[$i] ?? ''));
|
||||
if ($fileName !== '' && $caption !== '') $captionLines[] = $fileName . ': ' . $caption;
|
||||
}
|
||||
if ($captionLines) {
|
||||
if ($description === '') $lines[] = '';
|
||||
array_push($lines, ...$captionLines);
|
||||
}
|
||||
}
|
||||
file_put_contents($dir . '/data.txt', implode("\n", $lines) . "\n");
|
||||
|
|
@ -102,13 +111,13 @@ try {
|
|||
} catch (Throwable $e) { $err = $e->getMessage(); }
|
||||
$dirs = input_dirs($config['uploads_dir']);
|
||||
$editName = $_GET['edit'] ?? '';
|
||||
$editDir = null; $editData = ['title'=>'','date'=>'','place'=>'','description'=>'','captions'=>'']; $editFiles = [];
|
||||
$editDir = null; $editData = ['title'=>'','date'=>'','place'=>'','description'=>'','captions'=>[]]; $editFiles = [];
|
||||
if ($editName !== '') { try { $editDir = safe_input_dir($config['uploads_dir'], $editName); $editData = read_data($editDir); $editFiles = list_files($editDir); } catch (Throwable $e) { $err = $e->getMessage(); } }
|
||||
?>
|
||||
<!doctype html><html><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Admin - <?=h($config['site_name'])?></title><link rel="stylesheet" href="style.css"></head><body>
|
||||
<header><h1>MVLog Admin</h1><nav><a href="index.php">Back</a></nav></header><main>
|
||||
<?php if($msg): ?><div class="ok"><?=h($msg)?></div><?php endif; ?><?php if($err): ?><div class="err"><?=h($err)?></div><?php endif; ?>
|
||||
<section><h2>Existing input dirs</h2><?php if(!$dirs): ?><p>No input directories yet.</p><?php endif; ?><div class="admin-list"><?php foreach($dirs as $d): $data=read_data($d); ?><div class="admin-item"><div><strong><?=h($data['title'] ?: basename($d))?></strong><br><span><?=h(basename($d))?> · <?=h(count(list_files($d)))?> files</span></div><a class="button" href="?edit=<?=rawurlencode(basename($d))?>">Edit</a></div><?php endforeach; ?></div></section>
|
||||
<?php if($editDir): ?><section><h2>Edit input dir</h2><p><code>in-dir/<?=h(basename($editDir))?></code></p><form method="post" enctype="multipart/form-data"><input type="hidden" name="action" value="update"><input type="hidden" name="dir" value="<?=h(basename($editDir))?>"><label>Title<input name="title" value="<?=h($editData['title'])?>"></label><label>Date<input name="date" value="<?=h($editData['date'])?>"></label><label>Place<input name="place" value="<?=h($editData['place'])?>"></label><label>Description<textarea name="description" maxlength="5000" data-counter="description-counter-edit"><?=h($editData['description'])?></textarea><small id="description-counter-edit" class="counter"></small></label><label>Per-file captions <small>filename: caption</small><textarea name="captions" placeholder="001_photo.jpg: Caption text"><?=h($editData['captions'])?></textarea></label><label>Add images / videos<input type="file" name="media[]" multiple accept="image/*,video/*"></label><label>Add audio<input type="file" name="audio[]" multiple accept="audio/*"></label><button type="submit">Save changes</button></form><h3>Files</h3><div class="file-list"><?php foreach($editFiles as $f): ?><form method="post" class="file-row"><input type="hidden" name="action" value="delete_file"><input type="hidden" name="dir" value="<?=h(basename($editDir))?>"><input type="hidden" name="file" value="<?=h($f)?>"><span><?=h($f)?></span><button type="submit" onclick="return confirm('Remove this file?')">Remove</button></form><?php endforeach; ?></div><form method="post" onsubmit="return confirm('Delete this whole input directory?')"><input type="hidden" name="action" value="delete_dir"><input type="hidden" name="dir" value="<?=h(basename($editDir))?>"><button type="submit">Delete input dir</button></form></section><?php endif; ?>
|
||||
<?php if(!$editDir): ?><section><h2>Add new movie input</h2><form method="post" enctype="multipart/form-data"><input type="hidden" name="action" value="create"><label>Title*<input name="title" required></label><label>Date<input name="date" placeholder="2026-05-25"></label><label>Place<input name="place"></label><label>Description<textarea name="description" maxlength="5000" data-counter="description-counter-new"></textarea><small id="description-counter-new" class="counter"></small></label><label>Per-file captions <small>filename: caption</small><textarea name="captions" placeholder="001_photo.jpg: Caption text"></textarea></label><label>Images / videos*<input type="file" name="media[]" multiple required accept="image/*,video/*"></label><label>Audio (optional)<input type="file" name="audio[]" multiple accept="audio/*"></label><button type="submit">Create input-dir</button></form></section><?php endif; ?>
|
||||
<?php if($editDir): ?><section><h2>Edit input dir</h2><p><code>in-dir/<?=h(basename($editDir))?></code></p><form method="post" enctype="multipart/form-data"><input type="hidden" name="action" value="update"><input type="hidden" name="dir" value="<?=h(basename($editDir))?>"><label>Title<input name="title" value="<?=h($editData['title'])?>"></label><label>Date<input name="date" value="<?=h($editData['date'])?>"></label><label>Place<input name="place" value="<?=h($editData['place'])?>"></label><label>Description<textarea name="description" maxlength="5000" data-counter="description-counter-edit"><?=h($editData['description'])?></textarea><small id="description-counter-edit" class="counter"></small></label><label>Add images / videos<input type="file" name="media[]" multiple accept="image/*,video/*"></label><label>Add audio<input type="file" name="audio[]" multiple accept="audio/*"></label><h3>Files and captions</h3><div class="file-list"><?php foreach($editFiles as $f): $ext=strtolower(pathinfo($f, PATHINFO_EXTENSION)); $fileUrl='in-dir/'.rawurlencode(basename($editDir)).'/'.rawurlencode($f); ?><div class="caption-row"><div class="preview"><?php if(in_array($ext,['jpg','jpeg','png','webp','gif'])): ?><img src="<?=h($fileUrl)?>" alt=""><?php elseif(in_array($ext,['mp4','mov','m4v','webm'])): ?><video src="<?=h($fileUrl)?>" muted preload="metadata"></video><?php else: ?><span><?=h(strtoupper($ext ?: 'FILE'))?></span><?php endif; ?></div><div class="caption-fields"><strong><?=h($f)?></strong><input type="hidden" name="caption_files[]" value="<?=h($f)?>"><textarea name="captions[]" placeholder="Optional caption for this file"><?=h($editData['captions'][$f] ?? '')?></textarea></div><button type="submit" form="remove-<?=h(md5($f))?>" onclick="return confirm('Remove this file?')">Remove</button></div><?php endforeach; ?></div><button type="submit">Save changes</button></form><?php foreach($editFiles as $f): ?><form method="post" id="remove-<?=h(md5($f))?>"><input type="hidden" name="action" value="delete_file"><input type="hidden" name="dir" value="<?=h(basename($editDir))?>"><input type="hidden" name="file" value="<?=h($f)?>"></form><?php endforeach; ?><form method="post" onsubmit="return confirm('Delete this whole input directory?')"><input type="hidden" name="action" value="delete_dir"><input type="hidden" name="dir" value="<?=h(basename($editDir))?>"><button type="submit">Delete input dir</button></form></section><?php endif; ?>
|
||||
<?php if(!$editDir): ?><section><h2>Add new movie input</h2><form method="post" enctype="multipart/form-data"><input type="hidden" name="action" value="create"><label>Title*<input name="title" required></label><label>Date<input name="date" placeholder="2026-05-25"></label><label>Place<input name="place"></label><label>Description<textarea name="description" maxlength="5000" data-counter="description-counter-new"></textarea><small id="description-counter-new" class="counter"></small></label><label>Images / videos*<input type="file" name="media[]" multiple required accept="image/*,video/*"></label><label>Audio (optional)<input type="file" name="audio[]" multiple accept="audio/*"></label><button type="submit">Create input-dir</button></form></section><?php endif; ?>
|
||||
</main><footer>Input dirs are saved under <code>in-dir/</code>.</footer><script>document.querySelectorAll('textarea[data-counter]').forEach(function(t){var c=document.getElementById(t.dataset.counter);function u(){c.textContent=t.value.length+' / '+t.maxLength+' characters';}t.addEventListener('input',u);u();});</script></body></html>
|
||||
|
|
|
|||
|
|
@ -1 +1 @@
|
|||
@font-face{font-family:BebasNeue;src:url('assets/fonts/BebasNeue-Regular.ttf')}@font-face{font-family:IBMPlexCaption;src:url('assets/fonts/IBMPlexSans-SemiBold.ttf')}@font-face{font-family:NotoStamp;src:url('assets/fonts/NotoSans-Bold.ttf')}:root{font-family:IBMPlexCaption,system-ui,sans-serif;color:#F3F4F6;background:#111315}body{margin:0;background:#111315 url(assets/img/background.jpg) center center/cover fixed no-repeat;min-height:100vh}body::before{content:'';position:fixed;inset:0;background:rgba(17,19,21,.78);z-index:-1}header{background:#1B1E22;color:#F3F4F6;padding:1.2rem 2rem;text-align:center}header h1{font-family:NotoStamp,system-ui,sans-serif;font-size:3rem;letter-spacing:.06em;margin:0}a{color:#3BA7A0}.button,button{background:#C46A3A;color:#111315;border:0;border-radius:.5rem;padding:.7rem 1rem;text-decoration:none;cursor:pointer}nav a{margin-left:1rem;color:#F3F4F6}main{max-width:1100px;margin:2rem auto;padding:0 1rem}.video-list{display:flex;flex-direction:column;gap:1rem}.video-row,form{background:rgba(27,30,34,.92);backdrop-filter:blur(2px);border-radius:.8rem;padding:1rem;box-shadow:0 2px 16px #0006}.video-row{display:grid;grid-template-columns:minmax(280px,42%) 1fr;gap:1rem;align-items:start}.video-row video{width:100%;aspect-ratio:16/9;background:#111315;border-radius:.5rem}.video-info h2{font-size:1.6rem;margin:.1rem 0 .5rem}.meta{display:flex;gap:.5rem;flex-wrap:wrap;color:#A0A4AB;margin:.25rem 0}.meta span{background:#26383A;border-radius:999px;padding:.25rem .55rem}.description{font-size:1rem;line-height:1.45;color:#F3F4F6;margin:.9rem 0}.details{color:#A0A4AB;font-size:.9rem}label{display:block;margin:.8rem 0;font-weight:600}.counter{display:block;margin-top:.25rem;color:#A0A4AB;font-size:.8rem;font-weight:400}input,textarea{display:block;width:100%;box-sizing:border-box;margin-top:.3rem;padding:.65rem;border:1px solid #3BA7A0;background:#111315;color:#F3F4F6;border-radius:.45rem}textarea{min-height:6rem}.ok,.err{padding:1rem;border-radius:.5rem;margin-bottom:1rem}.ok{background:#1f3b32;color:#F3F4F6}.err{background:#4a241f;color:#F3F4F6}.pages a{display:inline-block;margin:.2rem;padding:.45rem .7rem;background:#1B1E22;border-radius:.4rem;text-decoration:none}.pages .active{background:#C46A3A;color:#111315}footer{font-family:NotoStamp,system-ui,sans-serif;text-align:center;color:#A0A4AB;padding:2rem;font-size:.8rem}.admin-list{display:flex;flex-direction:column;gap:.7rem}.admin-item,.file-row{display:flex;justify-content:space-between;align-items:center;gap:1rem;background:rgba(27,30,34,.92);padding:.8rem;border-radius:.6rem}.admin-item span,.file-row span{color:#A0A4AB}.file-list{display:flex;flex-direction:column;gap:.5rem;margin:1rem 0}.file-row{margin:0}.file-row button{padding:.45rem .7rem}@media(max-width:760px){.video-row{grid-template-columns:1fr}header h1{font-size:2.4rem}}
|
||||
@font-face{font-family:BebasNeue;src:url('assets/fonts/BebasNeue-Regular.ttf')}@font-face{font-family:IBMPlexCaption;src:url('assets/fonts/IBMPlexSans-SemiBold.ttf')}@font-face{font-family:NotoStamp;src:url('assets/fonts/NotoSans-Bold.ttf')}:root{font-family:IBMPlexCaption,system-ui,sans-serif;color:#F3F4F6;background:#111315}body{margin:0;background:#111315 url(assets/img/background.jpg) center center/cover fixed no-repeat;min-height:100vh}body::before{content:'';position:fixed;inset:0;background:rgba(17,19,21,.78);z-index:-1}header{background:#1B1E22;color:#F3F4F6;padding:1.2rem 2rem;text-align:center}header h1{font-family:NotoStamp,system-ui,sans-serif;font-size:3rem;letter-spacing:.06em;margin:0}a{color:#3BA7A0}.button,button{background:#C46A3A;color:#111315;border:0;border-radius:.5rem;padding:.7rem 1rem;text-decoration:none;cursor:pointer}nav a{margin-left:1rem;color:#F3F4F6}main{max-width:1100px;margin:2rem auto;padding:0 1rem}.video-list{display:flex;flex-direction:column;gap:1rem}.video-row,form{background:rgba(27,30,34,.92);backdrop-filter:blur(2px);border-radius:.8rem;padding:1rem;box-shadow:0 2px 16px #0006}.video-row{display:grid;grid-template-columns:minmax(280px,42%) 1fr;gap:1rem;align-items:start}.video-row video{width:100%;aspect-ratio:16/9;background:#111315;border-radius:.5rem}.video-info h2{font-size:1.6rem;margin:.1rem 0 .5rem}.meta{display:flex;gap:.5rem;flex-wrap:wrap;color:#A0A4AB;margin:.25rem 0}.meta span{background:#26383A;border-radius:999px;padding:.25rem .55rem}.description{font-size:1rem;line-height:1.45;color:#F3F4F6;margin:.9rem 0}.details{color:#A0A4AB;font-size:.9rem}label{display:block;margin:.8rem 0;font-weight:600}.counter{display:block;margin-top:.25rem;color:#A0A4AB;font-size:.8rem;font-weight:400}input,textarea{display:block;width:100%;box-sizing:border-box;margin-top:.3rem;padding:.65rem;border:1px solid #3BA7A0;background:#111315;color:#F3F4F6;border-radius:.45rem}textarea{min-height:6rem}.ok,.err{padding:1rem;border-radius:.5rem;margin-bottom:1rem}.ok{background:#1f3b32;color:#F3F4F6}.err{background:#4a241f;color:#F3F4F6}.pages a{display:inline-block;margin:.2rem;padding:.45rem .7rem;background:#1B1E22;border-radius:.4rem;text-decoration:none}.pages .active{background:#C46A3A;color:#111315}footer{font-family:NotoStamp,system-ui,sans-serif;text-align:center;color:#A0A4AB;padding:2rem;font-size:.8rem}.admin-list{display:flex;flex-direction:column;gap:.7rem}.admin-item,.file-row{display:flex;justify-content:space-between;align-items:center;gap:1rem;background:rgba(27,30,34,.92);padding:.8rem;border-radius:.6rem}.admin-item span,.file-row span{color:#A0A4AB}.file-list{display:flex;flex-direction:column;gap:.5rem;margin:1rem 0}.file-row{margin:0}.file-row button{padding:.45rem .7rem}.caption-row{display:grid;grid-template-columns:140px 1fr auto;gap:1rem;align-items:start;background:rgba(27,30,34,.92);padding:.8rem;border-radius:.6rem}.preview img,.preview video{width:140px;aspect-ratio:16/9;object-fit:cover;border-radius:.4rem;background:#111315}.preview span{display:grid;place-items:center;width:140px;aspect-ratio:16/9;background:#111315;border-radius:.4rem;color:#A0A4AB}.caption-fields textarea{min-height:4rem}.caption-fields strong{display:block;margin-bottom:.35rem}@media(max-width:760px){.caption-row{grid-template-columns:1fr}.preview img,.preview video,.preview span{width:100%}}@media(max-width:760px){.video-row{grid-template-columns:1fr}header h1{font-size:2.4rem}}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue