# rahuldadige/pastebox architecture

> Architecture diagram of the GitHub repository rahuldadige/pastebox, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-02-15). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/rahuldadige/pastebox
- Repository: https://github.com/rahuldadige/pastebox

## Overview

**PasteBox is a full-stack web application**: a React (Vite) SPA client + a Node/Express REST API server, backed by **MongoDB (Mongoose)** and **Cloudinary** for blob/file storage. It supports two primary user journeys:
- **Registered users** (JWT auth): dashboard, manage uploads, aliasing, expiry/password updates, delete, etc.
- **Guest users**: upload/share without account (still supports password/expiry).

##### Client (React SPA)
**Root:** `client/`
Key architectural signals:
- **Vite entry:** `client/index.html`, `client/src/main.jsx`
- **App + routing:** `client/src/App.jsx` (likely routes/layout composition)
- **Auth gating:** `client/src/components/Auth/RequireAuth.jsx`, `NotRequireAuth.jsx`
- **State management:** Redux Toolkit in `client/src/redux/**`
- `authSlice.js`, `authThunk.js`
- `fileSlice.js`, `fileThunk.js`
- **HTTP layer:** `client/src/config/axiosInstance.js` (central API base URL, interceptors likely)
- **Pages/sections:** `Home`, `Dashboard/*`, `Guest/*`, `Download*`, `AliasDownloadPage.jsx`, etc.

- Router / Pages
- Auth guard layer
- Redux store + async thunks (API calls)
- Axios API client

##### Server (Express API)
**Root:** `server/`
Key architectural signals:
- **Entrypoints:** `server/src/index.js` (server start), `server/src/app.js` (Express app wiring)
- **DB:** `server/src/db/index.js`
- **Config:** `server/src/config/cloudinary.js`, `server/src/constant.js`
- **Routes:** `server/src/routes/*.routes.js`
- **Controllers:** `server/src/controllers/*.controller.js`
- **Models:** `server/src/models/*.models.js` for `user`, `file`, `guestFile`, `alias`
- **Middlewares:**
- `auth.middlewares.js` (JWT verification, probably role/user attach)
- `upload.middlewares.js` (Multer pipeline, file validation, etc.)

**Routes → Middleware → Controllers → Models → DB/Cloudinary**.

##### External dependencies worth explicit nodes
- **MongoDB Atlas** (per README prerequisites)
- **Cloudinary** (file storage + retrieval)
- **JWT** signing/verification (auth boundary)
- **BCrypt** (password-protect links / hashing)
- **Helmet / Rate Limiting / Compression** (cross-cutting concerns)

##### React SPA (Vite)
- Auth/Guest UI
- Redux store + thunks
- Axios client
##### Express API (Node.js)
- Route modules: `user.routes`, `file.routes`, `alias.routes`
- Middlewares: auth, upload, security/perf (helmet/rate-limit/compression)
##### MongoDB (Mongoose)
- Collections: users, files, guestFiles, aliases
##### Cloudinary
- Blob storage for uploaded files

##### Frontend ↔ Backend
- **HTTPS/REST JSON** calls from SPA to API:
- `POST /api/users/register`
- `POST /api/users/login` → returns **JWT**
- `POST /api/files/upload` (auth required) → multipart/form-data
- `GET /api/files/f/:code` (download metadata / access flow)
- `POST /api/alias/upload` (alias creation)
- That **Axios instance** uses `VITE_API_BASE_URL`.

##### Backend ↔ MongoDB
- Controllers use Mongoose models:
- `user.models.js` for auth/user profile
- `file.models.js` for user uploads
- `guestFile.models.js` for guest uploads
- `alias.models.js` for mapping aliases → file groups / codes

##### Backend ↔ Cloudinary
- Upload flow uses `upload.middlewares.js` (Multer) and `config/cloudinary.js`.
- Store **Cloudinary public_id/URL** in MongoDB.
- Download flow likely:
- validate code/alias + password/expiry in MongoDB
- fetch Cloudinary URL / stream / redirect, then client downloads as blob (README mentions blob downloads)

