Files
SaaS-PDF/backend/app/routes/image.py
Your Name 85d98381df feat: Initialize frontend with React, Vite, and Tailwind CSS
- Set up main entry point for React application.
- Create About, Home, NotFound, Privacy, and Terms pages with SEO support.
- Implement API service for file uploads and task management.
- Add global styles using Tailwind CSS.
- Create utility functions for SEO and text processing.
- Configure Vite for development and production builds.
- Set up Nginx configuration for serving frontend and backend.
- Add scripts for cleanup of expired files and sitemap generation.
- Implement deployment script for production environment.
2026-02-28 23:31:19 +02:00

123 lines
3.9 KiB
Python

"""Image processing routes."""
from flask import Blueprint, request, jsonify
from app.extensions import limiter
from app.utils.file_validator import validate_file, FileValidationError
from app.utils.sanitizer import generate_safe_path
from app.tasks.image_tasks import convert_image_task, resize_image_task
image_bp = Blueprint("image", __name__)
ALLOWED_IMAGE_TYPES = ["png", "jpg", "jpeg", "webp"]
ALLOWED_OUTPUT_FORMATS = ["jpg", "png", "webp"]
@image_bp.route("/convert", methods=["POST"])
@limiter.limit("10/minute")
def convert_image_route():
"""
Convert an image to a different format.
Accepts: multipart/form-data with:
- 'file': Image file (PNG, JPG, JPEG, WebP)
- 'format': Target format ("jpg", "png", "webp")
- 'quality' (optional): Quality 1-100 (default: 85)
Returns: JSON with task_id for polling
"""
if "file" not in request.files:
return jsonify({"error": "No file provided."}), 400
file = request.files["file"]
output_format = request.form.get("format", "").lower()
quality = request.form.get("quality", "85")
# Validate output format
if output_format not in ALLOWED_OUTPUT_FORMATS:
return jsonify({
"error": f"Invalid format. Supported: {', '.join(ALLOWED_OUTPUT_FORMATS)}"
}), 400
# Validate quality
try:
quality = max(1, min(100, int(quality)))
except ValueError:
quality = 85
try:
original_filename, ext = validate_file(file, allowed_types=ALLOWED_IMAGE_TYPES)
except FileValidationError as e:
return jsonify({"error": e.message}), e.code
# Save file
task_id, input_path = generate_safe_path(ext, folder_type="upload")
file.save(input_path)
# Dispatch task
task = convert_image_task.delay(
input_path, task_id, original_filename, output_format, quality
)
return jsonify({
"task_id": task.id,
"message": "Image conversion started. Poll /api/tasks/{task_id}/status for progress.",
}), 202
@image_bp.route("/resize", methods=["POST"])
@limiter.limit("10/minute")
def resize_image_route():
"""
Resize an image.
Accepts: multipart/form-data with:
- 'file': Image file
- 'width' (optional): Target width
- 'height' (optional): Target height
- 'quality' (optional): Quality 1-100 (default: 85)
Returns: JSON with task_id for polling
"""
if "file" not in request.files:
return jsonify({"error": "No file provided."}), 400
file = request.files["file"]
width = request.form.get("width")
height = request.form.get("height")
quality = request.form.get("quality", "85")
# Validate dimensions
try:
width = int(width) if width else None
height = int(height) if height else None
except ValueError:
return jsonify({"error": "Width and height must be integers."}), 400
if width is None and height is None:
return jsonify({"error": "At least one of width or height is required."}), 400
if width and (width < 1 or width > 10000):
return jsonify({"error": "Width must be between 1 and 10000."}), 400
if height and (height < 1 or height > 10000):
return jsonify({"error": "Height must be between 1 and 10000."}), 400
try:
quality = max(1, min(100, int(quality)))
except ValueError:
quality = 85
try:
original_filename, ext = validate_file(file, allowed_types=ALLOWED_IMAGE_TYPES)
except FileValidationError as e:
return jsonify({"error": e.message}), e.code
task_id, input_path = generate_safe_path(ext, folder_type="upload")
file.save(input_path)
task = resize_image_task.delay(
input_path, task_id, original_filename, width, height, quality
)
return jsonify({
"task_id": task.id,
"message": "Image resize started. Poll /api/tasks/{task_id}/status for progress.",
}), 202