updated upload functions

This commit is contained in:
2025-05-15 11:23:49 +03:00
parent de7e234da1
commit ebb9bf4583
11 changed files with 107 additions and 60 deletions

90
app.py
View File

@@ -8,6 +8,10 @@ from functools import wraps
from extensions import db, bcrypt, login_manager
from models import User, Player, Content, Group # Add Group to the imports
from flask_login import login_user, logout_user, login_required, current_user
from pptx import Presentation
from pptx.util import Inches
from PIL import Image
import io
app = Flask(__name__, instance_relative_config=True)
@@ -55,6 +59,10 @@ def convert_ppt_to_pdf(input_file, output_file):
command = ['libreoffice', '--headless', '--convert-to', 'pdf', '--outdir', os.path.dirname(output_file), input_file]
subprocess.run(command, check=True)
# Convert EMU to pixels
def emu_to_pixels(emu):
return int(emu / 914400 * 96)
@app.route('/')
@login_required
def dashboard():
@@ -107,50 +115,61 @@ def upload_content():
duration = int(request.form['duration'])
return_url = request.form['return_url']
media_type = request.form['media_type']
print(f"Redirecting to: {return_url}") # Debugging: Log the return_url
for file in files:
filename = secure_filename(file.filename)
file_path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
file.save(file_path)
# Handle PDF to JPG conversion
if media_type == 'pdf':
images = convert_from_path(file_path, dpi=300)
for i, image in enumerate(images):
image_filename = f"{os.path.splitext(filename)[0]}_{i + 1}.jpg"
image_path = os.path.join(app.config['UPLOAD_FOLDER'], image_filename)
image.save(image_path, 'JPEG')
# Add each converted image to the playlist
if target_type == 'group':
group = Group.query.get_or_404(target_id)
for player in group.players:
new_content = Content(file_name=image_filename, duration=duration, player_id=player.id)
# Handle PPT/PPTX to JPG conversion
if media_type == 'ppt':
try:
presentation = Presentation(file_path)
for i, slide in enumerate(presentation.slides):
slide_width = emu_to_pixels(presentation.slide_width)
slide_height = emu_to_pixels(presentation.slide_height)
img = Image.new('RGB', (slide_width, slide_height), 'white')
# Save the slide as an image
image_filename = f"{os.path.splitext(filename)[0]}_slide_{i + 1}.jpg"
image_path = os.path.join(app.config['UPLOAD_FOLDER'], image_filename)
img.save(image_path, 'JPEG')
# Add each converted image to the playlist
if target_type == 'group':
group = Group.query.get_or_404(target_id)
for player in group.players:
new_content = Content(file_name=image_filename, duration=duration, player_id=player.id)
db.session.add(new_content)
elif target_type == 'player':
new_content = Content(file_name=image_filename, duration=duration, player_id=target_id)
db.session.add(new_content)
elif target_type == 'player':
new_content = Content(file_name=image_filename, duration=duration, player_id=target_id)
db.session.add(new_content)
# Optionally, delete the original PDF file after conversion
os.remove(file_path)
finally:
# Ensure the original PPT file is deleted after processing
if os.path.exists(file_path):
os.remove(file_path)
# Handle other media types
elif media_type in ['image', 'video', 'ppt']:
if media_type == 'ppt':
ppt_output_file = os.path.splitext(file_path)[0] + '.pdf'
convert_ppt_to_pdf(file_path, ppt_output_file)
os.remove(file_path) # Remove the original PPT file
file_path = ppt_output_file
elif media_type in ['image', 'video', 'pdf']:
if media_type == 'pdf':
images = convert_from_path(file_path, dpi=300)
for i, image in enumerate(images):
image_filename = f"{os.path.splitext(filename)[0]}_{i + 1}.jpg"
image_path = os.path.join(app.config['UPLOAD_FOLDER'], image_filename)
image.save(image_path, 'JPEG')
if target_type == 'group':
group = Group.query.get_or_404(target_id)
for player in group.players:
new_content = Content(file_name=image_filename, duration=duration, player_id=player.id)
db.session.add(new_content)
elif target_type == 'player':
new_content = Content(file_name=image_filename, duration=duration, player_id=target_id)
db.session.add(new_content)
if os.path.exists(file_path):
os.remove(file_path)
if target_type == 'group':
group = Group.query.get_or_404(target_id)
for player in group.players:
new_content = Content(file_name=filename, duration=duration, player_id=player.id)
db.session.add(new_content)
elif target_type == 'player':
new_content = Content(file_name=filename, duration=duration, player_id=target_id)
db.session.add(new_content)
db.session.commit()
return redirect(return_url)
@@ -158,7 +177,6 @@ def upload_content():
target_id = request.args.get('target_id')
return_url = request.args.get('return_url', url_for('dashboard'))
# Serialize players and groups into JSON-serializable dictionaries
players = [{'id': player.id, 'username': player.username} for player in Player.query.filter(~Player.groups.any()).all()]
groups = [{'id': group.id, 'name': group.name} for group in Group.query.all()]