##### Cross-cutting security/performance
- **Helmet**: headers hardening
- **Rate limiting**: anti-abuse
- **Compression**: response compression
- **JWT middleware**: protects authenticated endpoints
- **BCrypt**: password hashing for protected file links

- **SPA + REST API** separation (client-only rendering, server is JSON API)
- **Layered backend** (Routes → Middlewares → Controllers → Models)
- **Externalized blob storage** (Cloudinary) + metadata in MongoDB
- **Stateless auth** (JWT): frontend stores token; backend verifies per request

##### Registered Upload + Share
- SPA → POST /login → JWT
- SPA → POST /files/upload (multipart + Authorization: Bearer)
- API: auth middleware → upload middleware → Cloudinary upload → MongoDB save metadata (ownerId, cloudinaryUrl, expiry, passwordHash, code)
- API → SPA: returns code/link/metadata
- SPA: show QR code / share link

##### Guest Upload + Download
- SPA → POST guest upload (may hit same `/files/upload` with different handling or a guest endpoint; model `guestFile.models.js` strongly suggests different persistence)
- API → Cloudinary + MongoDB(guestFiles)
- Downloader SPA → GET /files/f/:code → validate expiry/password → return download URL/stream → blob download in browser

##### Connect
- Axios → Routes (label endpoints)
- Controllers → MongoDB (metadata)
- Controllers → Cloudinary (file blob)
- Endpoints requiring JWT (e.g., `/api/files/upload` auth required per README)

## Diagram (Mermaid)

