From d154853c7d569c38a626846823262da22f5b37e4 Mon Sep 17 00:00:00 2001 From: ske087 Date: Fri, 27 Jun 2025 16:50:15 +0300 Subject: [PATCH] updated structurea and file management , improved ppt transformationa and pdf handeling --- app.py | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 63 insertions(+), 2 deletions(-) diff --git a/app.py b/app.py index 885eba3..897b2f4 100644 --- a/app.py +++ b/app.py @@ -181,9 +181,70 @@ def convert_video_and_update_playlist(file_path, original_filename, target_type, else: print(f"Video conversion failed for: {file_path}") -def convert_pptx_to_images(input_file, output_folder): +def convert_pdf_to_images_and_update_playlist(pdf_file, output_folder, duration, target_type, target_id, delete_pdf=True): """ - Calls the external pptx_to_images.py script to convert PPTX to images. + Converts a PDF file to images and adds them to the playlist. + Can be used by both direct PDF uploads and PPTX conversions. + """ + print(f"Converting PDF to images: {pdf_file}") # Debugging + try: + images = convert_from_path(pdf_file, dpi=300) + print(f"Number of pages in PDF: {len(images)}") # Debugging + base_name = os.path.splitext(os.path.basename(pdf_file))[0] + image_filenames = [] + + for i, image in enumerate(images): + image_filename = f"{base_name}_page_{i + 1}.jpg" + image_path = os.path.join(output_folder, image_filename) + image.save(image_path, 'JPEG') + image_filenames.append(image_filename) + print(f"Saved page {i + 1} as image: {image_path}") # Debugging + + # Add images to playlist + if target_type == 'group': + group = Group.query.get_or_404(target_id) + for player in group.players: + for image_filename in image_filenames: + new_content = Content(file_name=image_filename, duration=duration, player_id=player.id) + db.session.add(new_content) + player.playlist_version += 1 + group.playlist_version += 1 + elif target_type == 'player': + player = Player.query.get_or_404(target_id) + for image_filename in image_filenames: + new_content = Content(file_name=image_filename, duration=duration, player_id=target_id) + db.session.add(new_content) + player.playlist_version += 1 + + db.session.commit() + print(f"Added {len(image_filenames)} pages to playlist") + + # Delete the PDF file if requested + if delete_pdf and os.path.exists(pdf_file): + os.remove(pdf_file) + print(f"PDF file deleted: {pdf_file}") + + return True + except Exception as e: + print(f"Error converting PDF to images: {e}") + return False + +def convert_pdf_to_images_and_upload(input_file, output_folder, duration, target_type, target_id): + """ + Converts a PDF file to images and adds them to the playlist. + Each page is saved as a separate image in the output folder. + """ + print(f"Converting PDF file: {input_file}") # Debugging + if not os.path.exists(output_folder): + os.makedirs(output_folder) + + # Convert PDF to images and update playlist + return convert_pdf_to_images_and_update_playlist(input_file, output_folder, duration, target_type, target_id, True) + +def convert_pptx_to_images_and_upload(input_file, output_folder, duration, target_type, target_id): + """ + Converts a PowerPoint file (.pptx) to individual slide images by first converting it to a PDF. + Each slide is saved as a separate image in the output folder and added to the playlist. """ command = [ "python", "pptx_to_images.py", input_file, output_folder