uploade finalized

This commit is contained in:
2025-05-15 12:54:05 +03:00
parent ebb9bf4583
commit 1f772798fb
5 changed files with 66 additions and 33 deletions

View File

@@ -58,6 +58,7 @@ COPY --from=build /root/.cargo /root/.cargo
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
RUN pip install gunicorn
RUN apt-get update && apt-get install -y libreoffice poppler-utils
# Make port 5000 available to the world outside this container
EXPOSE 5000

98
app.py
View File

@@ -59,6 +59,47 @@ 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)
def convert_ppt_to_images(input_file, output_folder):
"""
Converts a PowerPoint file (.ppt or .pptx) to images using LibreOffice.
Each slide is saved as a separate image in the output folder.
"""
if not os.path.exists(output_folder):
os.makedirs(output_folder)
# Convert the PowerPoint file to images using LibreOffice
command = [
'libreoffice',
'--headless',
'--convert-to', 'png',
'--outdir', output_folder,
input_file
]
subprocess.run(command, check=True)
# Rename the generated images to follow the naming convention
base_name = os.path.splitext(os.path.basename(input_file))[0]
for i, file_name in enumerate(sorted(os.listdir(output_folder))):
if file_name.endswith('.png'):
new_name = f"{base_name}_{i + 1}.png"
os.rename(os.path.join(output_folder, file_name), os.path.join(output_folder, new_name))
def convert_pdf_to_images(input_file, output_folder):
"""
Converts a PDF file to images using pdf2image.
Each page is saved as a separate image in the output folder.
"""
if not os.path.exists(output_folder):
os.makedirs(output_folder)
# Convert the PDF file to images
images = convert_from_path(input_file, dpi=300)
base_name = os.path.splitext(os.path.basename(input_file))[0]
for i, image in enumerate(images):
image_filename = f"{base_name}_{i + 1}.jpg"
image_path = os.path.join(output_folder, image_filename)
image.save(image_path, 'JPEG')
# Convert EMU to pixels
def emu_to_pixels(emu):
return int(emu / 914400 * 96)
@@ -116,59 +157,50 @@ def upload_content():
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 PPT/PPTX to JPG conversion
# Handle PPT/PPTX to images
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')
convert_ppt_to_images(file_path, app.config['UPLOAD_FOLDER'])
# 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
# Add each slide image to the playlist
base_name = os.path.splitext(filename)[0]
for slide_image in os.listdir(app.config['UPLOAD_FOLDER']):
if slide_image.startswith(base_name) and slide_image.endswith('.png'):
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)
new_content = Content(file_name=slide_image, 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)
new_content = Content(file_name=slide_image, duration=duration, player_id=target_id)
db.session.add(new_content)
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', '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')
# Remove the original PPT/PPTX file after processing
os.remove(file_path)
# Handle PDF to images
elif media_type == 'pdf':
convert_pdf_to_images(file_path, app.config['UPLOAD_FOLDER'])
# Add each page image to the playlist
base_name = os.path.splitext(filename)[0]
for page_image in os.listdir(app.config['UPLOAD_FOLDER']):
if page_image.startswith(base_name) and page_image.endswith('.jpg'):
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)
new_content = Content(file_name=page_image, 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)
new_content = Content(file_name=page_image, duration=duration, player_id=target_id)
db.session.add(new_content)
if os.path.exists(file_path):
os.remove(file_path)
# Remove the original PDF file after processing
os.remove(file_path)
db.session.commit()
return redirect(return_url)

Binary file not shown.

Binary file not shown.

After

Width:  |  Height:  |  Size: 150 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 8.6 KiB