updated for beter media management

This commit is contained in:
DigiServer Developer
2025-11-22 18:24:40 +02:00
parent f4df930d82
commit b73e10cde7
5 changed files with 735 additions and 70 deletions

View File

@@ -409,7 +409,7 @@ def delete_leftover_images():
try:
# Find all leftover image content
leftover_images = db.session.query(Content).filter(
Content.media_type == 'image',
Content.content_type == 'image',
~Content.id.in_(
db.session.query(playlist_content.c.content_id)
)
@@ -421,8 +421,8 @@ def delete_leftover_images():
for content in leftover_images:
try:
# Delete physical file
if content.file_path:
file_path = os.path.join(current_app.config['UPLOAD_FOLDER'], content.file_path)
if content.filename:
file_path = os.path.join(current_app.config['UPLOAD_FOLDER'], content.filename)
if os.path.exists(file_path):
os.remove(file_path)
@@ -430,7 +430,7 @@ def delete_leftover_images():
db.session.delete(content)
deleted_count += 1
except Exception as e:
errors.append(f"Error deleting {content.file_path}: {str(e)}")
errors.append(f"Error deleting {content.filename}: {str(e)}")
db.session.commit()
@@ -455,7 +455,7 @@ def delete_leftover_videos():
try:
# Find all leftover video content
leftover_videos = db.session.query(Content).filter(
Content.media_type == 'video',
Content.content_type == 'video',
~Content.id.in_(
db.session.query(playlist_content.c.content_id)
)
@@ -467,8 +467,8 @@ def delete_leftover_videos():
for content in leftover_videos:
try:
# Delete physical file
if content.file_path:
file_path = os.path.join(current_app.config['UPLOAD_FOLDER'], content.file_path)
if content.filename:
file_path = os.path.join(current_app.config['UPLOAD_FOLDER'], content.filename)
if os.path.exists(file_path):
os.remove(file_path)
@@ -476,7 +476,7 @@ def delete_leftover_videos():
db.session.delete(content)
deleted_count += 1
except Exception as e:
errors.append(f"Error deleting {content.file_path}: {str(e)}")
errors.append(f"Error deleting {content.filename}: {str(e)}")
db.session.commit()
@@ -500,8 +500,8 @@ def delete_single_leftover(content_id):
content = Content.query.get_or_404(content_id)
# Delete physical file
if content.file_path:
file_path = os.path.join(current_app.config['UPLOAD_FOLDER'], content.file_path)
if content.filename:
file_path = os.path.join(current_app.config['UPLOAD_FOLDER'], content.filename)
if os.path.exists(file_path):
os.remove(file_path)
@@ -509,7 +509,7 @@ def delete_single_leftover(content_id):
db.session.delete(content)
db.session.commit()
flash(f'Successfully deleted {content.file_path}', 'success')
flash(f'Successfully deleted {content.filename}', 'success')
except Exception as e:
db.session.rollback()

View File

@@ -19,15 +19,88 @@ content_bp = Blueprint('content', __name__, url_prefix='/content')
def content_list():
"""Main playlist management page."""
playlists = Playlist.query.order_by(Playlist.created_at.desc()).all()
media_files = Content.query.order_by(Content.uploaded_at.desc()).all()
media_files = Content.query.order_by(Content.uploaded_at.desc()).limit(3).all() # Only last 3
total_media_count = Content.query.count() # Total count for display
players = Player.query.order_by(Player.name).all()
return render_template('content/content_list_new.html',
playlists=playlists,
media_files=media_files,
total_media_count=total_media_count,
players=players)
@content_bp.route('/media-library')
@login_required
def media_library():
"""View all media files in the library."""
media_files = Content.query.order_by(Content.uploaded_at.desc()).all()
# Group by content type
images = [m for m in media_files if m.content_type == 'image']
videos = [m for m in media_files if m.content_type == 'video']
pdfs = [m for m in media_files if m.content_type == 'pdf']
presentations = [m for m in media_files if m.content_type == 'pptx']
others = [m for m in media_files if m.content_type not in ['image', 'video', 'pdf', 'pptx']]
return render_template('content/media_library.html',
media_files=media_files,
images=images,
videos=videos,
pdfs=pdfs,
presentations=presentations,
others=others)
@content_bp.route('/media/<int:media_id>/delete', methods=['POST'])
@login_required
def delete_media(media_id: int):
"""Delete a media file and remove it from all playlists."""
try:
media = Content.query.get_or_404(media_id)
filename = media.filename
# Get all playlists containing this media
affected_playlists = list(media.playlists.all())
# Delete physical file
file_path = os.path.join(current_app.config['UPLOAD_FOLDER'], media.filename)
if os.path.exists(file_path):
os.remove(file_path)
log_action('info', f'Deleted physical file: {filename}')
# Remove from all playlists (this will cascade properly)
db.session.delete(media)
# Increment version for all affected playlists
for playlist in affected_playlists:
playlist.version += 1
log_action('info', f'Playlist "{playlist.name}" version updated to {playlist.version} (media removed)')
db.session.commit()
# Clear cache for affected playlists
from app.blueprints.players import get_player_playlist
from app.extensions import cache
for playlist in affected_playlists:
for player in playlist.players:
cache.delete_memoized(get_player_playlist, player.id)
if affected_playlists:
flash(f'Deleted "{filename}" and removed from {len(affected_playlists)} playlist(s). Playlist versions updated.', 'success')
else:
flash(f'Deleted "{filename}" successfully.', 'success')
log_action('info', f'Media deleted: {filename} (affected {len(affected_playlists)} playlists)')
except Exception as e:
db.session.rollback()
log_action('error', f'Error deleting media: {str(e)}')
flash(f'Error deleting media: {str(e)}', 'danger')
return redirect(url_for('content.media_library'))
@content_bp.route('/playlist/create', methods=['POST'])
@login_required
def create_playlist():