66 lines
3.1 KiB
Python
66 lines
3.1 KiB
Python
"""Plan local-file removal conservatively; never recursively delete a directory."""
|
|
|
|
import hashlib
|
|
import json
|
|
from pathlib import Path
|
|
from urllib.parse import unquote, urlsplit
|
|
|
|
|
|
def local_media_path(uri, directory):
|
|
if not uri or not uri.strip() or '\x00' in uri:
|
|
return None
|
|
try:
|
|
parsed = urlsplit(uri)
|
|
except ValueError:
|
|
return None # An invalid legacy URI must never be treated as a deletion path.
|
|
if parsed.scheme == 'file':
|
|
if parsed.netloc.lower() not in ('', 'localhost') or parsed.query or parsed.fragment:
|
|
return None
|
|
uri = unquote(parsed.path)
|
|
elif parsed.scheme:
|
|
return None
|
|
if '\x00' in uri:
|
|
return None
|
|
return Path(directory) / uri
|
|
|
|
|
|
def deletion_plan(db, media, directory, config):
|
|
"""media is (id, name, uri, active, default). Return a message and optional file."""
|
|
plan = {'path': None, 'reason': 'remote', 'identity': None}
|
|
source = local_media_path(media[2], directory)
|
|
if source is None:
|
|
plan['message'] = 'Only the database record will be removed. No stored file is associated with this source.'
|
|
return plan
|
|
resolved = source.resolve()
|
|
roots = [(Path(directory) / config.get(key, default)).resolve() for key, default in (
|
|
('image_upload_location', 'Images'), ('youtube_download_location', 'Videos'),
|
|
('upload_location', 'uploads')
|
|
)]
|
|
if not any(resolved.is_relative_to(root) and resolved != root for root in roots):
|
|
plan.update(reason='outside', message='This file is outside managed media storage. Its database record will be removed, but the file will be kept.')
|
|
return plan
|
|
if source.is_symlink():
|
|
plan.update(reason='symlink', message='This source is a symbolic link. Its database record will be removed, but the link and target file will be kept.')
|
|
return plan
|
|
if not resolved.exists():
|
|
plan.update(reason='missing', message='The stored file is already missing. Its database record will be removed.')
|
|
return plan
|
|
if not resolved.is_file():
|
|
plan.update(reason='directory', message='This source is not a regular file. Its database record will be removed, but the source will be kept.')
|
|
return plan
|
|
for other_id, other_uri in db.query('SELECT media_id, media_uri FROM media WHERE media_id != ?', [media[0]])[1]:
|
|
other = local_media_path(other_uri, directory)
|
|
if other is not None and other.resolve() == resolved:
|
|
plan.update(reason='shared', message='Another media item uses this file. Only this database record will be removed; the shared file will be kept.')
|
|
return plan
|
|
stat = resolved.stat()
|
|
plan.update(path=resolved, reason='managed',
|
|
identity=(stat.st_dev, stat.st_ino, stat.st_size, stat.st_mtime_ns),
|
|
message='The stored file will be permanently deleted from the server to free its storage.')
|
|
return plan
|
|
|
|
|
|
def confirmation_version(media, plan):
|
|
value = [list(media), plan['reason'], str(plan['path']), plan['identity']]
|
|
return hashlib.sha256(json.dumps(value).encode()).hexdigest()
|