Files
web-framework-resources/plugins/media-handoff/plugin.py
T

189 lines
10 KiB
Python

"""Per-season, collision-safe media handoff with checksum-verified copy/move."""
import hashlib
import os
from pathlib import Path
import re
import tempfile
import time
from flask import Blueprint, render_template, request, flash, redirect, url_for
from web_framework.auth import require_permission
from web_framework.plugins import Plugin, Setting, get_plugins
from web_framework.plugin_runtime import Service, QUEUE_SETTING, text, integer, managed_path, run_one
bp = Blueprint('plugin_media_handoff', __name__, template_folder='templates')
VIDEO = {'.mkv', '.mp4', '.avi', '.webm', '.mov', '.m4v', '.ts'}
def digest(path, heartbeat=lambda: None):
result = hashlib.sha256()
with path.open('rb') as source:
for chunk in iter(lambda: source.read(1024 * 1024), b''):
result.update(chunk)
heartbeat()
return result.hexdigest()
class Handoff(Service):
plugin_id = 'media-handoff'
def submit(self, item):
self.settings()
if not isinstance(item, dict) or item.get('category') != 'finished':
raise ValueError('Only finished watchlist items can be handed off.')
text(item.get('media_path'), 'Media folder', 2048)
integer(item.get('season', 1), 'Season', 1, 999)
if item.get('media_type') == 'tv':
integer(item.get('expected', 0), 'Verified expected episode count', 1, 10000)
return self.enqueue('Media handoff: ' + text(item.get('title'), 'Title', 140), {'item': item, 'mode': self.settings()['mode']})
def roots(self):
settings = self.settings()
source = Path(text(settings['source-root'], 'Source root')).expanduser().resolve()
target = Path(text(settings['target-root'], 'Target root')).expanduser().resolve()
if source == target or source in target.parents or target in source.parents:
raise ValueError('Source and target roots must be separate, without nesting.')
if not source.is_dir() or not target.is_dir():
raise ValueError('Configure existing source and target folders and mount them into the container.')
return source, target
def process(self, payload, progress):
item = payload.get('item', {})
if item.get('category') != 'finished':
raise ValueError('Media must be marked finished before handoff.')
source_root, target_root = self.roots()
source = managed_path(source_root, item.get('media_path'), exists=False)
target = managed_path(target_root, item.get('media_path'), exists=False)
mode = payload.get('mode', self.settings()['mode'])
if mode not in ('copy', 'move'):
raise ValueError('Choose copy or move mode.')
media_type = item.get('media_type', 'tv')
expected = integer(item.get('expected', 0), 'Expected count', 0, 10000)
season = integer(item.get('season', 1), 'Season', 1, 999)
if media_type == 'tv':
match = re.fullmatch(r'season[ ._-]*0*(\d+)', source.name, re.I)
if not match or int(match[1]) != season or expected < 1:
raise ValueError('TV handoff needs a matching Season NN folder and verified expected episode count.')
paths = sorted(path for path in source.rglob('*') if path.is_file()) if source.is_dir() else []
for path in source.rglob('*') if source.is_dir() else []:
if path.is_symlink():
raise ValueError('Media folders cannot contain symbolic links.')
videos = [path for path in paths if path.suffix.lower() in VIDEO and path.stat().st_size > 0]
target_videos = [path for path in target.rglob('*') if path.is_file() and path.suffix.lower() in VIDEO and path.stat().st_size > 0] if target.is_dir() else []
for path in target.rglob('*') if target.is_dir() else []:
if path.is_symlink():
raise ValueError('Target folders cannot contain symbolic links.')
if media_type == 'tv':
episodes = set()
for path in [*videos, *target_videos]:
match = re.search(r'S(\d+)E(\d+)', path.stem, re.I)
if match and int(match[1]) == season:
episodes.add(int(match[2]))
elif not match:
match = re.fullmatch(r'(?:E(?:pisode)?[ ._-]*)?(\d+)', path.stem, re.I)
if match:
episodes.add(int(match[1]))
if not set(range(1, expected + 1)).issubset(episodes):
raise ValueError('Expected season episode files are missing. Originals were retained.')
elif not videos and not target_videos:
raise ValueError('No completed video files were found.')
if not paths:
if target_videos:
progress(1, 1, 'Media is already present in the target.')
return
raise ValueError('Source media folder is missing.')
total = len(paths)
checked = []
last_heartbeat = [0.0]
completed = [0]
def heartbeat():
if time.monotonic() - last_heartbeat[0] >= 10:
progress(completed[0], total, 'Verifying media files.')
last_heartbeat[0] = time.monotonic()
# Preflight every destination before copying anything.
for path in paths:
destination = managed_path(target_root, str(path.relative_to(source_root)), exists=False)
value = digest(path, heartbeat)
if destination.exists() and (not destination.is_file() or digest(destination, heartbeat) != value):
raise ValueError('A different file already exists at the destination. No files were overwritten.')
checked.append((path, destination, value))
for index, (path, destination, value) in enumerate(checked):
completed[0] = index
progress(index, total, 'Transferring ' + path.name[:160])
destination.parent.mkdir(parents=True, exist_ok=True)
managed_path(target_root, str(destination.relative_to(target_root)), exists=False)
if not destination.exists():
fd, temporary = tempfile.mkstemp(dir=destination.parent, prefix='.handoff-')
try:
with os.fdopen(fd, 'wb') as output, path.open('rb') as input_file:
for chunk in iter(lambda: input_file.read(1024 * 1024), b''):
output.write(chunk)
heartbeat()
output.flush()
os.fsync(output.fileno())
if digest(Path(temporary), heartbeat) != value:
raise ValueError('Source changed during transfer. Original files were retained.')
try:
os.link(temporary, destination)
except FileExistsError:
if digest(destination, heartbeat) != value:
raise ValueError('Destination changed during transfer. Originals were retained.') from None
finally:
Path(temporary).unlink(missing_ok=True)
if digest(destination, heartbeat) != value:
raise ValueError('Destination verification failed. Originals were retained.')
if mode == 'move':
for index, (path, destination, value) in enumerate(checked):
progress(index, total, 'Removing verified source ' + path.name[:160])
if digest(path, heartbeat) != value or digest(destination, heartbeat) != value:
raise ValueError('Media changed before cleanup. Remaining originals were retained.')
path.unlink()
for directory in sorted((path for path in source.rglob('*') if path.is_dir()), reverse=True):
try:
directory.rmdir()
except OSError:
pass
try:
source.rmdir()
except OSError:
pass
subtitle = self.resolve('subtitles')
if subtitle:
for _, destination, _ in checked:
if destination.suffix.lower() in {'.srt', '.vtt'}:
subtitle.submit(str(destination.relative_to(target_root)))
self.state.put_item('media-handoff', {'title': item['title'], 'path': item['media_path'], 'files': total, 'mode': mode, 'stamp': time.time()})
self.notify('media.handoff', f"{item['title']}: {total} file(s) handed off.")
progress(total, total, 'Media handoff verified.')
def service():
return get_plugins().service('media-handoff', 'media-handoff')
@bp.route('/', methods=['GET', 'POST'])
def index():
if request.method == 'POST':
return submit()
return render_template('media-handoff/index.html', title='Media Handoff', history=service().state.items('media-handoff')[:100])
@require_permission('content.write')
def submit():
try:
service().submit({'title': request.form.get('title'), 'category': 'finished', 'media_path': request.form.get('media_path'),
'season': request.form.get('season', '1'), 'expected': request.form.get('expected', '0'), 'media_type': request.form.get('media_type', 'tv')})
flash('Handoff queued. Files will be verified before transfer.', 'success')
except ValueError as exc:
flash(str(exc), 'error')
return redirect(url_for('plugin_media_handoff.index'))
plugin = Plugin('media-handoff', 'Media Handoff', version='0.1.1', description='Verified per-season copy/move to a media library without overwriting files.', blueprint=bp,
endpoint='plugin_media_handoff.index', services={'media-handoff': Handoff}, workers={'transfer': lambda: run_one(service())}, settings_schema={
'queue': QUEUE_SETTING,
'source-root': Setting('Source media root', required=True), 'target-root': Setting('Target library root', required=True),
'mode': Setting('Transfer mode', kind='choice', choices=('copy', 'move'), default='copy'),
'subtitles': Setting('AI subtitle plugin (target library root must match)', kind='service', service='subtitles'),
'notifications': Setting('Notification plugin', kind='service', service='notify'),
})