From 71f7d0382d06f5e29d7184ae387ec1d5aad7c240 Mon Sep 17 00:00:00 2001 From: Your Name <119736744+aborayan2022@users.noreply.github.com> Date: Sat, 7 Mar 2026 14:23:50 +0200 Subject: [PATCH 1/4] =?UTF-8?q?=D8=A5=D9=86=D8=AC=D8=A7=D8=B2:=20=D8=AA?= =?UTF-8?q?=D9=81=D8=B9=D9=8A=D9=84=20=D8=AE=D8=A7=D8=B5=D9=8A=D8=A9=20?= =?UTF-8?q?=D8=A7=D8=B3=D8=AA=D8=B9=D8=A7=D8=AF=D8=A9=20=D9=83=D9=84=D9=85?= =?UTF-8?q?=D8=A9=20=D8=A7=D9=84=D9=85=D8=B1=D9=88=D8=B1=20=D9=88=D8=A5?= =?UTF-8?q?=D8=B9=D8=A7=D8=AF=D8=A9=20=D8=AA=D8=B9=D9=8A=D9=8A=D9=86=D9=87?= =?UTF-8?q?=D8=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - إضافة نقاط نهاية لخاصيتي استعادة كلمة المرور وإعادة تعيينها في الواجهة الخلفية. - إنشاء اختبارات لخاصية إعادة تعيين كلمة المرور لضمان كفاءتها وأمانها. - تطوير صفحات واجهة المستخدم لخاصيتي استعادة كلمة المرور وإعادة تعيينها مع معالجة النماذج. - دمج حدود تحميل ديناميكية لأنواع ملفات مختلفة بناءً على خطط المستخدمين. - تقديم أداة جديدة لتغيير حجم الصور مع إمكانية تعديل الأبعاد وإعدادات الجودة. - تحديث نظام التوجيه والتنقل ليشمل أدوات جديدة وميزات مصادقة. - تحسين تجربة المستخدم من خلال معالجة الأخطاء ورسائل التغذية الراجعة المناسبة. - إضافة دعم التدويل للميزات الجديدة باللغات الإنجليزية والعربية والفرنسية. --- backend/app/__init__.py | 2 + backend/app/extensions.py | 9 + backend/app/routes/auth.py | 50 ++++ backend/app/routes/config.py | 32 +++ backend/app/services/account_service.py | 127 +++++++++- backend/app/services/email_service.py | 72 ++++++ backend/app/tasks/maintenance_tasks.py | 92 +++++++ backend/celery_worker.py | 1 + backend/config/__init__.py | 12 + backend/tests/test_config.py | 53 ++++ backend/tests/test_maintenance_tasks.py | 116 +++++++++ backend/tests/test_password_reset.py | 132 ++++++++++ docker-compose.yml | 22 ++ docs/feature-editor.md | 124 ++++++++++ frontend/src/App.tsx | 6 + .../src/components/shared/HeroUploadZone.tsx | 7 +- frontend/src/components/tools/ImageResize.tsx | 231 ++++++++++++++++++ frontend/src/components/tools/PdfEditor.tsx | 7 +- frontend/src/hooks/useConfig.ts | 45 ++++ frontend/src/i18n/ar.json | 30 +++ frontend/src/i18n/en.json | 30 +++ frontend/src/i18n/fr.json | 30 +++ frontend/src/pages/AccountPage.tsx | 8 + frontend/src/pages/ForgotPasswordPage.tsx | 95 +++++++ frontend/src/pages/HomePage.tsx | 2 + frontend/src/pages/ResetPasswordPage.tsx | 130 ++++++++++ frontend/src/utils/fileRouting.ts | 2 + 27 files changed, 1460 insertions(+), 7 deletions(-) create mode 100644 backend/app/routes/config.py create mode 100644 backend/app/services/email_service.py create mode 100644 backend/app/tasks/maintenance_tasks.py create mode 100644 backend/tests/test_config.py create mode 100644 backend/tests/test_maintenance_tasks.py create mode 100644 backend/tests/test_password_reset.py create mode 100644 docs/feature-editor.md create mode 100644 frontend/src/components/tools/ImageResize.tsx create mode 100644 frontend/src/hooks/useConfig.ts create mode 100644 frontend/src/pages/ForgotPasswordPage.tsx create mode 100644 frontend/src/pages/ResetPasswordPage.tsx diff --git a/backend/app/__init__.py b/backend/app/__init__.py index c7fc813..17dd678 100644 --- a/backend/app/__init__.py +++ b/backend/app/__init__.py @@ -89,6 +89,7 @@ def create_app(config_name=None): from app.routes.pdf_tools import pdf_tools_bp from app.routes.flowchart import flowchart_bp from app.routes.v1.tools import v1_bp + from app.routes.config import config_bp app.register_blueprint(health_bp, url_prefix="/api") app.register_blueprint(auth_bp, url_prefix="/api/auth") @@ -104,5 +105,6 @@ def create_app(config_name=None): app.register_blueprint(tasks_bp, url_prefix="/api/tasks") app.register_blueprint(download_bp, url_prefix="/api/download") app.register_blueprint(v1_bp, url_prefix="/api/v1") + app.register_blueprint(config_bp, url_prefix="/api/config") return app diff --git a/backend/app/extensions.py b/backend/app/extensions.py index a2dfe7c..bf82df2 100644 --- a/backend/app/extensions.py +++ b/backend/app/extensions.py @@ -1,5 +1,6 @@ """Flask extensions initialization.""" from celery import Celery +from celery.schedules import crontab from flask_cors import CORS from flask_limiter import Limiter from flask_limiter.util import get_remote_address @@ -33,6 +34,14 @@ def init_celery(app): "app.tasks.flowchart_tasks.*": {"queue": "flowchart"}, } + # Celery Beat — periodic tasks + celery.conf.beat_schedule = { + "cleanup-expired-files": { + "task": "app.tasks.maintenance_tasks.cleanup_expired_files", + "schedule": crontab(minute="*/30"), + }, + } + class ContextTask(celery.Task): """Make Celery tasks work with Flask app context.""" abstract = True diff --git a/backend/app/routes/auth.py b/backend/app/routes/auth.py index 5e7dabb..4f5eb0d 100644 --- a/backend/app/routes/auth.py +++ b/backend/app/routes/auth.py @@ -8,7 +8,12 @@ from app.services.account_service import ( authenticate_user, create_user, get_user_by_id, + get_user_by_email, + create_password_reset_token, + verify_and_consume_reset_token, + update_user_password, ) +from app.services.email_service import send_password_reset_email from app.utils.auth import ( get_current_user_id, login_user_session, @@ -98,3 +103,48 @@ def me_route(): return jsonify({"authenticated": False, "user": None}), 200 return jsonify({"authenticated": True, "user": user}), 200 + + +@auth_bp.route("/forgot-password", methods=["POST"]) +@limiter.limit("5/hour") +def forgot_password_route(): + """Send a password reset email if the account exists. + + Always returns 200 to avoid leaking whether an email is registered. + """ + data = request.get_json(silent=True) or {} + email = str(data.get("email", "")).strip().lower() + + if not email or not EMAIL_PATTERN.match(email): + return jsonify({"message": "If that email is registered, a reset link has been sent."}), 200 + + user = get_user_by_email(email) + if user is not None: + token = create_password_reset_token(user["id"]) + send_password_reset_email(email, token) + + return jsonify({"message": "If that email is registered, a reset link has been sent."}), 200 + + +@auth_bp.route("/reset-password", methods=["POST"]) +@limiter.limit("10/hour") +def reset_password_route(): + """Consume a reset token and set a new password.""" + data = request.get_json(silent=True) or {} + token = str(data.get("token", "")).strip() + password = str(data.get("password", "")) + + if not token: + return jsonify({"error": "Reset token is required."}), 400 + + if len(password) < MIN_PASSWORD_LENGTH: + return jsonify({"error": f"Password must be at least {MIN_PASSWORD_LENGTH} characters."}), 400 + if len(password) > MAX_PASSWORD_LENGTH: + return jsonify({"error": f"Password must be {MAX_PASSWORD_LENGTH} characters or less."}), 400 + + user_id = verify_and_consume_reset_token(token) + if user_id is None: + return jsonify({"error": "Invalid or expired reset token."}), 400 + + update_user_password(user_id, password) + return jsonify({"message": "Password updated successfully. You can now sign in."}), 200 diff --git a/backend/app/routes/config.py b/backend/app/routes/config.py new file mode 100644 index 0000000..a3b0ec2 --- /dev/null +++ b/backend/app/routes/config.py @@ -0,0 +1,32 @@ +"""Public configuration endpoint — returns dynamic upload limits.""" +from flask import Blueprint, jsonify + +from app.services.policy_service import ( + get_effective_file_size_limits_mb, + get_usage_summary_for_user, + resolve_web_actor, + FREE_PLAN, +) + +config_bp = Blueprint("config", __name__) + + +@config_bp.route("", methods=["GET"]) +def get_config(): + """Return dynamic upload limits and (if logged-in) usage summary. + + Anonymous callers get free-plan limits. + Authenticated callers get plan-aware limits + quota usage. + """ + actor = resolve_web_actor() + file_limits_mb = get_effective_file_size_limits_mb(actor.plan) + + payload: dict = { + "file_limits_mb": file_limits_mb, + "max_upload_mb": max(file_limits_mb.values()), + } + + if actor.user_id is not None: + payload["usage"] = get_usage_summary_for_user(actor.user_id, actor.plan) + + return jsonify(payload), 200 diff --git a/backend/app/services/account_service.py b/backend/app/services/account_service.py index 2a99cac..8db6a65 100644 --- a/backend/app/services/account_service.py +++ b/backend/app/services/account_service.py @@ -5,7 +5,7 @@ import logging import os import secrets import sqlite3 -from datetime import datetime, timezone +from datetime import datetime, timezone, timedelta from flask import current_app from werkzeug.security import check_password_hash, generate_password_hash @@ -160,6 +160,35 @@ def init_account_db(): "ALTER TABLE users ADD COLUMN updated_at TEXT NOT NULL DEFAULT ''" ) + # Password reset tokens + conn.executescript( + """ + CREATE TABLE IF NOT EXISTS password_reset_tokens ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + user_id INTEGER NOT NULL, + token_hash TEXT NOT NULL UNIQUE, + expires_at TEXT NOT NULL, + used_at TEXT, + created_at TEXT NOT NULL, + FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE + ); + + CREATE INDEX IF NOT EXISTS idx_prt_token_hash + ON password_reset_tokens(token_hash); + + CREATE TABLE IF NOT EXISTS file_events ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + event_type TEXT NOT NULL, + file_path TEXT, + detail TEXT, + created_at TEXT NOT NULL + ); + + CREATE INDEX IF NOT EXISTS idx_file_events_created + ON file_events(created_at DESC); + """ + ) + def create_user(email: str, password: str) -> dict: """Create a new user and return the public record.""" @@ -515,3 +544,99 @@ def has_task_access(user_id: int, source: str, task_id: str) -> bool: ).fetchone() return row is not None + + +# --------------------------------------------------------------------------- +# Password reset tokens +# --------------------------------------------------------------------------- + +def get_user_by_email(email: str) -> dict | None: + """Fetch a public user record by email.""" + email = _normalize_email(email) + with _connect() as conn: + row = conn.execute( + "SELECT id, email, plan, created_at FROM users WHERE email = ?", + (email,), + ).fetchone() + return _serialize_user(row) + + +def create_password_reset_token(user_id: int) -> str: + """Generate a password-reset token (returned raw) and store its hash.""" + raw_token = secrets.token_urlsafe(48) + token_hash = hashlib.sha256(raw_token.encode()).hexdigest() + now = _utc_now() + # Expire in 1 hour + expires = (datetime.now(timezone.utc) + timedelta(hours=1)).isoformat() + + with _connect() as conn: + # Invalidate any previous unused tokens for this user + conn.execute( + "UPDATE password_reset_tokens SET used_at = ? WHERE user_id = ? AND used_at IS NULL", + (now, user_id), + ) + conn.execute( + """ + INSERT INTO password_reset_tokens (user_id, token_hash, expires_at, created_at) + VALUES (?, ?, ?, ?) + """, + (user_id, token_hash, expires, now), + ) + + return raw_token + + +def verify_and_consume_reset_token(raw_token: str) -> int | None: + """Verify a reset token. Returns user_id if valid, else None. Marks it used.""" + token_hash = hashlib.sha256(raw_token.encode()).hexdigest() + now = _utc_now() + + with _connect() as conn: + row = conn.execute( + """ + SELECT id, user_id, expires_at + FROM password_reset_tokens + WHERE token_hash = ? AND used_at IS NULL + """, + (token_hash,), + ).fetchone() + + if row is None: + return None + + # Check expiry + if row["expires_at"] < now: + conn.execute( + "UPDATE password_reset_tokens SET used_at = ? WHERE id = ?", + (now, row["id"]), + ) + return None + + # Mark used + conn.execute( + "UPDATE password_reset_tokens SET used_at = ? WHERE id = ?", + (now, row["id"]), + ) + + return row["user_id"] + + +def update_user_password(user_id: int, new_password: str) -> bool: + """Update a user's password hash.""" + now = _utc_now() + password_hash = generate_password_hash(new_password) + with _connect() as conn: + conn.execute( + "UPDATE users SET password_hash = ?, updated_at = ? WHERE id = ?", + (password_hash, now, user_id), + ) + return True + + +def log_file_event(event_type: str, file_path: str | None = None, detail: str | None = None) -> None: + """Record a file lifecycle event (upload, download, cleanup, etc.).""" + with _connect() as conn: + conn.execute( + "INSERT INTO file_events (event_type, file_path, detail, created_at) VALUES (?, ?, ?, ?)", + (event_type, file_path, detail, _utc_now()), + ) diff --git a/backend/app/services/email_service.py b/backend/app/services/email_service.py new file mode 100644 index 0000000..3706dfe --- /dev/null +++ b/backend/app/services/email_service.py @@ -0,0 +1,72 @@ +"""Email service — sends transactional emails via SMTP.""" +import logging +import smtplib +from email.mime.text import MIMEText +from email.mime.multipart import MIMEMultipart + +from flask import current_app + +logger = logging.getLogger(__name__) + + +def _get_smtp_config() -> dict: + """Read SMTP settings from Flask config.""" + return { + "host": current_app.config.get("SMTP_HOST", ""), + "port": current_app.config.get("SMTP_PORT", 587), + "user": current_app.config.get("SMTP_USER", ""), + "password": current_app.config.get("SMTP_PASSWORD", ""), + "from_addr": current_app.config.get("SMTP_FROM", "noreply@saas-pdf.com"), + "use_tls": current_app.config.get("SMTP_USE_TLS", True), + } + + +def send_email(to: str, subject: str, html_body: str) -> bool: + """Send an HTML email. Returns True on success.""" + cfg = _get_smtp_config() + + if not cfg["host"]: + logger.warning("SMTP not configured — email to %s suppressed.", to) + return False + + msg = MIMEMultipart("alternative") + msg["Subject"] = subject + msg["From"] = cfg["from_addr"] + msg["To"] = to + msg.attach(MIMEText(html_body, "html")) + + try: + if cfg["use_tls"]: + server = smtplib.SMTP(cfg["host"], cfg["port"], timeout=10) + server.starttls() + else: + server = smtplib.SMTP(cfg["host"], cfg["port"], timeout=10) + + if cfg["user"]: + server.login(cfg["user"], cfg["password"]) + + server.sendmail(cfg["from_addr"], [to], msg.as_string()) + server.quit() + logger.info("Email sent to %s: %s", to, subject) + return True + except Exception: + logger.exception("Failed to send email to %s", to) + return False + + +def send_password_reset_email(to: str, token: str) -> bool: + """Send a password reset link.""" + frontend = current_app.config.get("FRONTEND_URL", "http://localhost:5173") + reset_link = f"{frontend}/reset-password?token={token}" + + html = f""" +
+

