updated server

This commit is contained in:
2025-06-20 16:33:21 +03:00
parent 2e94a334de
commit c57bb7fe6d
10 changed files with 87 additions and 91 deletions

53
app.py
View File

@@ -62,19 +62,28 @@ def admin_required(f):
def add_image_to_playlist(file, filename, duration, target_type, target_id):
"""
Save the image file and add it to the playlist database.
Increment the playlist version for the player or group.
"""
file_path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
# Only save if file does not already exist (prevents double-saving)
if not os.path.exists(file_path):
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)
db.session.add(new_content)
# Increment playlist version for the group
group.playlist_version += 1
elif target_type == 'player':
player = Player.query.get_or_404(target_id)
new_content = Content(file_name=filename, duration=duration, player_id=target_id)
db.session.add(new_content)
# Increment playlist version for the player
player.playlist_version += 1
db.session.commit()
def convert_ppt_to_images(input_file, output_folder):
"""
@@ -597,20 +606,36 @@ def clean_unused_files():
@app.route('/api/playlists', methods=['GET'])
def get_playlist():
def get_playlists():
hostname = request.args.get('hostname')
quickconnect_code = request.args.get('quickconnect_code')
# Validate the parameters
if not hostname or not quickconnect_code:
return jsonify({'error': 'Hostname and quick connect code are required'}), 400
# Find the player by hostname and verify the quickconnect code
player = Player.query.filter_by(hostname=hostname).first()
if not player or not player.verify_quickconnect_code(quickconnect_code):
if not player or not bcrypt.check_password_hash(player.quickconnect_password, quickconnect_code):
return jsonify({'error': 'Invalid hostname or quick connect code'}), 404
# Query the Content table for media files associated with the player
content = Content.query.filter_by(player_id=player.id).all()
playlist = [
{
'file_name': media.file_name,
'url': f"http://{request.host}/media/{media.file_name}",
'duration': media.duration
}
for media in content
]
playlist = [{'file_name': item.file_name, 'duration': item.duration, 'url': url_for('media', filename=item.file_name, _external=True)} for item in content]
return jsonify({'playlist': playlist})
# Return the playlist, version, and hashed quickconnect code
return jsonify({
'playlist': playlist,
'playlist_version': player.playlist_version,
'hashed_quickconnect': player.quickconnect_password
})
@app.route('/media/<path:filename>')
def media(filename):
@@ -720,6 +745,26 @@ def delete_group_media(group_id, content_id):
db.session.commit()
return redirect(url_for('manage_group', group_id=group_id))
@app.route('/api/playlist_version', methods=['GET'])
def get_playlist_version():
hostname = request.args.get('hostname')
quickconnect_code = request.args.get('quickconnect_code')
# Validate the parameters
if not hostname or not quickconnect_code:
return jsonify({'error': 'Hostname and quick connect code are required'}), 400
# Find the player by hostname and verify the quickconnect code
player = Player.query.filter_by(hostname=hostname).first()
if not player or not bcrypt.check_password_hash(player.quickconnect_password, quickconnect_code):
return jsonify({'error': 'Invalid hostname or quick connect code'}), 404
# Return the playlist version and hashed quickconnect code
return jsonify({
'playlist_version': player.playlist_version,
'hashed_quickconnect': player.quickconnect_password
})
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000, debug=True)