Files
simpleblog/templates/admin/media.html
T
2026-11-14 17:05:01 +00:00

125 lines
5.0 KiB
HTML

{% extends "admin/base.html" %}
{% block title %}Media · {{ site_title }}{% endblock %}
{% block admin_content %}
<h1>Media</h1>
<div class="admin-panel">
<h2>Upload a photo</h2>
<form id="media-upload-form" method="POST" action="{{ url_for('admin.upload') }}" enctype="multipart/form-data" class="stack">
<input type="hidden" name="csrf_token" value="{{ csrf_token }}">
<div class="form-group">
<label for="media-photo-file">Photo file</label>
<input type="file" id="media-photo-file" name="file" accept="image/*" multiple required>
</div>
<div class="form-group">
<label for="media-photo-album">Add to album (optional)</label>
<select id="media-photo-album" name="album">
<option value="">— Leave ungrouped —</option>
{% for a in albums %}
<option value="{{ a.slug }}">{{ a.name }}</option>
{% endfor %}
</select>
</div>
<button type="submit" class="btn btn-primary">Upload photo</button>
</form>
</div>
{% if ungrouped %}
<div class="admin-panel">
<h2>Ungrouped photos ({{ ungrouped|length }})</h2>
{% for m in ungrouped %}
{{ _media_row(m, albums) }}
{% else %}
<p class="empty-state">No ungrouped photos.</p>
{% endfor %}
</div>
{% endif %}
{% for aid, rows in grouped.items() %}
<div class="admin-panel">
<h2>{{ album_names.get(aid, 'Album') }} ({{ rows|length }})</h2>
{% for m in rows %}
{{ _media_row(m, albums) }}
{% else %}
<p class="empty-state">No photos.</p>
{% endfor %}
</div>
{% else %}
<p class="empty-state">No photos yet. Upload one from the Albums page or a post/page editor.</p>
{% endfor %}
<script>
// NOTE: one file per request, with FormData built from scratch. Cloning the
// form (new FormData(form)) also re-sends the <input type="file"> parts, so
// the server stored the selected photo twice.
// NOTE: every upload is awaited before location.reload(). The old version
// fired all the fetches and reloaded synchronously in the same tick, which
// aborted the in-flight requests — the page came back with no new photos,
// so uploads looked broken even though /admin/upload was healthy.
(function () {
var form = document.getElementById("media-upload-form");
if (!form) return;
form.addEventListener("submit", async function (e) {
e.preventDefault();
var fileInput = form.elements["media-photo-file"];
var files = Array.prototype.slice.call(fileInput.files);
if (!files.length) { alert("Choose one or more photos."); return; }
var album = form.elements["media-photo-album"].value;
var csrf = form.elements["csrf_token"].value;
var btn = form.querySelector("button[type=submit]");
btn.disabled = true;
var ok = 0, failed = [];
for (var i = 0; i < files.length; i++) {
btn.textContent = "Uploading " + (i + 1) + "/" + files.length + "...";
var fd = new FormData();
fd.append("csrf_token", csrf);
if (album) fd.append("album", album);
fd.append("file", files[i], files[i].name);
try {
var r = await fetch(form.action, { method: "POST", credentials: "same-origin", body: fd });
if (!r.ok) throw new Error("HTTP " + r.status);
await r.json();
ok++;
} catch (err) {
failed.push(files[i].name + ": " + err.message);
}
}
btn.disabled = false;
btn.textContent = "Upload photo";
if (failed.length) {
alert(ok + " uploaded, " + failed.length + " failed:\n" + failed.join("\n"));
} else {
alert(ok + " photo" + (ok === 1 ? "" : "s") + " uploaded.");
}
location.reload();
});
})();
</script>
{% endblock %}
{% macro _media_row(m, albums) %}
<div class="media-row">
<img class="media-thumb" src="{{ m.thumb_url or m.url }}" data-src="{{ m.url }}" alt="" style="width:60px;height:60px;object-fit:cover;">
<div class="media-info">
<a href="{{ m.url }}" target="_blank" rel="noreferrer">{{ m.url }}</a>
<small>{{ m.created_at or '' }}</small>
</div>
<div class="media-actions">
<form method="POST" action="{{ url_for('admin.media_assign', mid=m.id) }}" style="display:inline;">
<input type="hidden" name="csrf_token" value="{{ csrf_token }}">
<select name="album_id" onchange="this.form.submit()">
{% for a in albums %}
<option value="{{ a.id }}">{{ a.name }}</option>
{% endfor %}
</select>
</form>
<span class="field-hint">·</span>
<form method="POST" action="{{ url_for('admin.media_remove', mid=m.id) }}" style="display:inline;" onsubmit="return confirm('Remove this photo permanently? The file will be deleted.');">
<input type="hidden" name="csrf_token" value="{{ csrf_token }}">
<button class="btn btn-danger" type="submit">Remove</button>
</form>
</div>
</div>
{% endmacro %}