Add marker image uploads
This commit is contained in:
1 parent
8d92d64fa3
commit
e4759b8961
3 files changed
+169
-2
No files matched your search
+52
-1
@@ -1,9 +1,11 @@
|
||||
import os
|
||||
import time
|
||||
import json
|
||||
import uuid
|
||||
from datetime import datetime
|
||||
from werkzeug.security import generate_password_hash, check_password_hash
|
||||
from flask import Flask, request, jsonify, session
|
||||
from werkzeug.utils import secure_filename
|
||||
from flask import Flask, request, jsonify, session, send_from_directory
|
||||
from flask_cors import CORS
|
||||
|
||||
app = Flask(__name__)
|
||||
@@ -14,7 +16,10 @@ CORS(app, supports_credentials=True) # allow cookies
|
||||
DATA_DIR = "data"
|
||||
USERS_FILE = os.path.join(DATA_DIR, "users.json")
|
||||
JOURNEYS_FILE = os.path.join(DATA_DIR, 'journeys.json')
|
||||
UPLOAD_DIR = "uploads"
|
||||
ALLOWED_IMAGE_EXTENSIONS = {"png", "jpg", "jpeg", "gif", "webp"}
|
||||
os.makedirs(DATA_DIR, exist_ok=True)
|
||||
os.makedirs(UPLOAD_DIR, exist_ok=True)
|
||||
|
||||
|
||||
|
||||
@@ -59,6 +64,13 @@ def get_user_by_username(username):
|
||||
def get_user_by_id(user_id):
|
||||
users = load_users()
|
||||
return next((u for u in users if u["id"] == user_id), None)
|
||||
|
||||
|
||||
def allowed_image_file(filename):
|
||||
if "." not in filename:
|
||||
return False
|
||||
extension = filename.rsplit(".", 1)[1].lower()
|
||||
return extension in ALLOWED_IMAGE_EXTENSIONS
|
||||
|
||||
# ==================== Authentication endpoints ====================
|
||||
@app.route("/api/register", methods=["POST"])
|
||||
@@ -354,6 +366,45 @@ def delete_comment(comment_id):
|
||||
return jsonify({'error': 'Not authorized'}), 403
|
||||
return jsonify({'error': 'Comment not found'}), 404
|
||||
|
||||
|
||||
# ==================== Uploads ====================
|
||||
@app.route("/api/uploads/images", methods=["POST"])
|
||||
def upload_images():
|
||||
if not require_login():
|
||||
return jsonify({"error": "Authentication required"}), 401
|
||||
|
||||
files = request.files.getlist("images")
|
||||
if not files:
|
||||
return jsonify({"error": "No images provided"}), 400
|
||||
|
||||
uploaded = []
|
||||
for file in files:
|
||||
if not file or file.filename == "":
|
||||
continue
|
||||
if not allowed_image_file(file.filename):
|
||||
return jsonify({"error": f"Unsupported image type: {file.filename}"}), 400
|
||||
|
||||
original_name = secure_filename(file.filename)
|
||||
extension = original_name.rsplit(".", 1)[1].lower()
|
||||
filename = f"{uuid.uuid4().hex}.{extension}"
|
||||
file.save(os.path.join(UPLOAD_DIR, filename))
|
||||
uploaded.append(
|
||||
{
|
||||
"filename": filename,
|
||||
"originalName": original_name,
|
||||
"url": f"/uploads/{filename}",
|
||||
}
|
||||
)
|
||||
|
||||
if not uploaded:
|
||||
return jsonify({"error": "No valid images provided"}), 400
|
||||
return jsonify({"images": uploaded}), 201
|
||||
|
||||
|
||||
@app.route("/uploads/<path:filename>")
|
||||
def uploaded_file(filename):
|
||||
return send_from_directory(UPLOAD_DIR, filename)
|
||||
|
||||
# ==================== Health and root ====================
|
||||
@app.route("/api/journeys/health", methods=["GET"])
|
||||
def health_check():
|
||||
|
||||
Reference in new issue
Block a user