diff --git a/game_collection/app.py b/game_collection/app.py index d51edee..0627a8c 100644 --- a/game_collection/app.py +++ b/game_collection/app.py @@ -15,6 +15,7 @@ db.init_app(app) # Stelle sicher, dass der instance-Ordner existiert os.makedirs(app.instance_path, exist_ok=True) +os.makedirs(os.path.join(app.instance_path, 'game_images'), exist_ok=True) # Create game_images directory with app.app_context(): init_db() # Ensure the database is initialized before any operations @@ -25,8 +26,19 @@ def add_game(): return jsonify({'message': 'Unauthorized access!'}), 401 data = request.json buyer_username = g.user.username # Get the username from the authenticated user + + # Download the image and save it locally + image_url = data['image'] + image_response = requests.get(image_url) + if image_response.status_code == 200: + image_filename = os.path.join(app.instance_path, 'game_images', f"{data['title'].replace(' ', '_')}.jpg") + with open(image_filename, 'wb') as f: + f.write(image_response.content) + else: + return jsonify({'message': 'Image could not be downloaded!'}), 400 + new_game = Game( - image=data['image'], + image=image_filename, # Save the local path to the image title=data['title'], date=datetime.now().strftime('%Y-%m-%d'), # Set current date buyer=buyer_username, # Set buyer as the authenticated user's username