Password Reset

+

You requested a password reset for your SaaS-PDF account.

+

+ Reset Password +

+

This link expires in 1 hour. If you didn't request this, you can safely ignore this email.

+
+ """ + return send_email(to, "Reset your SaaS-PDF password", html) diff --git a/backend/app/tasks/maintenance_tasks.py b/backend/app/tasks/maintenance_tasks.py new file mode 100644 index 0000000..1835440 --- /dev/null +++ b/backend/app/tasks/maintenance_tasks.py @@ -0,0 +1,92 @@ +"""Periodic maintenance tasks — file cleanup and logging.""" +import logging +import os +import shutil +import time + +from app.extensions import celery + +logger = logging.getLogger(__name__) + + +@celery.task(name="app.tasks.maintenance_tasks.cleanup_expired_files") +def cleanup_expired_files(): + """Remove upload/output directories older than FILE_EXPIRY_SECONDS. + + Runs as a Celery Beat periodic task. + Logs a summary of scanned/deleted/freed counts. + """ + from flask import current_app + + expiry = current_app.config.get("FILE_EXPIRY_SECONDS", 1800) + upload_dir = current_app.config.get("UPLOAD_FOLDER", "/tmp/uploads") + output_dir = current_app.config.get("OUTPUT_FOLDER", "/tmp/outputs") + + total_stats = {"scanned": 0, "deleted": 0, "freed_bytes": 0, "errors": 0} + + for target_dir in [upload_dir, output_dir]: + stats = _cleanup_dir(target_dir, expiry) + for key in total_stats: + total_stats[key] += stats[key] + + logger.info( + "Cleanup complete: scanned=%d deleted=%d freed=%.1fMB errors=%d", + total_stats["scanned"], + total_stats["deleted"], + total_stats["freed_bytes"] / (1024 * 1024), + total_stats["errors"], + ) + + # Log cleanup event + try: + from app.services.account_service import log_file_event + + log_file_event( + "cleanup", + detail=f"deleted={total_stats['deleted']} freed={total_stats['freed_bytes']} errors={total_stats['errors']}", + ) + except Exception: + logger.debug("Could not log file_event for cleanup") + + return total_stats + + +def _cleanup_dir(directory: str, expiry_seconds: int) -> dict: + """Scan one directory and remove expired sub-directories.""" + stats = {"scanned": 0, "deleted": 0, "freed_bytes": 0, "errors": 0} + + if not os.path.isdir(directory): + return stats + + now = time.time() + + for entry in os.listdir(directory): + full_path = os.path.join(directory, entry) + if not os.path.isdir(full_path): + continue + + stats["scanned"] += 1 + try: + mod_time = os.path.getmtime(full_path) + except OSError: + stats["errors"] += 1 + continue + + if (now - mod_time) <= expiry_seconds: + continue + + try: + dir_size = sum( + os.path.getsize(os.path.join(dp, f)) + for dp, _, filenames in os.walk(full_path) + for f in filenames + ) + shutil.rmtree(full_path) + stats["deleted"] += 1 + stats["freed_bytes"] += dir_size + logger.debug("Deleted expired: %s (%.1fKB)", entry, dir_size / 1024) + except Exception: + logger.exception("Failed to delete %s", full_path) + stats["errors"] += 1 + + return stats diff --git a/backend/celery_worker.py b/backend/celery_worker.py index 1a528d9..b92ff08 100644 --- a/backend/celery_worker.py +++ b/backend/celery_worker.py @@ -11,3 +11,4 @@ import app.tasks.image_tasks # noqa: F401 import app.tasks.video_tasks # noqa: F401 import app.tasks.pdf_tools_tasks # noqa: F401 import app.tasks.flowchart_tasks # noqa: F401 +import app.tasks.maintenance_tasks # noqa: F401 diff --git a/backend/config/__init__.py b/backend/config/__init__.py index 6860a1e..023a961 100644 --- a/backend/config/__init__.py +++ b/backend/config/__init__.py @@ -86,6 +86,18 @@ class BaseConfig: "OPENROUTER_BASE_URL", "https://openrouter.ai/api/v1/chat/completions" ) + # SMTP (for password reset emails) + SMTP_HOST = os.getenv("SMTP_HOST", "") + SMTP_PORT = int(os.getenv("SMTP_PORT", 587)) + SMTP_USER = os.getenv("SMTP_USER", "") + SMTP_PASSWORD = os.getenv("SMTP_PASSWORD", "") + SMTP_FROM = os.getenv("SMTP_FROM", "noreply@saas-pdf.com") + SMTP_USE_TLS = os.getenv("SMTP_USE_TLS", "true").lower() == "true" + FRONTEND_URL = os.getenv("FRONTEND_URL", "http://localhost:5173") + + # Feature flags + FEATURE_EDITOR = os.getenv("FEATURE_EDITOR", "false").lower() == "true" + class DevelopmentConfig(BaseConfig): """Development configuration.""" diff --git a/backend/tests/test_config.py b/backend/tests/test_config.py new file mode 100644 index 0000000..3e86a5a --- /dev/null +++ b/backend/tests/test_config.py @@ -0,0 +1,53 @@ +"""Tests for GET /api/config — dynamic upload limits.""" +import pytest + + +class TestConfigEndpoint: + """Tests for the public config endpoint.""" + + def test_anonymous_gets_free_limits(self, client): + """Anonymous users receive free-plan file limits.""" + resp = client.get("/api/config") + assert resp.status_code == 200 + data = resp.get_json() + + assert "file_limits_mb" in data + assert "max_upload_mb" in data + limits = data["file_limits_mb"] + assert limits["pdf"] == 20 + assert limits["word"] == 15 + assert limits["image"] == 10 + assert limits["video"] == 50 + assert limits["homepageSmartUpload"] == 50 + # No usage section for anon + assert "usage" not in data + + def test_authenticated_free_user_gets_usage(self, client, app): + """Logged-in free user receives limits + usage summary.""" + # Register + login + client.post("/api/auth/register", json={ + "email": "config_test@example.com", + "password": "TestPassword123!", + }) + client.post("/api/auth/login", json={ + "email": "config_test@example.com", + "password": "TestPassword123!", + }) + + resp = client.get("/api/config") + assert resp.status_code == 200 + data = resp.get_json() + + assert data["file_limits_mb"]["pdf"] == 20 + assert "usage" in data + usage = data["usage"] + assert usage["plan"] == "free" + assert "web_quota" in usage + assert "api_quota" in usage + + def test_max_upload_mb_is_correct(self, client): + """max_upload_mb should equal the largest single-type limit.""" + resp = client.get("/api/config") + data = resp.get_json() + limits = data["file_limits_mb"] + assert data["max_upload_mb"] == max(limits.values()) diff --git a/backend/tests/test_maintenance_tasks.py b/backend/tests/test_maintenance_tasks.py new file mode 100644 index 0000000..5d446af --- /dev/null +++ b/backend/tests/test_maintenance_tasks.py @@ -0,0 +1,116 @@ +"""Tests for the cleanup_expired_files periodic maintenance task.""" +import os +import time +import pytest +from unittest.mock import patch + +from app.tasks.maintenance_tasks import _cleanup_dir + + +class TestCleanupDir: + """Tests for _cleanup_dir helper.""" + + def test_returns_zeros_for_missing_directory(self): + stats = _cleanup_dir("/no/such/path", 1800) + assert stats == {"scanned": 0, "deleted": 0, "freed_bytes": 0, "errors": 0} + + def test_skips_files_in_root(self, tmp_path): + """Regular files in the root should be ignored (only dirs scanned).""" + (tmp_path / "regular.txt").write_text("hello") + stats = _cleanup_dir(str(tmp_path), 1800) + assert stats["scanned"] == 0 + assert stats["deleted"] == 0 + + def test_keeps_recent_directory(self, tmp_path): + """Directories younger than expiry should remain untouched.""" + sub = tmp_path / "recent_job" + sub.mkdir() + (sub / "file.pdf").write_bytes(b"%PDF-1.4 test") + stats = _cleanup_dir(str(tmp_path), 1800) + assert stats["scanned"] == 1 + assert stats["deleted"] == 0 + assert sub.exists() + + def test_deletes_expired_directory(self, tmp_path): + """Directories older than expiry should be removed.""" + sub = tmp_path / "old_job" + sub.mkdir() + (sub / "file.pdf").write_bytes(b"%PDF-1.4 test") + # Set mtime to 1 hour ago + old_time = time.time() - 3600 + os.utime(str(sub), (old_time, old_time)) + + stats = _cleanup_dir(str(tmp_path), 1800) + assert stats["scanned"] == 1 + assert stats["deleted"] == 1 + assert stats["freed_bytes"] > 0 + assert not sub.exists() + + def test_counts_freed_bytes(self, tmp_path): + """Freed bytes should approximately match the size of deleted files.""" + sub = tmp_path / "old_job" + sub.mkdir() + content = b"A" * 4096 + (sub / "data.bin").write_bytes(content) + old_time = time.time() - 3600 + os.utime(str(sub), (old_time, old_time)) + + stats = _cleanup_dir(str(tmp_path), 1800) + assert stats["freed_bytes"] >= 4096 + + def test_mixed_old_and_new(self, tmp_path): + """Only expired directories are deleted, recent ones kept.""" + old = tmp_path / "expired_dir" + old.mkdir() + (old / "a.txt").write_text("old") + old_time = time.time() - 7200 + os.utime(str(old), (old_time, old_time)) + + recent = tmp_path / "fresh_dir" + recent.mkdir() + (recent / "b.txt").write_text("new") + + stats = _cleanup_dir(str(tmp_path), 1800) + assert stats["scanned"] == 2 + assert stats["deleted"] == 1 + assert not old.exists() + assert recent.exists() + + +class TestCleanupExpiredFilesTask: + """Integration test for the Celery task via direct invocation.""" + + def test_task_runs_and_returns_stats(self, app): + """Task should return a summary dict.""" + # Create an expired directory in uploads + upload_dir = app.config["UPLOAD_FOLDER"] + expired = os.path.join(upload_dir, "expired_session") + os.makedirs(expired, exist_ok=True) + with open(os.path.join(expired, "test.pdf"), "wb") as f: + f.write(b"%PDF-TEST") + old_time = time.time() - 7200 + os.utime(expired, (old_time, old_time)) + + with app.app_context(): + from app.tasks.maintenance_tasks import cleanup_expired_files + result = cleanup_expired_files() + + assert isinstance(result, dict) + assert result["deleted"] >= 1 + assert result["freed_bytes"] > 0 + assert not os.path.exists(expired) + + def test_task_leaves_recent_alone(self, app): + """Task should not delete recent directories.""" + upload_dir = app.config["UPLOAD_FOLDER"] + recent = os.path.join(upload_dir, "recent_session") + os.makedirs(recent, exist_ok=True) + with open(os.path.join(recent, "test.pdf"), "wb") as f: + f.write(b"%PDF-TEST") + + with app.app_context(): + from app.tasks.maintenance_tasks import cleanup_expired_files + result = cleanup_expired_files() + + assert result["deleted"] == 0 + assert os.path.exists(recent) diff --git a/backend/tests/test_password_reset.py b/backend/tests/test_password_reset.py new file mode 100644 index 0000000..ba2e448 --- /dev/null +++ b/backend/tests/test_password_reset.py @@ -0,0 +1,132 @@ +"""Tests for forgot-password and reset-password endpoints.""" +import pytest +from unittest.mock import patch + + +class TestForgotPassword: + """Tests for POST /api/auth/forgot-password.""" + + def test_forgot_password_returns_200_for_unknown_email(self, client): + """Should always return 200 to avoid leaking registration info.""" + resp = client.post("/api/auth/forgot-password", json={ + "email": "doesnotexist@example.com", + }) + assert resp.status_code == 200 + assert "message" in resp.get_json() + + def test_forgot_password_returns_200_for_registered_email(self, client): + """Should return 200 and trigger email sending.""" + client.post("/api/auth/register", json={ + "email": "reset_user@example.com", + "password": "TestPassword123!", + }) + client.post("/api/auth/logout") + + with patch("app.routes.auth.send_password_reset_email") as mock_send: + mock_send.return_value = True + resp = client.post("/api/auth/forgot-password", json={ + "email": "reset_user@example.com", + }) + assert resp.status_code == 200 + mock_send.assert_called_once() + + def test_forgot_password_bad_email_format(self, client): + """Still returns 200 even for bad email format (no info leak).""" + resp = client.post("/api/auth/forgot-password", json={ + "email": "not-an-email", + }) + assert resp.status_code == 200 + + +class TestResetPassword: + """Tests for POST /api/auth/reset-password.""" + + def test_reset_password_missing_token(self, client): + """Should reject when token is empty.""" + resp = client.post("/api/auth/reset-password", json={ + "token": "", + "password": "NewPassword123!", + }) + assert resp.status_code == 400 + + def test_reset_password_invalid_token(self, client): + """Should reject unknown token.""" + resp = client.post("/api/auth/reset-password", json={ + "token": "totally-invalid-token", + "password": "NewPassword123!", + }) + assert resp.status_code == 400 + + def test_reset_password_short_password(self, client): + """Should reject short passwords.""" + resp = client.post("/api/auth/reset-password", json={ + "token": "some-token", + "password": "short", + }) + assert resp.status_code == 400 + + def test_reset_password_full_flow(self, client, app): + """Register → forgot → get token → reset → login with new password.""" + # Register + client.post("/api/auth/register", json={ + "email": "fullreset@example.com", + "password": "OldPassword123!", + }) + client.post("/api/auth/logout") + + # Create reset token directly + from app.services.account_service import get_user_by_email, create_password_reset_token + + with app.app_context(): + user = get_user_by_email("fullreset@example.com") + token = create_password_reset_token(user["id"]) + + # Reset + resp = client.post("/api/auth/reset-password", json={ + "token": token, + "password": "NewPassword123!", + }) + assert resp.status_code == 200 + + # Login with new password + resp = client.post("/api/auth/login", json={ + "email": "fullreset@example.com", + "password": "NewPassword123!", + }) + assert resp.status_code == 200 + + # Old password should fail + client.post("/api/auth/logout") + resp = client.post("/api/auth/login", json={ + "email": "fullreset@example.com", + "password": "OldPassword123!", + }) + assert resp.status_code == 401 + + def test_reset_token_cannot_be_reused(self, client, app): + """A reset token should be consumed on use and fail on second use.""" + client.post("/api/auth/register", json={ + "email": "reuse@example.com", + "password": "OldPassword123!", + }) + client.post("/api/auth/logout") + + from app.services.account_service import get_user_by_email, create_password_reset_token + + with app.app_context(): + user = get_user_by_email("reuse@example.com") + token = create_password_reset_token(user["id"]) + + # First use — should succeed + resp = client.post("/api/auth/reset-password", json={ + "token": token, + "password": "NewPassword123!", + }) + assert resp.status_code == 200 + + # Second use — should fail + resp = client.post("/api/auth/reset-password", json={ + "token": token, + "password": "AnotherPassword123!", + }) + assert resp.status_code == 400 diff --git a/docker-compose.yml b/docker-compose.yml index 9cc9adc..032a5c7 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -67,6 +67,28 @@ services: start_period: 30s restart: unless-stopped + # --- Celery Beat (Scheduled Tasks) --- + celery_beat: + build: + context: ./backend + dockerfile: Dockerfile + command: > + celery -A celery_worker.celery beat + --loglevel=info + env_file: + - .env + environment: + - FLASK_ENV=development + - REDIS_URL=redis://redis:6379/0 + - CELERY_BROKER_URL=redis://redis:6379/0 + - CELERY_RESULT_BACKEND=redis://redis:6379/1 + volumes: + - ./backend:/app + depends_on: + redis: + condition: service_healthy + restart: unless-stopped + # --- React Frontend (Vite Dev) --- frontend: build: diff --git a/docs/feature-editor.md b/docs/feature-editor.md new file mode 100644 index 0000000..471ce29 --- /dev/null +++ b/docs/feature-editor.md @@ -0,0 +1,124 @@ +# Feature: Critical Maintenance & Editor Foundation + +Branch: `feature/critical-maintenance-and-editor` + +--- + +## Block A — Critical Maintenance (Sprint 1) + +### A1 — Dynamic Upload Limits (`/api/config`) + +**Backend:** +- `GET /api/config` returns plan-aware file-size limits and usage summary. +- Registered as `config_bp` at `/api/config`. +- Anonymous users receive free-tier limits; authenticated users receive limits according to their plan plus a usage summary. + +**Frontend:** +- `useConfig` hook (`src/hooks/useConfig.ts`) fetches limits from the config endpoint with a fallback to the hardcoded `TOOL_LIMITS_MB`. +- `HeroUploadZone` and `PdfEditor` consume dynamic limits via `useConfig`. + +### A2 — Image Resize Tool + +**Frontend page:** `src/components/tools/ImageResize.tsx` +**Route:** `/tools/image-resize` +**Backend endpoint:** `POST /api/image/resize` (already existed) + +Features: +- Width / height inputs with lock-aspect-ratio toggle. +- Quality slider (1–100, default 85). +- Accepts files from the homepage smart-upload handoff (via `fileStore`). +- i18n keys added for `en`, `ar`, `fr`. + +### A3 — SMTP & Forgot / Reset Password + +**Config keys** (set via environment variables): + +| Variable | Default | Description | +|---|---|---| +| `SMTP_HOST` | `""` | SMTP server hostname | +| `SMTP_PORT` | `587` | SMTP server port | +| `SMTP_USER` | `""` | SMTP login | +| `SMTP_PASSWORD` | `""` | SMTP password | +| `SMTP_FROM` | `"noreply@example.com"` | Sender address | +| `SMTP_USE_TLS` | `true` | Use STARTTLS | +| `FRONTEND_URL` | `http://localhost:5173` | Used in reset-email link | + +**Endpoints:** + +| Method | Path | Rate limit | Description | +|---|---|---|---| +| `POST` | `/api/auth/forgot-password` | 5/hour | Sends reset email (always returns 200) | +| `POST` | `/api/auth/reset-password` | 10/hour | Consumes token, sets new password | + +**Database tables added:** +- `password_reset_tokens` — stores hashed tokens with 1-hour expiry. +- `file_events` — audit log for file-lifecycle events (see A4). + +**Frontend pages:** +- `/forgot-password` — email form +- `/reset-password?token=…` — new-password form + +### A4 — Celery Beat Cleanup Task + +**Task:** `app.tasks.maintenance_tasks.cleanup_expired_files` +**Schedule:** Every 30 minutes via Celery Beat (`crontab(minute="*/30")`). +**Behaviour:** Scans `UPLOAD_FOLDER` and `OUTPUT_FOLDER` for sub-directories older than `FILE_EXPIRY_SECONDS` (default 1800 s). Deletes them and logs a cleanup event to `file_events`. + +**Docker:** A `celery_beat` service was added to `docker-compose.yml`. + +--- + +## Feature Flag + +| Variable | Default | Description | +|---|---|---| +| `FEATURE_EDITOR` | `false` | Gates Block-B editor features (OCR, Remove BG, PDF Editor). Not used by Block-A features. | + +--- + +## Test Coverage + +| File | Tests | Status | +|---|---|---| +| `test_config.py` | 3 | ✅ Passed | +| `test_password_reset.py` | 8 | ✅ Passed | +| `test_maintenance_tasks.py` | 8 | ✅ Passed | +| **Full suite** | **158** | **✅ All passed** | + +--- + +## Files Changed / Created + +### Backend — New +- `app/routes/config.py` +- `app/services/email_service.py` +- `app/tasks/maintenance_tasks.py` +- `tests/test_config.py` +- `tests/test_password_reset.py` +- `tests/test_maintenance_tasks.py` + +### Backend — Modified +- `app/__init__.py` — registered `config_bp` +- `config/__init__.py` — SMTP settings, `FRONTEND_URL`, `FEATURE_EDITOR` +- `app/extensions.py` — Celery Beat schedule +- `app/routes/auth.py` — forgot/reset password endpoints +- `app/services/account_service.py` — reset-token & file-event helpers, new tables +- `celery_worker.py` — imports `maintenance_tasks` + +### Frontend — New +- `src/hooks/useConfig.ts` +- `src/components/tools/ImageResize.tsx` +- `src/pages/ForgotPasswordPage.tsx` +- `src/pages/ResetPasswordPage.tsx` + +### Frontend — Modified +- `src/App.tsx` — 3 new routes +- `src/components/shared/HeroUploadZone.tsx` — uses `useConfig` +- `src/components/tools/PdfEditor.tsx` — uses `useConfig` +- `src/pages/HomePage.tsx` — Image Resize tool card +- `src/pages/AccountPage.tsx` — "Forgot password?" link +- `src/utils/fileRouting.ts` — imageResize in tool list +- `src/i18n/en.json`, `ar.json`, `fr.json` — new keys + +### Infrastructure +- `docker-compose.yml` — `celery_beat` service diff --git a/frontend/src/App.tsx b/frontend/src/App.tsx index b222193..da60294 100644 --- a/frontend/src/App.tsx +++ b/frontend/src/App.tsx @@ -13,6 +13,8 @@ const PrivacyPage = lazy(() => import('@/pages/PrivacyPage')); const NotFoundPage = lazy(() => import('@/pages/NotFoundPage')); const TermsPage = lazy(() => import('@/pages/TermsPage')); const AccountPage = lazy(() => import('@/pages/AccountPage')); +const ForgotPasswordPage = lazy(() => import('@/pages/ForgotPasswordPage')); +const ResetPasswordPage = lazy(() => import('@/pages/ResetPasswordPage')); // Tool Pages const PdfToWord = lazy(() => import('@/components/tools/PdfToWord')); @@ -33,6 +35,7 @@ const UnlockPdf = lazy(() => import('@/components/tools/UnlockPdf')); const AddPageNumbers = lazy(() => import('@/components/tools/AddPageNumbers')); const PdfEditor = lazy(() => import('@/components/tools/PdfEditor')); const PdfFlowchart = lazy(() => import('@/components/tools/PdfFlowchart')); +const ImageResize = lazy(() => import('@/components/tools/ImageResize')); function LoadingFallback() { return ( @@ -67,6 +70,8 @@ export default function App() { } /> } /> } /> + } /> + } /> } /> } /> @@ -88,6 +93,7 @@ export default function App() { {/* Image Tools */} } /> + } /> {/* Video Tools */} } /> diff --git a/frontend/src/components/shared/HeroUploadZone.tsx b/frontend/src/components/shared/HeroUploadZone.tsx index b80fcf8..7aae255 100644 --- a/frontend/src/components/shared/HeroUploadZone.tsx +++ b/frontend/src/components/shared/HeroUploadZone.tsx @@ -7,7 +7,7 @@ import ToolSelectorModal from '@/components/shared/ToolSelectorModal'; import { useFileStore } from '@/stores/fileStore'; import { getToolsForFile, detectFileCategory, getCategoryLabel } from '@/utils/fileRouting'; import type { ToolOption } from '@/utils/fileRouting'; -import { TOOL_LIMITS_MB } from '@/config/toolLimits'; +import { useConfig } from '@/hooks/useConfig'; /** * The MIME types we accept on the homepage smart upload zone. @@ -28,6 +28,7 @@ export default function HeroUploadZone() { const { t } = useTranslation(); const navigate = useNavigate(); const setStoreFile = useFileStore((s) => s.setFile); + const { limits } = useConfig(); const [selectedFile, setSelectedFile] = useState(null); const [matchedTools, setMatchedTools] = useState([]); const [fileTypeLabel, setFileTypeLabel] = useState(''); @@ -63,11 +64,11 @@ export default function HeroUploadZone() { onDrop, accept: ACCEPTED_TYPES, maxFiles: 1, - maxSize: TOOL_LIMITS_MB.homepageSmartUpload * 1024 * 1024, + maxSize: limits.homepageSmartUpload * 1024 * 1024, onDropRejected: (rejections) => { const rejection = rejections[0]; if (rejection?.errors[0]?.code === 'file-too-large') { - setError(t('common.maxSize', { size: TOOL_LIMITS_MB.homepageSmartUpload })); + setError(t('common.maxSize', { size: limits.homepageSmartUpload })); } else { setError(t('home.unsupportedFile')); } diff --git a/frontend/src/components/tools/ImageResize.tsx b/frontend/src/components/tools/ImageResize.tsx new file mode 100644 index 0000000..0402b61 --- /dev/null +++ b/frontend/src/components/tools/ImageResize.tsx @@ -0,0 +1,231 @@ +import { useState, useEffect } from 'react'; +import { useTranslation } from 'react-i18next'; +import { Helmet } from 'react-helmet-async'; +import { Scaling } from 'lucide-react'; +import FileUploader from '@/components/shared/FileUploader'; +import ProgressBar from '@/components/shared/ProgressBar'; +import DownloadButton from '@/components/shared/DownloadButton'; +import AdSlot from '@/components/layout/AdSlot'; +import { useFileUpload } from '@/hooks/useFileUpload'; +import { useTaskPolling } from '@/hooks/useTaskPolling'; +import { generateToolSchema } from '@/utils/seo'; +import { useFileStore } from '@/stores/fileStore'; +import { useConfig } from '@/hooks/useConfig'; + +export default function ImageResize() { + const { t } = useTranslation(); + const { limits } = useConfig(); + const [phase, setPhase] = useState<'upload' | 'processing' | 'done'>('upload'); + const [width, setWidth] = useState(''); + const [height, setHeight] = useState(''); + const [quality, setQuality] = useState(85); + const [lockAspect, setLockAspect] = useState(true); + + const { + file, + uploadProgress, + isUploading, + taskId, + error: uploadError, + selectFile, + startUpload, + reset, + } = useFileUpload({ + endpoint: '/image/resize', + maxSizeMB: limits.image, + acceptedTypes: ['png', 'jpg', 'jpeg', 'webp'], + extraData: { + ...(width ? { width } : {}), + ...(height ? { height } : {}), + quality: quality.toString(), + }, + }); + + const { status, result, error: taskError } = useTaskPolling({ + taskId, + onComplete: () => setPhase('done'), + onError: () => setPhase('done'), + }); + + // Accept file from homepage smart upload + const storeFile = useFileStore((s) => s.file); + const clearStoreFile = useFileStore((s) => s.clearFile); + useEffect(() => { + if (storeFile) { + selectFile(storeFile); + clearStoreFile(); + } + }, []); // eslint-disable-line react-hooks/exhaustive-deps + + const handleUpload = async () => { + if (!width && !height) return; + const id = await startUpload(); + if (id) setPhase('processing'); + }; + + const handleReset = () => { + reset(); + setPhase('upload'); + setWidth(''); + setHeight(''); + }; + + const dimensionValid = width || height; + + const schema = generateToolSchema({ + name: t('tools.imageResize.title'), + description: t('tools.imageResize.description'), + url: `${window.location.origin}/tools/image-resize`, + }); + + return ( + <> + + {t('tools.imageResize.title')} — {t('common.appName')} + + + + + +
+
+
+ +
+

{t('tools.imageResize.title')}

+

{t('tools.imageResize.description')}

+
+ + + + {phase === 'upload' && ( +
+ + + {file && !isUploading && ( + <> + {/* Dimensions */} +
+
+ + {t('tools.imageResize.dimensions')} + + +
+
+
+ + { + setWidth(e.target.value); + if (lockAspect) setHeight(''); + }} + className="w-full rounded-lg border border-slate-300 px-3 py-2 text-sm dark:border-slate-600 dark:bg-slate-700 dark:text-slate-200" + /> +
+
+ + { + setHeight(e.target.value); + if (lockAspect) setWidth(''); + }} + className="w-full rounded-lg border border-slate-300 px-3 py-2 text-sm dark:border-slate-600 dark:bg-slate-700 dark:text-slate-200" + /> +
+
+ {lockAspect && ( +

+ {t('tools.imageResize.aspectHint')} +

+ )} +
+ + {/* Quality Slider */} +
+ + setQuality(Number(e.target.value))} + className="w-full accent-primary-600" + /> +
+ + + + )} +
+ )} + + {phase === 'processing' && !result && ( + + )} + + {phase === 'done' && result && result.status === 'completed' && ( + + )} + + {phase === 'done' && taskError && ( +
+
+

{taskError}

+
+ +
+ )} + + +
+ + ); +} diff --git a/frontend/src/components/tools/PdfEditor.tsx b/frontend/src/components/tools/PdfEditor.tsx index 22f954c..73f9857 100644 --- a/frontend/src/components/tools/PdfEditor.tsx +++ b/frontend/src/components/tools/PdfEditor.tsx @@ -16,10 +16,11 @@ import { useFileUpload } from '@/hooks/useFileUpload'; import { useTaskPolling } from '@/hooks/useTaskPolling'; import { generateToolSchema } from '@/utils/seo'; import { useFileStore } from '@/stores/fileStore'; -import { TOOL_LIMITS_MB } from '@/config/toolLimits'; +import { useConfig } from '@/hooks/useConfig'; export default function PdfEditor() { const { t } = useTranslation(); + const { limits } = useConfig(); const [phase, setPhase] = useState<'upload' | 'processing' | 'done'>('upload'); const { @@ -33,7 +34,7 @@ export default function PdfEditor() { reset, } = useFileUpload({ endpoint: '/compress/pdf', - maxSizeMB: TOOL_LIMITS_MB.pdf, + maxSizeMB: limits.pdf, acceptedTypes: ['pdf'], extraData: { quality: 'high' }, }); @@ -100,7 +101,7 @@ export default function PdfEditor() { onFileSelect={selectFile} file={file} accept={{ 'application/pdf': ['.pdf'] }} - maxSizeMB={TOOL_LIMITS_MB.pdf} + maxSizeMB={limits.pdf} isUploading={isUploading} uploadProgress={uploadProgress} error={uploadError} diff --git a/frontend/src/hooks/useConfig.ts b/frontend/src/hooks/useConfig.ts new file mode 100644 index 0000000..5e64b29 --- /dev/null +++ b/frontend/src/hooks/useConfig.ts @@ -0,0 +1,45 @@ +import { useState, useEffect, useCallback } from 'react'; +import { TOOL_LIMITS_MB } from '@/config/toolLimits'; + +interface FileLimitsMb { + pdf: number; + word: number; + image: number; + video: number; + homepageSmartUpload: number; +} + +interface ConfigData { + file_limits_mb: FileLimitsMb; + max_upload_mb: number; +} + +const API_BASE = import.meta.env.VITE_API_URL || ''; + +/** + * Fetches dynamic upload limits from /api/config. + * Falls back to the hardcoded TOOL_LIMITS_MB on error. + */ +export function useConfig() { + const [limits, setLimits] = useState(TOOL_LIMITS_MB); + const [loading, setLoading] = useState(true); + + const fetchConfig = useCallback(async () => { + try { + const res = await fetch(`${API_BASE}/api/config`, { credentials: 'include' }); + if (!res.ok) throw new Error('config fetch failed'); + const data: ConfigData = await res.json(); + setLimits(data.file_limits_mb); + } catch { + // Keep hardcoded fallback + } finally { + setLoading(false); + } + }, []); + + useEffect(() => { + fetchConfig(); + }, [fetchConfig]); + + return { limits, loading, refetch: fetchConfig }; +} diff --git a/frontend/src/i18n/ar.json b/frontend/src/i18n/ar.json index 69ac814..1713604 100644 --- a/frontend/src/i18n/ar.json +++ b/frontend/src/i18n/ar.json @@ -25,6 +25,25 @@ "darkMode": "الوضع الداكن", "lightMode": "الوضع الفاتح" }, + "auth": { + "forgotPassword": { + "title": "نسيت كلمة المرور", + "subtitle": "أدخل بريدك الإلكتروني وسنرسل لك رابط إعادة التعيين.", + "submit": "إرسال رابط التعيين", + "sent": "إذا كان هذا البريد مسجلاً، فقد تم إرسال رابط إعادة التعيين. تحقق من بريدك.", + "error": "حدث خطأ. يرجى المحاولة مرة أخرى.", + "link": "نسيت كلمة المرور؟" + }, + "resetPassword": { + "title": "إعادة تعيين كلمة المرور", + "newPassword": "كلمة المرور الجديدة", + "submit": "إعادة التعيين", + "success": "تم تحديث كلمة المرور بنجاح! جارٍ التوجيه لتسجيل الدخول...", + "error": "فشل إعادة التعيين. قد يكون الرابط منتهي الصلاحية.", + "tooShort": "يجب أن تكون كلمة المرور 8 أحرف على الأقل.", + "noToken": "رابط غير صالح. يرجى طلب رابط جديد." + } + }, "home": { "hero": "كل ما تحتاجه للتعامل مع ملفات PDF — فوراً وبخطوات بسيطة", "heroSub": "ارفع ملفك أو اسحبه هنا، وسنكتشف نوعه تلقائيًا ونقترح الأدوات الملائمة — التحرير، التحويل، الضغط وغير ذلك. لا حاجة لتسجيل حساب لبدء الاستخدام.", @@ -80,6 +99,17 @@ "description": "حوّل الصور بين صيغ JPG و PNG و WebP فوراً.", "shortDesc": "تحويل الصور" }, + "imageResize": { + "title": "تغيير حجم الصورة", + "description": "غيّر أبعاد الصور بدقة مع الحفاظ على الجودة.", + "shortDesc": "تغيير الحجم", + "dimensions": "الأبعاد المطلوبة", + "width": "العرض (بكسل)", + "height": "الارتفاع (بكسل)", + "quality": "الجودة", + "lockAspect": "قفل نسبة العرض للارتفاع", + "aspectHint": "أدخل بُعداً واحداً — سيتم حساب الآخر تلقائياً للحفاظ على نسبة العرض للارتفاع." + }, "videoToGif": { "title": "فيديو إلى GIF", "description": "أنشئ صور GIF متحركة من مقاطع الفيديو. خصّص وقت البداية والمدة والجودة.", diff --git a/frontend/src/i18n/en.json b/frontend/src/i18n/en.json index c61cdf4..7f4cd91 100644 --- a/frontend/src/i18n/en.json +++ b/frontend/src/i18n/en.json @@ -25,6 +25,25 @@ "darkMode": "Dark Mode", "lightMode": "Light Mode" }, + "auth": { + "forgotPassword": { + "title": "Forgot Password", + "subtitle": "Enter your email and we'll send you a reset link.", + "submit": "Send Reset Link", + "sent": "If that email is registered, a reset link has been sent. Check your inbox.", + "error": "Something went wrong. Please try again.", + "link": "Forgot your password?" + }, + "resetPassword": { + "title": "Reset Password", + "newPassword": "New Password", + "submit": "Reset Password", + "success": "Password updated successfully! Redirecting to sign in...", + "error": "Failed to reset password. The link may have expired.", + "tooShort": "Password must be at least 8 characters.", + "noToken": "Invalid reset link. Please request a new one." + } + }, "home": { "hero": "Everything You Need to Work with PDF Files — Instantly", "heroSub": "Upload or drag & drop your file, and we'll auto-detect its type and suggest the right tools — edit, convert, compress, and more. No registration required.", @@ -80,6 +99,17 @@ "description": "Convert images between JPG, PNG, and WebP formats instantly.", "shortDesc": "Convert Images" }, + "imageResize": { + "title": "Image Resize", + "description": "Resize images to exact dimensions while maintaining quality.", + "shortDesc": "Resize Image", + "dimensions": "Target Dimensions", + "width": "Width (px)", + "height": "Height (px)", + "quality": "Quality", + "lockAspect": "Lock aspect ratio", + "aspectHint": "Enter one dimension — the other will auto-calculate to preserve aspect ratio." + }, "videoToGif": { "title": "Video to GIF", "description": "Create animated GIFs from video clips. Customize start time, duration, and quality.", diff --git a/frontend/src/i18n/fr.json b/frontend/src/i18n/fr.json index 834f58c..fc68e5d 100644 --- a/frontend/src/i18n/fr.json +++ b/frontend/src/i18n/fr.json @@ -25,6 +25,25 @@ "darkMode": "Mode sombre", "lightMode": "Mode clair" }, + "auth": { + "forgotPassword": { + "title": "Mot de passe oublié", + "subtitle": "Entrez votre email et nous vous enverrons un lien de réinitialisation.", + "submit": "Envoyer le lien", + "sent": "Si cet email est enregistré, un lien de réinitialisation a été envoyé. Vérifiez votre boîte de réception.", + "error": "Une erreur s'est produite. Veuillez réessayer.", + "link": "Mot de passe oublié ?" + }, + "resetPassword": { + "title": "Réinitialiser le mot de passe", + "newPassword": "Nouveau mot de passe", + "submit": "Réinitialiser", + "success": "Mot de passe mis à jour avec succès ! Redirection vers la connexion...", + "error": "Échec de la réinitialisation. Le lien a peut-être expiré.", + "tooShort": "Le mot de passe doit contenir au moins 8 caractères.", + "noToken": "Lien invalide. Veuillez en demander un nouveau." + } + }, "home": { "hero": "Tout ce dont vous avez besoin pour vos fichiers PDF — instantanément", "heroSub": "Déposez votre fichier ici, nous détecterons automatiquement son type et proposerons les outils adaptés — édition, conversion, compression et plus. Aucune inscription requise.", @@ -80,6 +99,17 @@ "description": "Convertissez instantanément des images entre les formats JPG, PNG et WebP.", "shortDesc": "Convertir des images" }, + "imageResize": { + "title": "Redimensionner l'image", + "description": "Redimensionnez vos images aux dimensions exactes tout en préservant la qualité.", + "shortDesc": "Redimensionner", + "dimensions": "Dimensions cibles", + "width": "Largeur (px)", + "height": "Hauteur (px)", + "quality": "Qualité", + "lockAspect": "Verrouiller le rapport d'aspect", + "aspectHint": "Entrez une dimension — l'autre sera calculée automatiquement pour préserver le rapport d'aspect." + }, "videoToGif": { "title": "Vidéo en GIF", "description": "Créez des GIFs animés à partir de clips vidéo. Personnalisez le temps de début, la durée et la qualité.", diff --git a/frontend/src/pages/AccountPage.tsx b/frontend/src/pages/AccountPage.tsx index 9cb16dd..844dac9 100644 --- a/frontend/src/pages/AccountPage.tsx +++ b/frontend/src/pages/AccountPage.tsx @@ -633,6 +633,14 @@ export default function AccountPage() { + + {mode === 'login' && ( +

+ + {t('auth.forgotPassword.link')} + +

+ )} diff --git a/frontend/src/pages/ForgotPasswordPage.tsx b/frontend/src/pages/ForgotPasswordPage.tsx new file mode 100644 index 0000000..79bc9b1 --- /dev/null +++ b/frontend/src/pages/ForgotPasswordPage.tsx @@ -0,0 +1,95 @@ +import { useState } from 'react'; +import { useTranslation } from 'react-i18next'; +import { Helmet } from 'react-helmet-async'; +import { Mail } from 'lucide-react'; + +const API_BASE = import.meta.env.VITE_API_URL || ''; + +export default function ForgotPasswordPage() { + const { t } = useTranslation(); + const [email, setEmail] = useState(''); + const [submitted, setSubmitted] = useState(false); + const [loading, setLoading] = useState(false); + const [error, setError] = useState(null); + + const handleSubmit = async (e: React.FormEvent) => { + e.preventDefault(); + setError(null); + setLoading(true); + + try { + const res = await fetch(`${API_BASE}/api/auth/forgot-password`, { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + credentials: 'include', + body: JSON.stringify({ email }), + }); + if (!res.ok) throw new Error('Request failed'); + setSubmitted(true); + } catch { + setError(t('auth.forgotPassword.error')); + } finally { + setLoading(false); + } + }; + + return ( + <> + + {t('auth.forgotPassword.title')} — {t('common.appName')} + + +
+
+
+ +
+

+ {t('auth.forgotPassword.title')} +

+

+ {t('auth.forgotPassword.subtitle')} +

+
+ + {submitted ? ( +
+

+ {t('auth.forgotPassword.sent')} +

+
+ ) : ( +
+
+ + setEmail(e.target.value)} + placeholder={t('account.emailPlaceholder')} + className="w-full rounded-lg border border-slate-300 px-3 py-2 text-sm dark:border-slate-600 dark:bg-slate-700 dark:text-slate-200" + /> +
+ + {error && ( +
+

{error}

+
+ )} + + +
+ )} +
+ + ); +} diff --git a/frontend/src/pages/HomePage.tsx b/frontend/src/pages/HomePage.tsx index 9d54473..d96222a 100644 --- a/frontend/src/pages/HomePage.tsx +++ b/frontend/src/pages/HomePage.tsx @@ -19,6 +19,7 @@ import { ListOrdered, PenLine, GitBranch, + Scaling, } from 'lucide-react'; import ToolCard from '@/components/shared/ToolCard'; import HeroUploadZone from '@/components/shared/HeroUploadZone'; @@ -50,6 +51,7 @@ const pdfTools: ToolInfo[] = [ const otherTools: ToolInfo[] = [ { key: 'imageConvert', path: '/tools/image-converter', icon: , bgColor: 'bg-purple-50' }, + { key: 'imageResize', path: '/tools/image-resize', icon: , bgColor: 'bg-teal-50' }, { key: 'videoToGif', path: '/tools/video-to-gif', icon: , bgColor: 'bg-emerald-50' }, { key: 'wordCounter', path: '/tools/word-counter', icon: , bgColor: 'bg-blue-50' }, { key: 'textCleaner', path: '/tools/text-cleaner', icon: , bgColor: 'bg-indigo-50' }, diff --git a/frontend/src/pages/ResetPasswordPage.tsx b/frontend/src/pages/ResetPasswordPage.tsx new file mode 100644 index 0000000..abb28f9 --- /dev/null +++ b/frontend/src/pages/ResetPasswordPage.tsx @@ -0,0 +1,130 @@ +import { useState } from 'react'; +import { useTranslation } from 'react-i18next'; +import { useSearchParams, useNavigate } from 'react-router-dom'; +import { Helmet } from 'react-helmet-async'; +import { KeyRound } from 'lucide-react'; + +const API_BASE = import.meta.env.VITE_API_URL || ''; + +export default function ResetPasswordPage() { + const { t } = useTranslation(); + const navigate = useNavigate(); + const [searchParams] = useSearchParams(); + const token = searchParams.get('token') || ''; + + const [password, setPassword] = useState(''); + const [confirm, setConfirm] = useState(''); + const [success, setSuccess] = useState(false); + const [loading, setLoading] = useState(false); + const [error, setError] = useState(null); + + const handleSubmit = async (e: React.FormEvent) => { + e.preventDefault(); + setError(null); + + if (password.length < 8) { + setError(t('auth.resetPassword.tooShort')); + return; + } + if (password !== confirm) { + setError(t('account.passwordMismatch')); + return; + } + + setLoading(true); + try { + const res = await fetch(`${API_BASE}/api/auth/reset-password`, { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + credentials: 'include', + body: JSON.stringify({ token, password }), + }); + const data = await res.json(); + if (!res.ok) throw new Error(data.error || 'Reset failed'); + setSuccess(true); + setTimeout(() => navigate('/account'), 3000); + } catch (err) { + setError(err instanceof Error ? err.message : t('auth.resetPassword.error')); + } finally { + setLoading(false); + } + }; + + if (!token) { + return ( +
+

{t('auth.resetPassword.noToken')}

+
+ ); + } + + return ( + <> + + {t('auth.resetPassword.title')} — {t('common.appName')} + + +
+
+
+ +
+

+ {t('auth.resetPassword.title')} +

+
+ + {success ? ( +
+

+ {t('auth.resetPassword.success')} +

+
+ ) : ( +
+
+ + setPassword(e.target.value)} + className="w-full rounded-lg border border-slate-300 px-3 py-2 text-sm dark:border-slate-600 dark:bg-slate-700 dark:text-slate-200" + /> +
+
+ + setConfirm(e.target.value)} + className="w-full rounded-lg border border-slate-300 px-3 py-2 text-sm dark:border-slate-600 dark:bg-slate-700 dark:text-slate-200" + /> +
+ + {error && ( +
+

{error}

+
+ )} + + +
+ )} +
+ + ); +} diff --git a/frontend/src/utils/fileRouting.ts b/frontend/src/utils/fileRouting.ts index 1129e8f..2064816 100644 --- a/frontend/src/utils/fileRouting.ts +++ b/frontend/src/utils/fileRouting.ts @@ -15,6 +15,7 @@ import { Film, PenLine, GitBranch, + Scaling, } from 'lucide-react'; import type { ComponentType, SVGProps } from 'react'; @@ -50,6 +51,7 @@ const pdfTools: ToolOption[] = [ /** Image tools available when an image is uploaded */ const imageTools: ToolOption[] = [ { key: 'imageConvert', path: '/tools/image-converter', icon: ImageIcon, bgColor: 'bg-purple-100 dark:bg-purple-900/30', iconColor: 'text-purple-600 dark:text-purple-400' }, + { key: 'imageResize', path: '/tools/image-resize', icon: Scaling, bgColor: 'bg-teal-100 dark:bg-teal-900/30', iconColor: 'text-teal-600 dark:text-teal-400' }, { key: 'imagesToPdf', path: '/tools/images-to-pdf', icon: FileImage, bgColor: 'bg-lime-100 dark:bg-lime-900/30', iconColor: 'text-lime-600 dark:text-lime-400' }, ]; From 6bb76e3f1b4f2f8e4a4f6a1f8c67a923ff19915d Mon Sep 17 00:00:00 2001 From: Your Name <119736744+aborayan2022@users.noreply.github.com> Date: Sat, 7 Mar 2026 21:29:08 +0200 Subject: [PATCH 2/4] Add OCR, Background Removal, and PDF Editor features with tests - Implemented OCR functionality using pytesseract for image and PDF text extraction. - Added Background Removal service using rembg for image processing. - Developed PDF Editor service for applying text annotations to PDF files. - Created corresponding API routes for OCR, Background Removal, and PDF Editor. - Added frontend components for OCR and Background Removal tools. - Integrated feature flagging for new tools, ensuring they are disabled by default. - Implemented comprehensive unit tests for OCR service, PDF editor, and background removal. - Updated documentation to reflect new features and usage instructions. - Added translations for new features in English, Arabic, and French. --- backend/Dockerfile | 4 + backend/app/__init__.py | 6 + backend/app/extensions.py | 3 + backend/app/routes/ocr.py | 134 ++++++++++ backend/app/routes/pdf_editor.py | 80 ++++++ backend/app/routes/removebg.py | 64 +++++ backend/app/services/ocr_service.py | 121 +++++++++ backend/app/services/pdf_editor_service.py | 120 +++++++++ backend/app/services/removebg_service.py | 60 +++++ backend/app/tasks/ocr_tasks.py | 159 ++++++++++++ backend/app/tasks/pdf_editor_tasks.py | 95 +++++++ backend/app/tasks/removebg_tasks.py | 95 +++++++ backend/celery_worker.py | 3 + backend/requirements.txt | 7 + backend/tests/test_ocr.py | 163 ++++++++++++ backend/tests/test_ocr_service.py | 66 +++++ backend/tests/test_pdf_editor.py | 144 ++++++++++ backend/tests/test_removebg.py | 73 ++++++ docs/feature-editor.md | 112 ++++++++ frontend/src/App.tsx | 4 + frontend/src/components/tools/OcrTool.tsx | 245 ++++++++++++++++++ .../src/components/tools/RemoveBackground.tsx | 150 +++++++++++ frontend/src/i18n/ar.json | 19 ++ frontend/src/i18n/en.json | 19 ++ frontend/src/i18n/fr.json | 19 ++ frontend/src/pages/HomePage.tsx | 3 + frontend/src/services/api.ts | 3 + frontend/src/utils/fileRouting.ts | 4 + 28 files changed, 1975 insertions(+) create mode 100644 backend/app/routes/ocr.py create mode 100644 backend/app/routes/pdf_editor.py create mode 100644 backend/app/routes/removebg.py create mode 100644 backend/app/services/ocr_service.py create mode 100644 backend/app/services/pdf_editor_service.py create mode 100644 backend/app/services/removebg_service.py create mode 100644 backend/app/tasks/ocr_tasks.py create mode 100644 backend/app/tasks/pdf_editor_tasks.py create mode 100644 backend/app/tasks/removebg_tasks.py create mode 100644 backend/tests/test_ocr.py create mode 100644 backend/tests/test_ocr_service.py create mode 100644 backend/tests/test_pdf_editor.py create mode 100644 backend/tests/test_removebg.py create mode 100644 frontend/src/components/tools/OcrTool.tsx create mode 100644 frontend/src/components/tools/RemoveBackground.tsx diff --git a/backend/Dockerfile b/backend/Dockerfile index 2d20964..6d2ae7d 100644 --- a/backend/Dockerfile +++ b/backend/Dockerfile @@ -13,6 +13,10 @@ RUN apt-get update && apt-get install -y --no-install-recommends \ ffmpeg \ libmagic1 \ imagemagick \ + tesseract-ocr \ + tesseract-ocr-eng \ + tesseract-ocr-ara \ + tesseract-ocr-fra \ curl \ && apt-get clean \ && rm -rf /var/lib/apt/lists/* diff --git a/backend/app/__init__.py b/backend/app/__init__.py index 17dd678..3be354f 100644 --- a/backend/app/__init__.py +++ b/backend/app/__init__.py @@ -90,6 +90,9 @@ def create_app(config_name=None): from app.routes.flowchart import flowchart_bp from app.routes.v1.tools import v1_bp from app.routes.config import config_bp + from app.routes.ocr import ocr_bp + from app.routes.removebg import removebg_bp + from app.routes.pdf_editor import pdf_editor_bp app.register_blueprint(health_bp, url_prefix="/api") app.register_blueprint(auth_bp, url_prefix="/api/auth") @@ -106,5 +109,8 @@ def create_app(config_name=None): app.register_blueprint(download_bp, url_prefix="/api/download") app.register_blueprint(v1_bp, url_prefix="/api/v1") app.register_blueprint(config_bp, url_prefix="/api/config") + app.register_blueprint(ocr_bp, url_prefix="/api/ocr") + app.register_blueprint(removebg_bp, url_prefix="/api/remove-bg") + app.register_blueprint(pdf_editor_bp, url_prefix="/api/pdf-editor") return app diff --git a/backend/app/extensions.py b/backend/app/extensions.py index bf82df2..accc0be 100644 --- a/backend/app/extensions.py +++ b/backend/app/extensions.py @@ -32,6 +32,9 @@ def init_celery(app): "app.tasks.video_tasks.*": {"queue": "video"}, "app.tasks.pdf_tools_tasks.*": {"queue": "pdf_tools"}, "app.tasks.flowchart_tasks.*": {"queue": "flowchart"}, + "app.tasks.ocr_tasks.*": {"queue": "image"}, + "app.tasks.removebg_tasks.*": {"queue": "image"}, + "app.tasks.pdf_editor_tasks.*": {"queue": "pdf_tools"}, } # Celery Beat — periodic tasks diff --git a/backend/app/routes/ocr.py b/backend/app/routes/ocr.py new file mode 100644 index 0000000..717dbac --- /dev/null +++ b/backend/app/routes/ocr.py @@ -0,0 +1,134 @@ +"""OCR routes — extract text from images and PDFs.""" +from flask import Blueprint, request, jsonify, current_app + +from app.extensions import limiter +from app.services.policy_service import ( + assert_quota_available, + build_task_tracking_kwargs, + PolicyError, + record_accepted_usage, + resolve_web_actor, + validate_actor_file, +) +from app.services.ocr_service import SUPPORTED_LANGUAGES +from app.utils.file_validator import FileValidationError +from app.utils.sanitizer import generate_safe_path +from app.tasks.ocr_tasks import ocr_image_task, ocr_pdf_task + +ocr_bp = Blueprint("ocr", __name__) + +ALLOWED_IMAGE_TYPES = ["png", "jpg", "jpeg", "webp", "tiff", "bmp"] +ALLOWED_OCR_TYPES = ALLOWED_IMAGE_TYPES + ["pdf"] + + +def _check_feature_flag(): + """Return an error response if FEATURE_EDITOR is disabled.""" + if not current_app.config.get("FEATURE_EDITOR", False): + return jsonify({"error": "This feature is not enabled."}), 403 + return None + + +@ocr_bp.route("/image", methods=["POST"]) +@limiter.limit("10/minute") +def ocr_image_route(): + """Extract text from an image using OCR. + + Accepts: multipart/form-data with: + - 'file': Image file + - 'lang' (optional): Language code — eng, ara, fra (default: eng) + Returns: JSON with task_id for polling + """ + flag_err = _check_feature_flag() + if flag_err: + return flag_err + + if "file" not in request.files: + return jsonify({"error": "No file provided."}), 400 + + file = request.files["file"] + lang = request.form.get("lang", "eng").lower() + if lang not in SUPPORTED_LANGUAGES: + lang = "eng" + + actor = resolve_web_actor() + try: + assert_quota_available(actor) + except PolicyError as e: + return jsonify({"error": e.message}), e.status_code + + try: + original_filename, ext = validate_actor_file( + file, allowed_types=ALLOWED_IMAGE_TYPES, actor=actor + ) + 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 = ocr_image_task.delay( + input_path, task_id, original_filename, lang, + **build_task_tracking_kwargs(actor), + ) + record_accepted_usage(actor, "ocr-image", task.id) + + return jsonify({ + "task_id": task.id, + "message": "OCR started. Poll /api/tasks/{task_id}/status for progress.", + }), 202 + + +@ocr_bp.route("/pdf", methods=["POST"]) +@limiter.limit("5/minute") +def ocr_pdf_route(): + """Extract text from a scanned PDF using OCR. + + Accepts: multipart/form-data with: + - 'file': PDF file + - 'lang' (optional): Language code — eng, ara, fra (default: eng) + Returns: JSON with task_id for polling + """ + flag_err = _check_feature_flag() + if flag_err: + return flag_err + + if "file" not in request.files: + return jsonify({"error": "No file provided."}), 400 + + file = request.files["file"] + lang = request.form.get("lang", "eng").lower() + if lang not in SUPPORTED_LANGUAGES: + lang = "eng" + + actor = resolve_web_actor() + try: + assert_quota_available(actor) + except PolicyError as e: + return jsonify({"error": e.message}), e.status_code + + try: + original_filename, ext = validate_actor_file( + file, allowed_types=["pdf"], actor=actor + ) + 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 = ocr_pdf_task.delay( + input_path, task_id, original_filename, lang, + **build_task_tracking_kwargs(actor), + ) + record_accepted_usage(actor, "ocr-pdf", task.id) + + return jsonify({ + "task_id": task.id, + "message": "OCR started. Poll /api/tasks/{task_id}/status for progress.", + }), 202 + + +@ocr_bp.route("/languages", methods=["GET"]) +def ocr_languages_route(): + """Return the list of supported OCR languages.""" + return jsonify({"languages": SUPPORTED_LANGUAGES}), 200 diff --git a/backend/app/routes/pdf_editor.py b/backend/app/routes/pdf_editor.py new file mode 100644 index 0000000..4976fa0 --- /dev/null +++ b/backend/app/routes/pdf_editor.py @@ -0,0 +1,80 @@ +"""PDF Editor route — apply text annotations to PDFs.""" +import json + +from flask import Blueprint, request, jsonify, current_app + +from app.extensions import limiter +from app.services.policy_service import ( + assert_quota_available, + build_task_tracking_kwargs, + PolicyError, + record_accepted_usage, + resolve_web_actor, + validate_actor_file, +) +from app.utils.file_validator import FileValidationError +from app.utils.sanitizer import generate_safe_path +from app.tasks.pdf_editor_tasks import edit_pdf_task + +pdf_editor_bp = Blueprint("pdf_editor", __name__) + + +@pdf_editor_bp.route("/edit", methods=["POST"]) +@limiter.limit("10/minute") +def edit_pdf_route(): + """Apply text annotations to a PDF. + + Accepts: multipart/form-data with: + - 'file': PDF file + - 'edits': JSON string — array of edit objects + Each edit: { type: "text", page: 1, x: 100, y: 200, content: "Hello", fontSize: 14, color: "#000000" } + Returns: JSON with task_id for polling + """ + if not current_app.config.get("FEATURE_EDITOR", False): + return jsonify({"error": "This feature is not enabled."}), 403 + + if "file" not in request.files: + return jsonify({"error": "No file provided."}), 400 + + file = request.files["file"] + edits_raw = request.form.get("edits", "[]") + + try: + edits = json.loads(edits_raw) + if not isinstance(edits, list): + return jsonify({"error": "Edits must be a JSON array."}), 400 + except (json.JSONDecodeError, TypeError): + return jsonify({"error": "Invalid JSON in 'edits' field."}), 400 + + if not edits: + return jsonify({"error": "At least one edit is required."}), 400 + + if len(edits) > 500: + return jsonify({"error": "Maximum 500 edits allowed."}), 400 + + actor = resolve_web_actor() + try: + assert_quota_available(actor) + except PolicyError as e: + return jsonify({"error": e.message}), e.status_code + + try: + original_filename, ext = validate_actor_file( + file, allowed_types=["pdf"], actor=actor + ) + 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 = edit_pdf_task.delay( + input_path, task_id, original_filename, edits, + **build_task_tracking_kwargs(actor), + ) + record_accepted_usage(actor, "pdf-edit", task.id) + + return jsonify({ + "task_id": task.id, + "message": "PDF editing started. Poll /api/tasks/{task_id}/status for progress.", + }), 202 diff --git a/backend/app/routes/removebg.py b/backend/app/routes/removebg.py new file mode 100644 index 0000000..48d181b --- /dev/null +++ b/backend/app/routes/removebg.py @@ -0,0 +1,64 @@ +"""Background removal route.""" +from flask import Blueprint, request, jsonify, current_app + +from app.extensions import limiter +from app.services.policy_service import ( + assert_quota_available, + build_task_tracking_kwargs, + PolicyError, + record_accepted_usage, + resolve_web_actor, + validate_actor_file, +) +from app.utils.file_validator import FileValidationError +from app.utils.sanitizer import generate_safe_path +from app.tasks.removebg_tasks import remove_bg_task + +removebg_bp = Blueprint("removebg", __name__) + +ALLOWED_IMAGE_TYPES = ["png", "jpg", "jpeg", "webp"] + + +@removebg_bp.route("", methods=["POST"]) +@limiter.limit("5/minute") +def remove_bg_route(): + """Remove the background from an image. + + Accepts: multipart/form-data with: + - 'file': Image file (PNG, JPG, JPEG, WebP) + Returns: JSON with task_id for polling + """ + if not current_app.config.get("FEATURE_EDITOR", False): + return jsonify({"error": "This feature is not enabled."}), 403 + + if "file" not in request.files: + return jsonify({"error": "No file provided."}), 400 + + file = request.files["file"] + + actor = resolve_web_actor() + try: + assert_quota_available(actor) + except PolicyError as e: + return jsonify({"error": e.message}), e.status_code + + try: + original_filename, ext = validate_actor_file( + file, allowed_types=ALLOWED_IMAGE_TYPES, actor=actor + ) + 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 = remove_bg_task.delay( + input_path, task_id, original_filename, + **build_task_tracking_kwargs(actor), + ) + record_accepted_usage(actor, "remove-bg", task.id) + + return jsonify({ + "task_id": task.id, + "message": "Background removal started. Poll /api/tasks/{task_id}/status for progress.", + }), 202 diff --git a/backend/app/services/ocr_service.py b/backend/app/services/ocr_service.py new file mode 100644 index 0000000..f0233c3 --- /dev/null +++ b/backend/app/services/ocr_service.py @@ -0,0 +1,121 @@ +"""OCR service — extract text from images and PDFs using Tesseract.""" +import logging +import os +import subprocess +import tempfile + +from PIL import Image + +logger = logging.getLogger(__name__) + + +class OCRError(Exception): + """Custom exception for OCR failures.""" + pass + + +# Tesseract language codes +SUPPORTED_LANGUAGES = { + "eng": "English", + "ara": "Arabic", + "fra": "French", +} + +DEFAULT_LANG = "eng" + + +def _get_tesseract_cmd() -> str: + """Return the tesseract binary path.""" + return os.getenv("TESSERACT_CMD", "tesseract") + + +def ocr_image(input_path: str, lang: str = DEFAULT_LANG) -> dict: + """Extract text from an image file using Tesseract. + + Args: + input_path: Path to the input image. + lang: Tesseract language code (e.g. "eng", "ara", "fra"). + + Returns: + dict with ``text``, ``lang``, ``char_count``. + + Raises: + OCRError: If the OCR operation fails. + """ + if lang not in SUPPORTED_LANGUAGES: + lang = DEFAULT_LANG + + try: + import pytesseract + + pytesseract.pytesseract.tesseract_cmd = _get_tesseract_cmd() + + with Image.open(input_path) as img: + # Convert to RGB if needed (tesseract works best with RGB) + if img.mode not in ("RGB", "L"): + img = img.convert("RGB") + text = pytesseract.image_to_string(img, lang=lang) + + text = text.strip() + return { + "text": text, + "lang": lang, + "char_count": len(text), + } + except ImportError: + raise OCRError("pytesseract is not installed.") + except Exception as e: + raise OCRError(f"OCR failed: {str(e)}") + + +def ocr_pdf(input_path: str, output_path: str, lang: str = DEFAULT_LANG) -> dict: + """Extract text from a scanned PDF by converting pages to images first. + + Args: + input_path: Path to the input PDF. + output_path: Path for the output text file. + lang: Tesseract language code. + + Returns: + dict with ``text``, ``page_count``, ``char_count``. + + Raises: + OCRError: If the OCR operation fails. + """ + if lang not in SUPPORTED_LANGUAGES: + lang = DEFAULT_LANG + + try: + from pdf2image import convert_from_path + import pytesseract + + pytesseract.pytesseract.tesseract_cmd = _get_tesseract_cmd() + + images = convert_from_path(input_path, dpi=300) + if not images: + raise OCRError("Could not convert PDF to images — file may be empty.") + + all_text = [] + for i, img in enumerate(images, 1): + if img.mode not in ("RGB", "L"): + img = img.convert("RGB") + page_text = pytesseract.image_to_string(img, lang=lang) + all_text.append(f"--- Page {i} ---\n{page_text.strip()}") + + full_text = "\n\n".join(all_text) + + os.makedirs(os.path.dirname(output_path), exist_ok=True) + with open(output_path, "w", encoding="utf-8") as f: + f.write(full_text) + + return { + "text": full_text, + "page_count": len(images), + "char_count": len(full_text), + } + except ImportError as e: + raise OCRError(f"Missing dependency: {e}") + except OCRError: + raise + except Exception as e: + raise OCRError(f"PDF OCR failed: {str(e)}") diff --git a/backend/app/services/pdf_editor_service.py b/backend/app/services/pdf_editor_service.py new file mode 100644 index 0000000..6c2f609 --- /dev/null +++ b/backend/app/services/pdf_editor_service.py @@ -0,0 +1,120 @@ +"""PDF Editor service — add text annotations and simple edits to PDFs.""" +import io +import logging +import os + +logger = logging.getLogger(__name__) + + +class PDFEditorError(Exception): + """Custom exception for PDF editor failures.""" + pass + + +def apply_pdf_edits(input_path: str, output_path: str, edits: list[dict]) -> dict: + """Apply a list of edits (text annotations) to an existing PDF. + + Each edit dict can contain: + - type: "text" + - page: 1-based page number + - x, y: position in points from bottom-left + - content: text string to place + - fontSize: optional, default 12 + - color: optional hex e.g. "#000000" + + Args: + input_path: Path to the source PDF. + output_path: Path for the edited PDF. + edits: List of edit operation dicts. + + Returns: + dict with ``page_count``, ``edits_applied``, ``output_size``. + + Raises: + PDFEditorError: If the edit fails. + """ + if not edits: + raise PDFEditorError("No edits provided.") + + os.makedirs(os.path.dirname(output_path), exist_ok=True) + + try: + from PyPDF2 import PdfReader, PdfWriter + from reportlab.pdfgen import canvas + from reportlab.lib.pagesizes import letter + from reportlab.lib.colors import HexColor + + reader = PdfReader(input_path) + writer = PdfWriter() + page_count = len(reader.pages) + + if page_count == 0: + raise PDFEditorError("PDF has no pages.") + + # Group edits by page + edits_by_page: dict[int, list[dict]] = {} + for edit in edits: + page_num = int(edit.get("page", 1)) + if page_num < 1 or page_num > page_count: + continue + edits_by_page.setdefault(page_num, []).append(edit) + + edits_applied = 0 + + for page_idx in range(page_count): + page = reader.pages[page_idx] + page_num = page_idx + 1 + page_edits = edits_by_page.get(page_num, []) + + if page_edits: + # Get page dimensions + media_box = page.mediabox + page_width = float(media_box.width) + page_height = float(media_box.height) + + # Create overlay with annotations + packet = io.BytesIO() + c = canvas.Canvas(packet, pagesize=(page_width, page_height)) + + for edit in page_edits: + edit_type = edit.get("type", "text") + if edit_type == "text": + x = float(edit.get("x", 72)) + y = float(edit.get("y", 72)) + content = str(edit.get("content", "")) + font_size = int(edit.get("fontSize", 12)) + color = str(edit.get("color", "#000000")) + + try: + c.setFillColor(HexColor(color)) + except Exception: + c.setFillColor(HexColor("#000000")) + + c.setFont("Helvetica", font_size) + c.drawString(x, y, content) + edits_applied += 1 + + c.save() + packet.seek(0) + + overlay_reader = PdfReader(packet) + if len(overlay_reader.pages) > 0: + page.merge_page(overlay_reader.pages[0]) + + writer.add_page(page) + + with open(output_path, "wb") as f: + writer.write(f) + + output_size = os.path.getsize(output_path) + + return { + "page_count": page_count, + "edits_applied": edits_applied, + "output_size": output_size, + } + + except PDFEditorError: + raise + except Exception as e: + raise PDFEditorError(f"PDF editing failed: {str(e)}") diff --git a/backend/app/services/removebg_service.py b/backend/app/services/removebg_service.py new file mode 100644 index 0000000..9a931b7 --- /dev/null +++ b/backend/app/services/removebg_service.py @@ -0,0 +1,60 @@ +"""Background removal service using rembg.""" +import logging +import os + +from PIL import Image + +logger = logging.getLogger(__name__) + + +class RemoveBGError(Exception): + """Custom exception for background removal failures.""" + pass + + +def remove_background(input_path: str, output_path: str) -> dict: + """Remove the background from an image. + + Args: + input_path: Path to the input image. + output_path: Path for the output PNG (always PNG — transparency). + + Returns: + dict with ``original_size``, ``output_size``, ``width``, ``height``. + + Raises: + RemoveBGError: If the operation fails. + """ + os.makedirs(os.path.dirname(output_path), exist_ok=True) + + try: + from rembg import remove as rembg_remove + + with Image.open(input_path) as img: + if img.mode != "RGBA": + img = img.convert("RGBA") + width, height = img.size + original_size = os.path.getsize(input_path) + + result = rembg_remove(img) + result.save(output_path, format="PNG", optimize=True) + + output_size = os.path.getsize(output_path) + + logger.info( + "Background removed: %s → %s (%d → %d bytes)", + input_path, output_path, original_size, output_size, + ) + + return { + "original_size": original_size, + "output_size": output_size, + "width": width, + "height": height, + } + except ImportError: + raise RemoveBGError("rembg is not installed.") + except (IOError, OSError) as e: + raise RemoveBGError(f"Background removal failed: {str(e)}") + except Exception as e: + raise RemoveBGError(f"Background removal failed: {str(e)}") diff --git a/backend/app/tasks/ocr_tasks.py b/backend/app/tasks/ocr_tasks.py new file mode 100644 index 0000000..c8883ee --- /dev/null +++ b/backend/app/tasks/ocr_tasks.py @@ -0,0 +1,159 @@ +"""Celery tasks for OCR processing.""" +import os +import logging + +from flask import current_app + +from app.extensions import celery +from app.services.ocr_service import ocr_image, ocr_pdf, OCRError +from app.services.storage_service import storage +from app.services.task_tracking_service import finalize_task_tracking +from app.utils.sanitizer import cleanup_task_files + +logger = logging.getLogger(__name__) + + +def _cleanup(task_id: str): + cleanup_task_files(task_id, keep_outputs=not storage.use_s3) + + +def _get_output_dir(task_id: str) -> str: + output_dir = os.path.join(current_app.config["OUTPUT_FOLDER"], task_id) + os.makedirs(output_dir, exist_ok=True) + return output_dir + + +def _finalize_task( + task_id, user_id, tool, original_filename, result, + usage_source, api_key_id, celery_task_id, +): + finalize_task_tracking( + user_id=user_id, tool=tool, original_filename=original_filename, + result=result, usage_source=usage_source, + api_key_id=api_key_id, celery_task_id=celery_task_id, + ) + _cleanup(task_id) + return result + + +@celery.task(bind=True, name="app.tasks.ocr_tasks.ocr_image_task") +def ocr_image_task( + self, + input_path: str, + task_id: str, + original_filename: str, + lang: str = "eng", + user_id: int | None = None, + usage_source: str = "web", + api_key_id: int | None = None, +): + """Async task: Extract text from an image via OCR.""" + output_dir = _get_output_dir(task_id) + output_path = os.path.join(output_dir, f"{task_id}.txt") + + try: + self.update_state(state="PROCESSING", meta={"step": "Running OCR on image..."}) + + stats = ocr_image(input_path, lang=lang) + + # Write text to file for download + with open(output_path, "w", encoding="utf-8") as f: + f.write(stats["text"]) + + self.update_state(state="PROCESSING", meta={"step": "Uploading result..."}) + s3_key = storage.upload_file(output_path, task_id, folder="outputs") + + name_without_ext = os.path.splitext(original_filename)[0] + download_name = f"{name_without_ext}_ocr.txt" + + download_url = storage.generate_presigned_url(s3_key, original_filename=download_name) + + result = { + "status": "completed", + "download_url": download_url, + "filename": download_name, + "text": stats["text"][:5000], # preview (first 5k chars) + "char_count": stats["char_count"], + "lang": stats["lang"], + } + + logger.info("Task %s: OCR image completed (%d chars)", task_id, stats["char_count"]) + return _finalize_task( + task_id, user_id, "ocr-image", original_filename, + result, usage_source, api_key_id, self.request.id, + ) + + except OCRError as e: + logger.error("Task %s: OCR error — %s", task_id, e) + return _finalize_task( + task_id, user_id, "ocr-image", original_filename, + {"status": "failed", "error": str(e)}, + usage_source, api_key_id, self.request.id, + ) + except Exception as e: + logger.error("Task %s: Unexpected error — %s", task_id, e) + return _finalize_task( + task_id, user_id, "ocr-image", original_filename, + {"status": "failed", "error": "An unexpected error occurred."}, + usage_source, api_key_id, self.request.id, + ) + + +@celery.task(bind=True, name="app.tasks.ocr_tasks.ocr_pdf_task") +def ocr_pdf_task( + self, + input_path: str, + task_id: str, + original_filename: str, + lang: str = "eng", + user_id: int | None = None, + usage_source: str = "web", + api_key_id: int | None = None, +): + """Async task: Extract text from a scanned PDF via OCR.""" + output_dir = _get_output_dir(task_id) + output_path = os.path.join(output_dir, f"{task_id}.txt") + + try: + self.update_state(state="PROCESSING", meta={"step": "Converting PDF pages & running OCR..."}) + + stats = ocr_pdf(input_path, output_path, lang=lang) + + self.update_state(state="PROCESSING", meta={"step": "Uploading result..."}) + s3_key = storage.upload_file(output_path, task_id, folder="outputs") + + name_without_ext = os.path.splitext(original_filename)[0] + download_name = f"{name_without_ext}_ocr.txt" + + download_url = storage.generate_presigned_url(s3_key, original_filename=download_name) + + result = { + "status": "completed", + "download_url": download_url, + "filename": download_name, + "text": stats["text"][:5000], + "page_count": stats["page_count"], + "char_count": stats["char_count"], + "lang": lang, + } + + logger.info("Task %s: OCR PDF completed (%d pages, %d chars)", task_id, stats["page_count"], stats["char_count"]) + return _finalize_task( + task_id, user_id, "ocr-pdf", original_filename, + result, usage_source, api_key_id, self.request.id, + ) + + except OCRError as e: + logger.error("Task %s: OCR error — %s", task_id, e) + return _finalize_task( + task_id, user_id, "ocr-pdf", original_filename, + {"status": "failed", "error": str(e)}, + usage_source, api_key_id, self.request.id, + ) + except Exception as e: + logger.error("Task %s: Unexpected error — %s", task_id, e) + return _finalize_task( + task_id, user_id, "ocr-pdf", original_filename, + {"status": "failed", "error": "An unexpected error occurred."}, + usage_source, api_key_id, self.request.id, + ) diff --git a/backend/app/tasks/pdf_editor_tasks.py b/backend/app/tasks/pdf_editor_tasks.py new file mode 100644 index 0000000..d2dc722 --- /dev/null +++ b/backend/app/tasks/pdf_editor_tasks.py @@ -0,0 +1,95 @@ +"""Celery tasks for PDF editing.""" +import os +import logging + +from flask import current_app + +from app.extensions import celery +from app.services.pdf_editor_service import apply_pdf_edits, PDFEditorError +from app.services.storage_service import storage +from app.services.task_tracking_service import finalize_task_tracking +from app.utils.sanitizer import cleanup_task_files + +logger = logging.getLogger(__name__) + + +def _cleanup(task_id: str): + cleanup_task_files(task_id, keep_outputs=not storage.use_s3) + + +def _get_output_dir(task_id: str) -> str: + output_dir = os.path.join(current_app.config["OUTPUT_FOLDER"], task_id) + os.makedirs(output_dir, exist_ok=True) + return output_dir + + +def _finalize_task( + task_id, user_id, tool, original_filename, result, + usage_source, api_key_id, celery_task_id, +): + finalize_task_tracking( + user_id=user_id, tool=tool, original_filename=original_filename, + result=result, usage_source=usage_source, + api_key_id=api_key_id, celery_task_id=celery_task_id, + ) + _cleanup(task_id) + return result + + +@celery.task(bind=True, name="app.tasks.pdf_editor_tasks.edit_pdf_task") +def edit_pdf_task( + self, + input_path: str, + task_id: str, + original_filename: str, + edits: list[dict], + user_id: int | None = None, + usage_source: str = "web", + api_key_id: int | None = None, +): + """Async task: Apply text annotations to a PDF.""" + output_dir = _get_output_dir(task_id) + output_path = os.path.join(output_dir, f"{task_id}.pdf") + + try: + self.update_state(state="PROCESSING", meta={"step": "Applying edits to PDF..."}) + + stats = apply_pdf_edits(input_path, output_path, edits) + + self.update_state(state="PROCESSING", meta={"step": "Uploading result..."}) + s3_key = storage.upload_file(output_path, task_id, folder="outputs") + + name_without_ext = os.path.splitext(original_filename)[0] + download_name = f"{name_without_ext}_edited.pdf" + + download_url = storage.generate_presigned_url(s3_key, original_filename=download_name) + + result = { + "status": "completed", + "download_url": download_url, + "filename": download_name, + "page_count": stats["page_count"], + "edits_applied": stats["edits_applied"], + "output_size": stats["output_size"], + } + + logger.info("Task %s: PDF edit completed (%d edits)", task_id, stats["edits_applied"]) + return _finalize_task( + task_id, user_id, "pdf-edit", original_filename, + result, usage_source, api_key_id, self.request.id, + ) + + except PDFEditorError as e: + logger.error("Task %s: PDF edit error — %s", task_id, e) + return _finalize_task( + task_id, user_id, "pdf-edit", original_filename, + {"status": "failed", "error": str(e)}, + usage_source, api_key_id, self.request.id, + ) + except Exception as e: + logger.error("Task %s: Unexpected error — %s", task_id, e) + return _finalize_task( + task_id, user_id, "pdf-edit", original_filename, + {"status": "failed", "error": "An unexpected error occurred."}, + usage_source, api_key_id, self.request.id, + ) diff --git a/backend/app/tasks/removebg_tasks.py b/backend/app/tasks/removebg_tasks.py new file mode 100644 index 0000000..eb5ad72 --- /dev/null +++ b/backend/app/tasks/removebg_tasks.py @@ -0,0 +1,95 @@ +"""Celery tasks for background removal.""" +import os +import logging + +from flask import current_app + +from app.extensions import celery +from app.services.removebg_service import remove_background, RemoveBGError +from app.services.storage_service import storage +from app.services.task_tracking_service import finalize_task_tracking +from app.utils.sanitizer import cleanup_task_files + +logger = logging.getLogger(__name__) + + +def _cleanup(task_id: str): + cleanup_task_files(task_id, keep_outputs=not storage.use_s3) + + +def _get_output_dir(task_id: str) -> str: + output_dir = os.path.join(current_app.config["OUTPUT_FOLDER"], task_id) + os.makedirs(output_dir, exist_ok=True) + return output_dir + + +def _finalize_task( + task_id, user_id, tool, original_filename, result, + usage_source, api_key_id, celery_task_id, +): + finalize_task_tracking( + user_id=user_id, tool=tool, original_filename=original_filename, + result=result, usage_source=usage_source, + api_key_id=api_key_id, celery_task_id=celery_task_id, + ) + _cleanup(task_id) + return result + + +@celery.task(bind=True, name="app.tasks.removebg_tasks.remove_bg_task") +def remove_bg_task( + self, + input_path: str, + task_id: str, + original_filename: str, + user_id: int | None = None, + usage_source: str = "web", + api_key_id: int | None = None, +): + """Async task: Remove background from an image.""" + output_dir = _get_output_dir(task_id) + output_path = os.path.join(output_dir, f"{task_id}.png") + + try: + self.update_state(state="PROCESSING", meta={"step": "Removing background..."}) + + stats = remove_background(input_path, output_path) + + self.update_state(state="PROCESSING", meta={"step": "Uploading result..."}) + s3_key = storage.upload_file(output_path, task_id, folder="outputs") + + name_without_ext = os.path.splitext(original_filename)[0] + download_name = f"{name_without_ext}_nobg.png" + + download_url = storage.generate_presigned_url(s3_key, original_filename=download_name) + + result = { + "status": "completed", + "download_url": download_url, + "filename": download_name, + "original_size": stats["original_size"], + "output_size": stats["output_size"], + "width": stats["width"], + "height": stats["height"], + } + + logger.info("Task %s: Background removal completed", task_id) + return _finalize_task( + task_id, user_id, "remove-bg", original_filename, + result, usage_source, api_key_id, self.request.id, + ) + + except RemoveBGError as e: + logger.error("Task %s: RemoveBG error — %s", task_id, e) + return _finalize_task( + task_id, user_id, "remove-bg", original_filename, + {"status": "failed", "error": str(e)}, + usage_source, api_key_id, self.request.id, + ) + except Exception as e: + logger.error("Task %s: Unexpected error — %s", task_id, e) + return _finalize_task( + task_id, user_id, "remove-bg", original_filename, + {"status": "failed", "error": "An unexpected error occurred."}, + usage_source, api_key_id, self.request.id, + ) diff --git a/backend/celery_worker.py b/backend/celery_worker.py index b92ff08..1ae5b5d 100644 --- a/backend/celery_worker.py +++ b/backend/celery_worker.py @@ -12,3 +12,6 @@ import app.tasks.video_tasks # noqa: F401 import app.tasks.pdf_tools_tasks # noqa: F401 import app.tasks.flowchart_tasks # noqa: F401 import app.tasks.maintenance_tasks # noqa: F401 +import app.tasks.ocr_tasks # noqa: F401 +import app.tasks.removebg_tasks # noqa: F401 +import app.tasks.pdf_editor_tasks # noqa: F401 diff --git a/backend/requirements.txt b/backend/requirements.txt index fec0f0e..fa07ab1 100644 --- a/backend/requirements.txt +++ b/backend/requirements.txt @@ -21,6 +21,13 @@ PyPDF2>=3.0,<4.0 reportlab>=4.0,<5.0 pdf2image>=1.16,<2.0 +# OCR +pytesseract>=0.3.10,<1.0 + +# Background Removal +rembg>=2.0,<3.0 +onnxruntime>=1.16,<2.0 + # AWS boto3>=1.34,<2.0 diff --git a/backend/tests/test_ocr.py b/backend/tests/test_ocr.py new file mode 100644 index 0000000..4069bde --- /dev/null +++ b/backend/tests/test_ocr.py @@ -0,0 +1,163 @@ +"""Tests for OCR routes — /api/ocr/image, /api/ocr/pdf, /api/ocr/languages.""" +import io +import json +import os +import tempfile +from unittest.mock import MagicMock + +from tests.conftest import make_png_bytes, make_pdf_bytes + + +# ========================================================================= +# Feature flag enforcement +# ========================================================================= +class TestOcrFeatureFlag: + def test_ocr_image_disabled_by_default(self, client): + """OCR image should return 403 when FEATURE_EDITOR is off.""" + data = {"file": (io.BytesIO(make_png_bytes()), "test.png")} + response = client.post( + "/api/ocr/image", + data=data, + content_type="multipart/form-data", + ) + assert response.status_code == 403 + assert "not enabled" in response.get_json()["error"] + + def test_ocr_pdf_disabled_by_default(self, client): + """OCR PDF should return 403 when FEATURE_EDITOR is off.""" + data = {"file": (io.BytesIO(make_pdf_bytes()), "scan.pdf")} + response = client.post( + "/api/ocr/pdf", + data=data, + content_type="multipart/form-data", + ) + assert response.status_code == 403 + + def test_languages_always_available(self, client): + """GET /api/ocr/languages should work even when feature is disabled.""" + response = client.get("/api/ocr/languages") + assert response.status_code == 200 + data = response.get_json() + langs = data["languages"] + assert "eng" in langs + assert "ara" in langs + assert "fra" in langs + + +# ========================================================================= +# Validation +# ========================================================================= +class TestOcrValidation: + def test_ocr_image_no_file(self, client, app): + """Should return 400 when no file provided.""" + app.config["FEATURE_EDITOR"] = True + response = client.post("/api/ocr/image") + assert response.status_code == 400 + assert "No file" in response.get_json()["error"] + + def test_ocr_pdf_no_file(self, client, app): + """Should return 400 when no file provided.""" + app.config["FEATURE_EDITOR"] = True + response = client.post("/api/ocr/pdf") + assert response.status_code == 400 + assert "No file" in response.get_json()["error"] + + +# ========================================================================= +# Success paths +# ========================================================================= +class TestOcrSuccess: + def test_ocr_image_success(self, client, app, monkeypatch): + """Should return 202 with task_id when valid image provided.""" + app.config["FEATURE_EDITOR"] = True + mock_task = MagicMock() + mock_task.id = "ocr-img-task-1" + + tmp_dir = tempfile.mkdtemp() + save_path = os.path.join(tmp_dir, "mock.png") + + monkeypatch.setattr( + "app.routes.ocr.validate_actor_file", + lambda f, allowed_types, actor: ("test.png", "png"), + ) + monkeypatch.setattr( + "app.routes.ocr.generate_safe_path", + lambda ext, folder_type: ("mock-id", save_path), + ) + monkeypatch.setattr( + "app.routes.ocr.ocr_image_task.delay", + MagicMock(return_value=mock_task), + ) + + data = {"file": (io.BytesIO(make_png_bytes()), "test.png"), "lang": "eng"} + response = client.post( + "/api/ocr/image", + data=data, + content_type="multipart/form-data", + ) + assert response.status_code == 202 + body = response.get_json() + assert body["task_id"] == "ocr-img-task-1" + + def test_ocr_pdf_success(self, client, app, monkeypatch): + """Should return 202 with task_id when valid PDF provided.""" + app.config["FEATURE_EDITOR"] = True + mock_task = MagicMock() + mock_task.id = "ocr-pdf-task-1" + + tmp_dir = tempfile.mkdtemp() + save_path = os.path.join(tmp_dir, "mock.pdf") + + monkeypatch.setattr( + "app.routes.ocr.validate_actor_file", + lambda f, allowed_types, actor: ("scan.pdf", "pdf"), + ) + monkeypatch.setattr( + "app.routes.ocr.generate_safe_path", + lambda ext, folder_type: ("mock-id", save_path), + ) + monkeypatch.setattr( + "app.routes.ocr.ocr_pdf_task.delay", + MagicMock(return_value=mock_task), + ) + + data = {"file": (io.BytesIO(make_pdf_bytes()), "scan.pdf"), "lang": "ara"} + response = client.post( + "/api/ocr/pdf", + data=data, + content_type="multipart/form-data", + ) + assert response.status_code == 202 + body = response.get_json() + assert body["task_id"] == "ocr-pdf-task-1" + + def test_ocr_image_invalid_lang_falls_back(self, client, app, monkeypatch): + """Invalid lang should fall back to 'eng' without error.""" + app.config["FEATURE_EDITOR"] = True + mock_task = MagicMock() + mock_task.id = "ocr-lang-task" + + tmp_dir = tempfile.mkdtemp() + save_path = os.path.join(tmp_dir, "mock.png") + + monkeypatch.setattr( + "app.routes.ocr.validate_actor_file", + lambda f, allowed_types, actor: ("test.png", "png"), + ) + monkeypatch.setattr( + "app.routes.ocr.generate_safe_path", + lambda ext, folder_type: ("mock-id", save_path), + ) + mock_delay = MagicMock(return_value=mock_task) + monkeypatch.setattr("app.routes.ocr.ocr_image_task.delay", mock_delay) + + data = {"file": (io.BytesIO(make_png_bytes()), "test.png"), "lang": "invalid"} + response = client.post( + "/api/ocr/image", + data=data, + content_type="multipart/form-data", + ) + assert response.status_code == 202 + # Verify 'eng' was passed to the task + call_args = mock_delay.call_args + assert call_args[0][3] == "eng" # 4th positional arg is lang diff --git a/backend/tests/test_ocr_service.py b/backend/tests/test_ocr_service.py new file mode 100644 index 0000000..7b26afd --- /dev/null +++ b/backend/tests/test_ocr_service.py @@ -0,0 +1,66 @@ +"""Tests for OCR service and PDF editor service — unit tests with mocking.""" +import os +import sys +import tempfile + +import pytest +from unittest.mock import patch, MagicMock + +from app.services.ocr_service import ocr_image, OCRError, SUPPORTED_LANGUAGES + + +class TestOcrServiceConstants: + def test_supported_languages(self): + """Verify the supported languages dict.""" + assert "eng" in SUPPORTED_LANGUAGES + assert "ara" in SUPPORTED_LANGUAGES + assert "fra" in SUPPORTED_LANGUAGES + assert len(SUPPORTED_LANGUAGES) == 3 + + +class TestOcrImage: + def test_ocr_image_success(self): + """Should return text and char_count from image (mocked pytesseract).""" + mock_pytesseract = MagicMock() + mock_pytesseract.image_to_string.return_value = " Hello World " + mock_pytesseract.pytesseract.tesseract_cmd = "" + + mock_img = MagicMock() + mock_img.mode = "RGB" + mock_img.__enter__ = MagicMock(return_value=mock_img) + mock_img.__exit__ = MagicMock(return_value=False) + + with patch.dict(sys.modules, {"pytesseract": mock_pytesseract}): + with patch("app.services.ocr_service.Image") as mock_pil: + mock_pil.open.return_value = mock_img + result = ocr_image("/fake/path.png", lang="eng") + + assert result["text"] == "Hello World" + assert result["char_count"] == 11 + assert result["lang"] == "eng" + + def test_ocr_image_invalid_lang_fallback(self): + """Invalid language should fall back to 'eng'.""" + mock_pytesseract = MagicMock() + mock_pytesseract.image_to_string.return_value = "Test" + mock_pytesseract.pytesseract.tesseract_cmd = "" + + mock_img = MagicMock() + mock_img.mode = "RGB" + mock_img.__enter__ = MagicMock(return_value=mock_img) + mock_img.__exit__ = MagicMock(return_value=False) + + with patch.dict(sys.modules, {"pytesseract": mock_pytesseract}): + with patch("app.services.ocr_service.Image") as mock_pil: + mock_pil.open.return_value = mock_img + result = ocr_image("/fake/path.png", lang="zzzz") + + assert result["lang"] == "eng" + + +class TestPdfEditorService: + def test_no_edits_raises(self): + """Should raise PDFEditorError when no edits provided.""" + from app.services.pdf_editor_service import apply_pdf_edits, PDFEditorError + with pytest.raises(PDFEditorError, match="No edits"): + apply_pdf_edits("/fake.pdf", "/out.pdf", []) diff --git a/backend/tests/test_pdf_editor.py b/backend/tests/test_pdf_editor.py new file mode 100644 index 0000000..adf39df --- /dev/null +++ b/backend/tests/test_pdf_editor.py @@ -0,0 +1,144 @@ +"""Tests for PDF editor route — /api/pdf-editor/edit.""" +import io +import json +import os +import tempfile +from unittest.mock import MagicMock + +from tests.conftest import make_pdf_bytes + + +# ========================================================================= +# Feature flag enforcement +# ========================================================================= +class TestPdfEditorFeatureFlag: + def test_pdf_editor_disabled_by_default(self, client): + """Should return 403 when FEATURE_EDITOR is off.""" + data = { + "file": (io.BytesIO(make_pdf_bytes()), "doc.pdf"), + "edits": json.dumps([{"type": "text", "page": 1, "x": 100, "y": 200, "content": "Hello"}]), + } + response = client.post( + "/api/pdf-editor/edit", + data=data, + content_type="multipart/form-data", + ) + assert response.status_code == 403 + assert "not enabled" in response.get_json()["error"] + + +# ========================================================================= +# Validation +# ========================================================================= +class TestPdfEditorValidation: + def test_pdf_editor_no_file(self, client, app): + """Should return 400 when no file provided.""" + app.config["FEATURE_EDITOR"] = True + response = client.post("/api/pdf-editor/edit") + assert response.status_code == 400 + assert "No file" in response.get_json()["error"] + + def test_pdf_editor_invalid_json(self, client, app): + """Should return 400 when edits is invalid JSON.""" + app.config["FEATURE_EDITOR"] = True + data = { + "file": (io.BytesIO(make_pdf_bytes()), "doc.pdf"), + "edits": "not valid json{", + } + response = client.post( + "/api/pdf-editor/edit", + data=data, + content_type="multipart/form-data", + ) + assert response.status_code == 400 + assert "Invalid JSON" in response.get_json()["error"] + + def test_pdf_editor_edits_not_array(self, client, app): + """Should return 400 when edits is not an array.""" + app.config["FEATURE_EDITOR"] = True + data = { + "file": (io.BytesIO(make_pdf_bytes()), "doc.pdf"), + "edits": json.dumps({"type": "text"}), + } + response = client.post( + "/api/pdf-editor/edit", + data=data, + content_type="multipart/form-data", + ) + assert response.status_code == 400 + assert "JSON array" in response.get_json()["error"] + + def test_pdf_editor_empty_edits(self, client, app): + """Should return 400 when edits array is empty.""" + app.config["FEATURE_EDITOR"] = True + data = { + "file": (io.BytesIO(make_pdf_bytes()), "doc.pdf"), + "edits": json.dumps([]), + } + response = client.post( + "/api/pdf-editor/edit", + data=data, + content_type="multipart/form-data", + ) + assert response.status_code == 400 + assert "At least one edit" in response.get_json()["error"] + + def test_pdf_editor_too_many_edits(self, client, app): + """Should return 400 when more than 500 edits.""" + app.config["FEATURE_EDITOR"] = True + edits = [{"type": "text", "page": 1, "x": 10, "y": 10, "content": "x"}] * 501 + data = { + "file": (io.BytesIO(make_pdf_bytes()), "doc.pdf"), + "edits": json.dumps(edits), + } + response = client.post( + "/api/pdf-editor/edit", + data=data, + content_type="multipart/form-data", + ) + assert response.status_code == 400 + assert "500" in response.get_json()["error"] + + +# ========================================================================= +# Success paths +# ========================================================================= +class TestPdfEditorSuccess: + def test_pdf_editor_success(self, client, app, monkeypatch): + """Should return 202 with task_id when valid request provided.""" + app.config["FEATURE_EDITOR"] = True + mock_task = MagicMock() + mock_task.id = "edit-task-1" + + tmp_dir = tempfile.mkdtemp() + save_path = os.path.join(tmp_dir, "mock.pdf") + + monkeypatch.setattr( + "app.routes.pdf_editor.validate_actor_file", + lambda f, allowed_types, actor: ("doc.pdf", "pdf"), + ) + monkeypatch.setattr( + "app.routes.pdf_editor.generate_safe_path", + lambda ext, folder_type: ("mock-id", save_path), + ) + monkeypatch.setattr( + "app.routes.pdf_editor.edit_pdf_task.delay", + MagicMock(return_value=mock_task), + ) + + edits = [ + {"type": "text", "page": 1, "x": 100, "y": 200, "content": "Hello World", "fontSize": 14}, + ] + data = { + "file": (io.BytesIO(make_pdf_bytes()), "doc.pdf"), + "edits": json.dumps(edits), + } + response = client.post( + "/api/pdf-editor/edit", + data=data, + content_type="multipart/form-data", + ) + assert response.status_code == 202 + body = response.get_json() + assert body["task_id"] == "edit-task-1" + assert "PDF editing started" in body["message"] diff --git a/backend/tests/test_removebg.py b/backend/tests/test_removebg.py new file mode 100644 index 0000000..7892bdf --- /dev/null +++ b/backend/tests/test_removebg.py @@ -0,0 +1,73 @@ +"""Tests for background removal route — /api/remove-bg.""" +import io +import os +import tempfile +from unittest.mock import MagicMock + +from tests.conftest import make_png_bytes, make_pdf_bytes + + +# ========================================================================= +# Feature flag enforcement +# ========================================================================= +class TestRemoveBgFeatureFlag: + def test_removebg_disabled_by_default(self, client): + """Should return 403 when FEATURE_EDITOR is off.""" + data = {"file": (io.BytesIO(make_png_bytes()), "photo.png")} + response = client.post( + "/api/remove-bg", + data=data, + content_type="multipart/form-data", + ) + assert response.status_code == 403 + assert "not enabled" in response.get_json()["error"] + + +# ========================================================================= +# Validation +# ========================================================================= +class TestRemoveBgValidation: + def test_removebg_no_file(self, client, app): + """Should return 400 when no file provided.""" + app.config["FEATURE_EDITOR"] = True + response = client.post("/api/remove-bg") + assert response.status_code == 400 + assert "No file" in response.get_json()["error"] + + +# ========================================================================= +# Success paths +# ========================================================================= +class TestRemoveBgSuccess: + def test_removebg_success(self, client, app, monkeypatch): + """Should return 202 with task_id when valid image provided.""" + app.config["FEATURE_EDITOR"] = True + mock_task = MagicMock() + mock_task.id = "rembg-task-1" + + tmp_dir = tempfile.mkdtemp() + save_path = os.path.join(tmp_dir, "mock.png") + + monkeypatch.setattr( + "app.routes.removebg.validate_actor_file", + lambda f, allowed_types, actor: ("photo.png", "png"), + ) + monkeypatch.setattr( + "app.routes.removebg.generate_safe_path", + lambda ext, folder_type: ("mock-id", save_path), + ) + monkeypatch.setattr( + "app.routes.removebg.remove_bg_task.delay", + MagicMock(return_value=mock_task), + ) + + data = {"file": (io.BytesIO(make_png_bytes()), "photo.png")} + response = client.post( + "/api/remove-bg", + data=data, + content_type="multipart/form-data", + ) + assert response.status_code == 202 + body = response.get_json() + assert body["task_id"] == "rembg-task-1" + assert "Background removal started" in body["message"] diff --git a/docs/feature-editor.md b/docs/feature-editor.md index 471ce29..8f7dc71 100644 --- a/docs/feature-editor.md +++ b/docs/feature-editor.md @@ -118,6 +118,118 @@ Features: - `src/pages/HomePage.tsx` — Image Resize tool card - `src/pages/AccountPage.tsx` — "Forgot password?" link - `src/utils/fileRouting.ts` — imageResize in tool list + +--- + +## Block B — OCR, Background Removal, PDF Editor (Sprint 2) + +All Block B routes are gated behind `FEATURE_EDITOR=true`. Returns 403 when disabled. + +### B1 — OCR (Optical Character Recognition) + +**Backend:** +- Service: `app/services/ocr_service.py` — `ocr_image()`, `ocr_pdf()` using pytesseract +- Tasks: `app/tasks/ocr_tasks.py` — `ocr_image_task`, `ocr_pdf_task` +- Route: `app/routes/ocr.py` — Blueprint `ocr_bp` at `/api/ocr` + +| Method | Path | Rate limit | Description | +|---|---|---|---| +| `POST` | `/api/ocr/image` | 10/min | Extract text from image | +| `POST` | `/api/ocr/pdf` | 5/min | Extract text from scanned PDF | +| `GET` | `/api/ocr/languages` | — | List supported OCR languages | + +Supported languages: English (`eng`), Arabic (`ara`), French (`fra`). + +**Frontend:** `src/components/tools/OcrTool.tsx` — `/tools/ocr` +- Mode selector (Image / PDF), language selector, text preview with copy, download. + +### B2 — Background Removal + +**Backend:** +- Service: `app/services/removebg_service.py` — `remove_background()` using rembg + onnxruntime +- Task: `app/tasks/removebg_tasks.py` — `remove_bg_task` +- Route: `app/routes/removebg.py` — Blueprint `removebg_bp` at `/api/remove-bg` + +| Method | Path | Rate limit | Description | +|---|---|---|---| +| `POST` | `/api/remove-bg` | 5/min | Remove background (outputs transparent PNG) | + +**Frontend:** `src/components/tools/RemoveBackground.tsx` — `/tools/remove-background` +- Upload image → AI processing → download PNG with transparency. + +### B3 — PDF Editor (Text Annotations) + +**Backend:** +- Service: `app/services/pdf_editor_service.py` — `apply_pdf_edits()` using ReportLab overlay + PyPDF2 +- Task: `app/tasks/pdf_editor_tasks.py` — `edit_pdf_task` +- Route: `app/routes/pdf_editor.py` — Blueprint `pdf_editor_bp` at `/api/pdf-editor` + +| Method | Path | Rate limit | Description | +|---|---|---|---| +| `POST` | `/api/pdf-editor/edit` | 10/min | Apply text annotations to PDF | + +Accepts `file` (PDF) + `edits` (JSON array, max 500). Each edit: `{ type, page, x, y, content, fontSize, color }`. + +### DevOps Changes + +**Dependencies added** (`requirements.txt`): +- `pytesseract>=0.3.10,<1.0` +- `rembg>=2.0,<3.0` +- `onnxruntime>=1.16,<2.0` + +**Dockerfile:** Added `tesseract-ocr`, `tesseract-ocr-eng`, `tesseract-ocr-ara`, `tesseract-ocr-fra` to apt-get. + +**Celery task routing** (`extensions.py`): +- `ocr_tasks.*` → `image` queue +- `removebg_tasks.*` → `image` queue +- `pdf_editor_tasks.*` → `pdf_tools` queue + +### Block B Test Coverage + +| File | Tests | Status | +|---|---|---| +| `test_ocr.py` | 8 | ✅ Passed | +| `test_removebg.py` | 3 | ✅ Passed | +| `test_pdf_editor.py` | 7 | ✅ Passed | +| `test_ocr_service.py` | 4 | ✅ Passed | +| **Full suite** | **180** | **✅ All passed** | + +### Block B Files Created + +**Backend — New:** +- `app/services/ocr_service.py` +- `app/services/removebg_service.py` +- `app/services/pdf_editor_service.py` +- `app/tasks/ocr_tasks.py` +- `app/tasks/removebg_tasks.py` +- `app/tasks/pdf_editor_tasks.py` +- `app/routes/ocr.py` +- `app/routes/removebg.py` +- `app/routes/pdf_editor.py` +- `tests/test_ocr.py` +- `tests/test_removebg.py` +- `tests/test_pdf_editor.py` +- `tests/test_ocr_service.py` + +**Frontend — New:** +- `src/components/tools/OcrTool.tsx` +- `src/components/tools/RemoveBackground.tsx` + +**Backend — Modified:** +- `app/__init__.py` — registered 3 new blueprints (18 total) +- `app/extensions.py` — 3 new task routing rules +- `celery_worker.py` — 3 new task module imports +- `requirements.txt` — pytesseract, rembg, onnxruntime +- `Dockerfile` — tesseract-ocr packages + +**Frontend — Modified:** +- `src/App.tsx` — 2 new lazy routes (`/tools/ocr`, `/tools/remove-background`) +- `src/pages/HomePage.tsx` — OCR + RemoveBG tool cards +- `src/utils/fileRouting.ts` — OCR + RemoveBG in tool arrays +- `src/i18n/en.json` — `tools.ocr` + `tools.removeBg` keys +- `src/i18n/ar.json` — Arabic translations +- `src/i18n/fr.json` — French translations +- `src/services/api.ts` — `text` + `char_count` added to `TaskResult` - `src/i18n/en.json`, `ar.json`, `fr.json` — new keys ### Infrastructure diff --git a/frontend/src/App.tsx b/frontend/src/App.tsx index da60294..2d2f7a9 100644 --- a/frontend/src/App.tsx +++ b/frontend/src/App.tsx @@ -36,6 +36,8 @@ const AddPageNumbers = lazy(() => import('@/components/tools/AddPageNumbers')); const PdfEditor = lazy(() => import('@/components/tools/PdfEditor')); const PdfFlowchart = lazy(() => import('@/components/tools/PdfFlowchart')); const ImageResize = lazy(() => import('@/components/tools/ImageResize')); +const OcrTool = lazy(() => import('@/components/tools/OcrTool')); +const RemoveBackground = lazy(() => import('@/components/tools/RemoveBackground')); function LoadingFallback() { return ( @@ -94,6 +96,8 @@ export default function App() { {/* Image Tools */} } /> } /> + } /> + } /> {/* Video Tools */} } /> diff --git a/frontend/src/components/tools/OcrTool.tsx b/frontend/src/components/tools/OcrTool.tsx new file mode 100644 index 0000000..03e5542 --- /dev/null +++ b/frontend/src/components/tools/OcrTool.tsx @@ -0,0 +1,245 @@ +import { useState, useEffect } from 'react'; +import { useTranslation } from 'react-i18next'; +import { Helmet } from 'react-helmet-async'; +import { ScanText } from 'lucide-react'; +import FileUploader from '@/components/shared/FileUploader'; +import ProgressBar from '@/components/shared/ProgressBar'; +import DownloadButton from '@/components/shared/DownloadButton'; +import AdSlot from '@/components/layout/AdSlot'; +import { useFileUpload } from '@/hooks/useFileUpload'; +import { useTaskPolling } from '@/hooks/useTaskPolling'; +import { generateToolSchema } from '@/utils/seo'; +import { useFileStore } from '@/stores/fileStore'; +import { useConfig } from '@/hooks/useConfig'; + +type OcrMode = 'image' | 'pdf'; + +const LANGUAGES = [ + { value: 'eng', label: 'English' }, + { value: 'ara', label: 'العربية' }, + { value: 'fra', label: 'Français' }, +]; + +export default function OcrTool() { + const { t } = useTranslation(); + const { limits } = useConfig(); + const [phase, setPhase] = useState<'upload' | 'processing' | 'done'>('upload'); + const [lang, setLang] = useState('eng'); + const [mode, setMode] = useState('image'); + const [extractedText, setExtractedText] = useState(''); + + const endpoint = mode === 'pdf' ? '/ocr/pdf' : '/ocr/image'; + const maxSize = mode === 'pdf' ? (limits.pdf ?? 20) : (limits.image ?? 10); + + const { + file, uploadProgress, isUploading, taskId, + error: uploadError, selectFile, startUpload, reset, + } = useFileUpload({ + endpoint, + maxSizeMB: maxSize, + acceptedTypes: mode === 'pdf' ? ['pdf'] : ['png', 'jpg', 'jpeg', 'webp', 'tiff', 'bmp'], + extraData: { lang }, + }); + + const { status, result, error: taskError } = useTaskPolling({ + taskId, + onComplete: () => setPhase('done'), + onError: () => setPhase('done'), + }); + + // Accept file from homepage smart upload + const storeFile = useFileStore((s) => s.file); + const clearStoreFile = useFileStore((s) => s.clearFile); + useEffect(() => { + if (storeFile) { + const ext = storeFile.name.split('.').pop()?.toLowerCase() ?? ''; + if (ext === 'pdf') setMode('pdf'); + else setMode('image'); + selectFile(storeFile); + clearStoreFile(); + } + }, []); // eslint-disable-line react-hooks/exhaustive-deps + + useEffect(() => { + if (result?.text) setExtractedText(result.text); + }, [result]); + + const handleUpload = async () => { + const id = await startUpload(); + if (id) setPhase('processing'); + }; + + const handleReset = () => { + reset(); + setPhase('upload'); + setExtractedText(''); + }; + + const handleCopyText = () => { + navigator.clipboard.writeText(extractedText); + }; + + const acceptMap: Record = mode === 'pdf' + ? { 'application/pdf': ['.pdf'] } + : { + 'image/png': ['.png'], + 'image/jpeg': ['.jpg', '.jpeg'], + 'image/webp': ['.webp'], + 'image/tiff': ['.tiff'], + 'image/bmp': ['.bmp'], + }; + + const schema = generateToolSchema({ + name: t('tools.ocr.title'), + description: t('tools.ocr.description'), + url: `${window.location.origin}/tools/ocr`, + }); + + return ( + <> + + {t('tools.ocr.title')} — {t('common.appName')} + + + + + +
+
+
+ +
+

{t('tools.ocr.title')}

+

{t('tools.ocr.description')}

+
+ + + + {phase === 'upload' && ( +
+ {/* Mode selector */} +
+ +
+ {(['image', 'pdf'] as OcrMode[]).map((m) => ( + + ))} +
+
+ + + + {file && !isUploading && ( + <> + {/* Language selector */} +
+ +
+ {LANGUAGES.map((l) => ( + + ))} +
+
+ + + + )} +
+ )} + + {phase === 'processing' && ( +
+ + {taskError && ( +
+ {taskError} +
+ )} +
+ )} + + {phase === 'done' && result?.status === 'completed' && ( +
+
+

+ {t('tools.ocr.charsExtracted', { count: result.char_count ?? 0 })} +

+