almost finished the project
This commit is contained in:
66
app.py
66
app.py
@@ -6,6 +6,8 @@ from flask_bcrypt import Bcrypt
|
|||||||
from werkzeug.utils import secure_filename
|
from werkzeug.utils import secure_filename
|
||||||
from functools import wraps
|
from functools import wraps
|
||||||
from flask_migrate import Migrate
|
from flask_migrate import Migrate
|
||||||
|
from pdf2image import convert_from_path
|
||||||
|
import subprocess
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
|
|
||||||
@@ -73,6 +75,10 @@ def admin_required(f):
|
|||||||
return f(*args, **kwargs)
|
return f(*args, **kwargs)
|
||||||
return decorated_function
|
return decorated_function
|
||||||
|
|
||||||
|
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)
|
||||||
|
|
||||||
@app.route('/')
|
@app.route('/')
|
||||||
@login_required
|
@login_required
|
||||||
def dashboard():
|
def dashboard():
|
||||||
@@ -124,16 +130,41 @@ def upload_content():
|
|||||||
files = request.files.getlist('files')
|
files = request.files.getlist('files')
|
||||||
duration = int(request.form['duration'])
|
duration = int(request.form['duration'])
|
||||||
return_url = request.form['return_url']
|
return_url = request.form['return_url']
|
||||||
|
media_type = request.form['media_type']
|
||||||
|
|
||||||
for file in files:
|
for file in files:
|
||||||
filename = secure_filename(file.filename)
|
filename = secure_filename(file.filename)
|
||||||
file_path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
|
file_path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
|
||||||
file.save(file_path)
|
file.save(file_path)
|
||||||
if target_type == 'player':
|
|
||||||
new_content = Content(file_name=filename, duration=duration, player_id=target_id)
|
if media_type == 'pdf':
|
||||||
elif target_type == 'group':
|
# Convert PDF to PNG
|
||||||
new_content = Content(file_name=filename, duration=duration, group_id=target_id)
|
images = convert_from_path(file_path)
|
||||||
db.session.add(new_content)
|
for i, image in enumerate(images):
|
||||||
|
image_filename = f"{os.path.splitext(filename)[0]}_page_{i + 1}.png"
|
||||||
|
image_path = os.path.join(app.config['UPLOAD_FOLDER'], image_filename)
|
||||||
|
image.save(image_path, 'PNG')
|
||||||
|
new_content = Content(file_name=image_filename, duration=duration, player_id=target_id if target_type == 'player' else None, group_id=target_id if target_type == 'group' else None)
|
||||||
|
db.session.add(new_content)
|
||||||
|
os.remove(file_path) # Remove the original PDF file
|
||||||
|
elif media_type == 'ppt':
|
||||||
|
# Convert PPT/PPTX to PDF
|
||||||
|
pdf_filename = f"{os.path.splitext(filename)[0]}.pdf"
|
||||||
|
pdf_path = os.path.join(app.config['UPLOAD_FOLDER'], pdf_filename)
|
||||||
|
convert_ppt_to_pdf(file_path, pdf_path)
|
||||||
|
# Convert PDF to PNG
|
||||||
|
images = convert_from_path(pdf_path)
|
||||||
|
for i, image in enumerate(images):
|
||||||
|
image_filename = f"{os.path.splitext(filename)[0]}_page_{i + 1}.png"
|
||||||
|
image_path = os.path.join(app.config['UPLOAD_FOLDER'], image_filename)
|
||||||
|
image.save(image_path, 'PNG')
|
||||||
|
new_content = Content(file_name=image_filename, duration=duration, player_id=target_id if target_type == 'player' else None, group_id=target_id if target_type == 'group' else None)
|
||||||
|
db.session.add(new_content)
|
||||||
|
os.remove(file_path) # Remove the original PPT/PPTX file
|
||||||
|
os.remove(pdf_path) # Remove the intermediate PDF file
|
||||||
|
else:
|
||||||
|
new_content = Content(file_name=filename, duration=duration, player_id=target_id if target_type == 'player' else None, group_id=target_id if target_type == 'group' else None)
|
||||||
|
db.session.add(new_content)
|
||||||
|
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
return redirect(return_url)
|
return redirect(return_url)
|
||||||
@@ -441,6 +472,31 @@ def upload_personalization_pictures():
|
|||||||
|
|
||||||
return redirect(url_for('admin'))
|
return redirect(url_for('admin'))
|
||||||
|
|
||||||
|
@app.route('/clean_unused_files', methods=['POST'])
|
||||||
|
@login_required
|
||||||
|
@admin_required
|
||||||
|
def clean_unused_files():
|
||||||
|
# Get all file names from the database
|
||||||
|
content_files = {content.file_name for content in Content.query.all()}
|
||||||
|
logo_file = 'logo.png'
|
||||||
|
login_picture_file = 'login_picture.png'
|
||||||
|
|
||||||
|
# Get all files in the upload folder
|
||||||
|
all_files = set(os.listdir(app.config['UPLOAD_FOLDER']))
|
||||||
|
|
||||||
|
# Determine unused files
|
||||||
|
used_files = content_files | {logo_file, login_picture_file}
|
||||||
|
unused_files = all_files - used_files
|
||||||
|
|
||||||
|
# Delete unused files
|
||||||
|
for file_name in unused_files:
|
||||||
|
file_path = os.path.join(app.config['UPLOAD_FOLDER'], file_name)
|
||||||
|
if os.path.isfile(file_path):
|
||||||
|
os.remove(file_path)
|
||||||
|
|
||||||
|
flash('Unused files have been cleaned.', 'success')
|
||||||
|
return redirect(url_for('admin'))
|
||||||
|
|
||||||
@app.context_processor
|
@app.context_processor
|
||||||
def inject_theme():
|
def inject_theme():
|
||||||
if current_user.is_authenticated:
|
if current_user.is_authenticated:
|
||||||
|
|||||||
Reference in New Issue
Block a user