Added a media delete mechanism
This commit is contained in:
1 parent
6c44950d23
commit
a58903d58e
7 files changed
+490
-3
No files matched your search
@@ -0,0 +1,34 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<title>Delete Media - Sanctuary Commander</title>
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
|
||||
<link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}">
|
||||
</head>
|
||||
<body>
|
||||
<main class="container py-3">
|
||||
<h1>Delete Media</h1>
|
||||
{% if error %}
|
||||
<div class="alert alert-danger" role="alert">{{ error }}</div>
|
||||
{% endif %}
|
||||
{% if media is defined %}
|
||||
<p>Are you sure you want to delete <strong class="text-break">{{ media[1] }}</strong>?</p>
|
||||
<div class="alert alert-warning">
|
||||
<p>{{ plan.message }}</p>
|
||||
<p class="mb-0">The media record and its playback options will be permanently removed. This cannot be undone.</p>
|
||||
</div>
|
||||
{% if media[3] == 1 or media[4] == 1 %}
|
||||
<p>This item is active or marked as default. The player will select another active/default item, or a blank screen, on its next check.</p>
|
||||
{% endif %}
|
||||
<form method="POST" action="{{ url_for('media_library.delete_media', media_id=media[0]) }}">
|
||||
<input type="hidden" name="version" value="{{ version }}">
|
||||
<a class="btn btn-secondary" href="{{ url_for('media_library.media_list') }}">Cancel</a>
|
||||
<button class="btn btn-danger" type="submit" name="confirm" value="yes">Yes, delete media</button>
|
||||
</form>
|
||||
{% else %}
|
||||
<a class="btn btn-secondary" href="{{ url_for('media_library.media_list') }}">Return to media library</a>
|
||||
{% endif %}
|
||||
</main>
|
||||
</body>
|
||||
</html>
|
||||
@@ -55,6 +55,9 @@
|
||||
<span aria-hidden="true">▶</span> Play <span class="visually-hidden">{{ item.media_name }}</span>
|
||||
</button>
|
||||
</form>
|
||||
<a class="btn btn-outline-danger w-100 mt-2" href="{{ url_for('media_library.delete_media', media_id=item.media_id) }}">
|
||||
Delete <span class="visually-hidden">{{ item.media_name }}</span>
|
||||
</a>
|
||||
</div>
|
||||
</article>
|
||||
</div>
|
||||
|
||||
@@ -3,10 +3,13 @@ from pathlib import Path
|
||||
import subprocess
|
||||
from tempfile import NamedTemporaryFile
|
||||
from urllib.parse import urlsplit
|
||||
from uuid import uuid4
|
||||
|
||||
from flask import Blueprint, abort, current_app, jsonify, redirect, render_template, send_file, url_for
|
||||
from flask import Blueprint, abort, current_app, jsonify, redirect, render_template, request, send_file, url_for
|
||||
|
||||
from utilities.tools import get_db
|
||||
from utilities.media_deletion import confirmation_version, deletion_plan
|
||||
from utilities.validation import write_transaction
|
||||
|
||||
|
||||
media_library_bp = Blueprint('media_library', __name__, url_prefix='/media', template_folder='.')
|
||||
@@ -102,6 +105,97 @@ def play_media(media_id):
|
||||
return redirect(url_for('media_library.media_list'), code=303)
|
||||
|
||||
|
||||
def deletion_media(db, media_id):
|
||||
rows = db.query('SELECT media_id, media_name, media_uri, media_active, media_default '
|
||||
'FROM media WHERE media_id = ?', [media_id])[1]
|
||||
if not rows:
|
||||
abort(404)
|
||||
return rows[0]
|
||||
|
||||
|
||||
def restore_staged_file(staged, source):
|
||||
try:
|
||||
if staged is not None and staged.exists():
|
||||
if source.exists() or source.is_symlink():
|
||||
raise OSError('Original file location is occupied; recovery file retained')
|
||||
staged.rename(source)
|
||||
except OSError:
|
||||
current_app.logger.exception('Restore media manually from %s to %s', staged, source)
|
||||
|
||||
|
||||
@media_library_bp.route('/<int:media_id>/delete', methods=['GET', 'POST'])
|
||||
def delete_media(media_id):
|
||||
db = get_db()
|
||||
directory = current_app.config['CONFIG_DIRECTORY']
|
||||
config = current_app.config['config']
|
||||
staged = None
|
||||
source = None
|
||||
legacy_thumbnail = None
|
||||
try:
|
||||
if request.method == 'GET':
|
||||
media = deletion_media(db, media_id)
|
||||
plan = deletion_plan(db, media, directory, config)
|
||||
return render_template('media_delete.html', media=media, plan=plan,
|
||||
version=confirmation_version(media, plan))
|
||||
# Reject cross-site browser submissions to this destructive endpoint.
|
||||
origin = request.headers.get('Origin')
|
||||
if (request.headers.get('Sec-Fetch-Site') == 'cross-site'
|
||||
or (origin and origin.rstrip('/') != request.host_url.rstrip('/'))):
|
||||
abort(403)
|
||||
with write_transaction(db):
|
||||
media = deletion_media(db, media_id)
|
||||
plan = deletion_plan(db, media, directory, config)
|
||||
version = confirmation_version(media, plan)
|
||||
if request.form.get('confirm') != 'yes':
|
||||
return render_template('media_delete.html', media=media, plan=plan, version=version,
|
||||
error='Please confirm the deletion using the button below.'), 400
|
||||
if request.form.get('version') != version:
|
||||
return render_template('media_delete.html', media=media, plan=plan, version=version,
|
||||
error='This media or its storage has changed. Please review and confirm again.'), 409
|
||||
preview = local_preview_source(media[2])
|
||||
if preview is not None:
|
||||
stat = preview.stat()
|
||||
legacy_thumbnail = hashlib.sha256(
|
||||
f'{media_id}:{preview}:{stat.st_mtime_ns}:{stat.st_size}'.encode()
|
||||
).hexdigest() + '.png'
|
||||
source = plan['path']
|
||||
if source is not None:
|
||||
# Rename within the same directory so a database failure can be undone.
|
||||
staged = source.with_name(f'.deleting-{uuid4().hex}-{source.name}')
|
||||
source.rename(staged)
|
||||
db.query('DELETE FROM media_playback_option WHERE media_id = ?', [media_id])
|
||||
db.query('DELETE FROM media WHERE media_id = ?', [media_id])
|
||||
except (OSError, RuntimeError):
|
||||
restore_staged_file(staged, source)
|
||||
current_app.logger.exception('Unable to prepare deletion of media %s', media_id)
|
||||
return render_template('media_delete.html', error='Unable to access or remove the stored file. '
|
||||
'The media record was not deleted. Please try again or contact the administrator.'), 500
|
||||
except BaseException:
|
||||
restore_staged_file(staged, source)
|
||||
raise
|
||||
|
||||
# Only destroy the staged file after the database transaction has committed.
|
||||
if staged is not None:
|
||||
try:
|
||||
staged.unlink()
|
||||
except OSError:
|
||||
current_app.logger.exception('Media record %s deleted, but storage cleanup is needed at %s', media_id, staged)
|
||||
return render_template('media_delete.html', error='The database record was deleted, but its file '
|
||||
'could not be removed. Storage has not been freed; contact the administrator '
|
||||
'to clean up the retained file (details are in the server log).'), 500
|
||||
cache = Path(directory) / '.thumbnails'
|
||||
try:
|
||||
if not cache.is_symlink():
|
||||
thumbnails = list(cache.glob(f'{media_id}-*.png'))
|
||||
if legacy_thumbnail:
|
||||
thumbnails.append(cache / legacy_thumbnail)
|
||||
for path in thumbnails:
|
||||
path.unlink(missing_ok=True)
|
||||
except OSError:
|
||||
current_app.logger.warning('Could not clean cached thumbnails for deleted media %s', media_id)
|
||||
return redirect(url_for('media_library.media_list'), code=303)
|
||||
|
||||
|
||||
@media_library_bp.route('/<int:media_id>/thumbnail')
|
||||
def thumbnail(media_id):
|
||||
rows = get_db().query('SELECT media_uri FROM media WHERE media_id = ?', [media_id])[1]
|
||||
@@ -115,7 +209,7 @@ def thumbnail(media_id):
|
||||
key = hashlib.sha256(f'{media_id}:{source}:{stat.st_mtime_ns}:{stat.st_size}'.encode()).hexdigest()
|
||||
cache = Path(current_app.config['CONFIG_DIRECTORY']) / '.thumbnails'
|
||||
cache.mkdir(parents=True, exist_ok=True)
|
||||
destination = cache / f'{key}.png'
|
||||
destination = cache / f'{media_id}-{key}.png'
|
||||
if not destination.is_file():
|
||||
temporary = None
|
||||
try:
|
||||
|
||||
Reference in new issue
Block a user