Updated to convert PDF

This commit is contained in:
2025-05-14 16:45:48 +03:00
parent 78fe1da113
commit de7e234da1
5 changed files with 36 additions and 7 deletions

43
app.py
View File

@@ -113,14 +113,43 @@ def upload_content():
file_path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
file.save(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)
# 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)
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)
# 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
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)
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)