```mermaid
flowchart TD
  %% =========================
  %% Level 1/2: Context + Containers (vertical)
  %% =========================
  U["User<br/>(Guest / Registered)"]:::external

  subgraph "Client (Browser)"
    B["Browser"]:::external
    subgraph "React SPA (Vite)"
      SPA["SPA Shell + UI"]:::frontend
      Router["App Routing / Layout"]:::frontend
      AuthGuards["Auth Guard Layer<br/>(RequireAuth / NotRequireAuth)"]:::security
      Redux["Redux Toolkit Store"]:::frontend
      AuthState["Auth Slice + Thunks"]:::frontend
      FileState["File Slice + Thunks"]:::frontend
      Axios["Axios Instance<br/>(VITE_API_BASE_URL + interceptors)"]:::api
      GuestUI["Guest Flow UI"]:::frontend
      DashUI["Dashboard UI (Registered)"]:::frontend
      DownloadUI["Download / Access UI<br/>(Code + Alias)"]:::frontend
    end
  end

  subgraph "Server (Node.js)"
    subgraph "Express API"
      API["Express App"]:::backend
      SecurityPerf["Security + Performance<br/>(Helmet / RateLimit / Compression)"]:::security

      subgraph "Routes"
        UserRoutes["user.routes"]:::backend
        FileRoutes["file.routes"]:::backend
        AliasRoutes["alias.routes"]:::backend
      end

      subgraph "Middlewares"
        JWT["JWT Auth Middleware"]:::security
        UploadMW["Upload Middleware<br/>(Multer + validation)"]:::security
        Bcrypt["BCrypt (hash/verify)"]:::security
        Expiry["Expiry Checks"]:::security
      end

      subgraph "Controllers"
        UserCtl["user.controller"]:::backend
        FileCtl["file.controller"]:::backend
        AliasCtl["alias.controller"]:::backend
      end

      subgraph "Data Access (Mongoose)"
        UserModel["user.models (users)"]:::data
        FileModel["file.models (files)"]:::data
        GuestFileModel["guestFile.models (guestFiles)"]:::data
        AliasModel["alias.models (aliases)"]:::data
        DBConn["MongoDB Connection (Mongoose)"]:::data
      end

      subgraph "Integrations"
        CloudCfg["Cloudinary Config/SDK"]:::integration
      end
    end
  end

  subgraph "External Services"
    Mongo["MongoDB Atlas"]:::datastore
    Cloud["Cloudinary (Blob Storage)"]:::externalService
  end

  %% =========================
  %% Hosting (optional, vertical)
  %% =========================
  subgraph "Deployment (Vercel)"
    VercelClient["Vercel: Client Hosting"]:::platform
    VercelServer["Vercel: Server Hosting"]:::platform
  end

  %% =========================
  %% User -> Client
  %% =========================
  U -->|"uses"| B
  B -->|"loads"| SPA

  %% SPA internal wiring
  SPA --> Router
  Router --> AuthGuards
  SPA --> Redux
  Redux --> AuthState
  Redux --> FileState
  AuthState --> Axios
  FileState --> Axios
  Router --> GuestUI
  Router --> DashUI
  Router --> DownloadUI

  %% Deployment links
  VercelClient -->|"serves"| SPA
  VercelServer -->|"hosts"| API

  %% =========================
  %% Client -> API (REST)
  %% =========================
  Axios -->|"HTTPS_JSON: /api/users/register"| UserRoutes
  Axios -->|"HTTPS_JSON: /api/users/login (JWT)"| UserRoutes

  Axios -->|"HTTPS_multipart/form-data: /api/files/upload (JWT)"| FileRoutes
  Axios -->|"HTTPS_JSON: /api/files/f/:code"| FileRoutes

  Axios -->|"HTTPS_JSON: /api/alias/upload"| AliasRoutes

  %% =========================
  %% Express app pipeline
  %% =========================
  API --> SecurityPerf
  SecurityPerf --> UserRoutes
  SecurityPerf --> FileRoutes
  SecurityPerf --> AliasRoutes

  %% Routes -> middlewares -> controllers
  UserRoutes -->|"validate + auth(optional)"| JWT
  JWT --> UserCtl

  FileRoutes -->|"auth_required (registered)"| JWT
  FileRoutes -->|"guest_or_registered upload"| UploadMW
  UploadMW --> FileCtl
  JWT --> FileCtl

  AliasRoutes -->|"auth_required (typically)"| JWT
  JWT --> AliasCtl

  %% Download/access validation
  FileRoutes -->|"access_validation"| Expiry
  FileRoutes -->|"password_protection"| Bcrypt
  Expiry --> FileCtl
  Bcrypt --> FileCtl

  %% Controllers -> Models -> DB
  UserCtl --> UserModel
  FileCtl --> FileModel
  FileCtl --> GuestFileModel
  AliasCtl --> AliasModel

  UserModel -->|"Mongoose_queries"| DBConn
  FileModel -->|"Mongoose_queries"| DBConn
  GuestFileModel -->|"Mongoose_queries"| DBConn
  AliasModel -->|"Mongoose_queries"| DBConn

  DBConn -->|"connects"| Mongo

  %% Controllers -> Cloudinary (store blob + persist metadata)
  FileCtl -->|"Cloudinary_SDK_upload/asset_ops"| CloudCfg
  CloudCfg --> Cloud
  FileCtl -->|"stores public_id/url in MongoDB"| FileModel
  FileCtl -->|"stores public_id/url in MongoDB"| GuestFileModel

  %% Download flow (URL/stream)
  FileCtl -->|"returns Cloudinary_URL_or_stream"| Axios

  %% =========================
  %% Click events (from component mapping)
  %% =========================
  click SPA "https://github.com/rahuldadige/pastebox/tree/main/client/"
  click Router "https://github.com/rahuldadige/pastebox/blob/main/client/src/App.jsx"
  click AuthGuards "https://github.com/rahuldadige/pastebox/blob/main/client/src/components/Auth/RequireAuth.jsx"
  click Redux "https://github.com/rahuldadige/pastebox/blob/main/client/src/redux/store.js"
  click AuthState "https://github.com/rahuldadige/pastebox/blob/main/client/src/redux/slice/auth/authSlice.js"
  click FileState "https://github.com/rahuldadige/pastebox/blob/main/client/src/redux/slice/file/fileSlice.js"
  click Axios "https://github.com/rahuldadige/pastebox/blob/main/client/src/config/axiosInstance.js"
  click GuestUI "https://github.com/rahuldadige/pastebox/tree/main/client/src/components/Guest/"
  click DashUI "https://github.com/rahuldadige/pastebox/tree/main/client/src/components/Dashboard/"
  click DownloadUI "https://github.com/rahuldadige/pastebox/blob/main/client/src/components/DownloadPage.jsx"

  click API "https://github.com/rahuldadige/pastebox/blob/main/server/src/app.js"
  click DBConn "https://github.com/rahuldadige/pastebox/blob/main/server/src/db/index.js"
  click CloudCfg "https://github.com/rahuldadige/pastebox/blob/main/server/src/config/cloudinary.js"
  click UserRoutes "https://github.com/rahuldadige/pastebox/blob/main/server/src/routes/user.routes.js"
  click FileRoutes "https://github.com/rahuldadige/pastebox/blob/main/server/src/routes/file.routes.js"
  click AliasRoutes "https://github.com/rahuldadige/pastebox/blob/main/server/src/routes/alias.routes.js"
  click UserCtl "https://github.com/rahuldadige/pastebox/blob/main/server/src/controllers/user.controller.js"
  click FileCtl "https://github.com/rahuldadige/pastebox/blob/main/server/src/controllers/file.controller.js"
  click AliasCtl "https://github.com/rahuldadige/pastebox/blob/main/server/src/controllers/alias.controller.js"
  click JWT "https://github.com/rahuldadige/pastebox/blob/main/server/src/middlewares/auth.middlewares.js"
  click UploadMW "https://github.com/rahuldadige/pastebox/blob/main/server/src/middlewares/upload.middlewares.js"
  click UserModel "https://github.com/rahuldadige/pastebox/blob/main/server/src/models/user.models.js"
  click FileModel "https://github.com/rahuldadige/pastebox/blob/main/server/src/models/file.models.js"
  click GuestFileModel "https://github.com/rahuldadige/pastebox/blob/main/server/src/models/guestFile.models.js"
  click AliasModel "https://github.com/rahuldadige/pastebox/blob/main/server/src/models/alias.models.js"
  click VercelClient "https://github.com/rahuldadige/pastebox/blob/main/client/vercel.json"
  click VercelServer "https://github.com/rahuldadige/pastebox/blob/main/server/vercel.json"

  %% Also map remaining specified files (not all are nodes)
  click B "https://github.com/rahuldadige/pastebox/blob/main/client/src/main.jsx"
  click U "https://github.com/rahuldadige/pastebox/blob/main/client/index.html"
  click SecurityPerf "https://github.com/rahuldadige/pastebox/blob/main/server/src/constant.js"

  %% =========================
  %% Styles (colored)
  %% =========================
  classDef frontend fill:#1f77b4,stroke:#0b2f4a,color:#ffffff,stroke-width:1px;
  classDef backend fill:#2b6cb0,stroke:#1a365d,color:#ffffff,stroke-width:1px;
  classDef api fill:#2c5282,stroke:#1a365d,color:#ffffff,stroke-width:1px;
  classDef data fill:#2f855a,stroke:#22543d,color:#ffffff,stroke-width:1px;
  classDef datastore fill:#38a169,stroke:#22543d,color:#ffffff,stroke-width:1px;
  classDef external fill:#4a5568,stroke:#2d3748,color:#ffffff,stroke-width:1px;
  classDef externalService fill:#dd6b20,stroke:#7b341e,color:#ffffff,stroke-width:1px;
  classDef integration fill:#c05621,stroke:#7b341e,color:#ffffff,stroke-width:1px;
  classDef security fill:#7c3aed,stroke:#3b0764,color:#ffffff,stroke-width:1px,stroke-dasharray: 4 3;
  classDef platform fill:#0ea5e9,stroke:#0c4a6e,color:#ffffff,stroke-width:1px;
```
