# mrxfr1ends/cloud_drive architecture

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

- Interactive diagram: https://gitdiagram.com/mrxfr1ends/cloud_drive
- Repository: https://github.com/mrxfr1ends/cloud_drive

## Overview

##### Project Type and Purpose
- The project is a backend API service that mirrors core Google Drive features. Its purpose is to handle user authentication, file and folder operations (uploading, downloading, moving to trash, etc.), and enforcing permissions.
- The README clearly details endpoints related to registration, login, file uploads/downloads, folder creation, and file management. This confirms the project is an HTTP REST API service providing cloud storage functionalities.

##### Key Components from the File Structure
- Routes: All API endpoints are defined under the “src/routes/api” directory (with separate files for auth and disk functionality). These represent the entry points to the service.
- Controllers: Found in “src/controllers”, they contain the business logic for each endpoint – for handling files (upload, download), disk operations, and user actions.
- Services: In “src/services”, the FileService, FolderService, and UserService encapsulate core business rules, data manipulation, and interactions that the controllers rely on.
- Models: Located in “src/models”, these define the data structure for File, Folder, and User entities, likely mapped to a database.
- Middlewares: Under “src/middlewares”, components like authentication checks, request validation, and centralized error handling are implemented.
- Helpers & Validators: Utility functions (such as JWT handling, password hashing, and validation logic) reside in “src/helpers” and “src/validators”, ensuring consistency and reusable logic.
- Errors: A dedicated error handling file manages custom error messages and codes (as outlined in the README).

##### Relationships and Interactions
- Client requests hit the defined API routes which then invoke corresponding controllers.
- Controllers use services to actually process user logic and to interact with the data layer via models.
- Middlewares are applied both globally and on specific routes to handle tasks like authentication (using JWT), error catching (serverErrorMiddleware), and request validation.
- Helpers support both controllers and middlewares by providing common functionality (for example, async handler to manage promise rejections).

##### Architectural Patterns and Design Principles
- MVC / Layered Architecture: The separation between controllers, services, and models clearly indicates the adoption of the MVC (or layered) pattern.
- Separation of Concerns: The use of validators, helpers, and middlewares ensures that each component has a specific task – making the system easier to maintain and scale.
- RESTful API design: The project design follows REST principles with descriptive endpoints, support for error codes, and standardized HTTP methods.

##### Relevant Technologies, Frameworks, and Libraries
- Although not explicitly listed, common elements include Node.js (with Express likely), middleware for authentication (JWT based), a database for persistence (MongoDB or similar), and possibly Mongoose for the models.
- Utility libraries for password hashing and async handling are also incorporated.

a. And Clearly Label Components:
- API Routes: Represent the entry point for HTTP requests (supported endpoints like /api/auth/* and /api/disk/*).
- Controllers: (DiskController, DownloadController, UploadController, UserController) that handle incoming requests.
- Services: (FileService, FolderService, UserService) which encapsulate the business logic.
- Models: (File, Folder, User) which represent the data schema.
- Middlewares: such as authMiddleware, serverErrorMiddleware, and validation middleware.
- Helpers & Validators: used across the system for JWT operations, password hashing, and route validation.
- External Components: Although not shown explicitly, include a Database component that stores Users, Files, and Folder data.

b. Relationships and Data Flow:
- Controllers further call Services, which in turn interact with Models (database operations).
- Insert middlewares into the flow where they intercept requests between the client and controllers (authentication and validation checks).
- Error handling flow from Controllers/Middlewares to a centralized error response node.

c. Architectural Patterns:
- Use separate layers (Routes → Controllers → Services → Models) to illustrate the MVC pattern.

d. Tailoring to this Specific Project:
- Given this is a backend API, emphasize the separation between authentication endpoints (/api/auth) and disk management endpoints (/api/disk).
- How error handling is centrally managed (using the errors folder and serverErrorMiddleware).
- Potential future expansion (e.g., additional endpoints for file sharing) and where these would integrate (likely at the Services and Controllers level).

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Global Nodes
    Client["Client"]:::default
    APIGateway["API Gateway"]

    %% API Routes Subgraph
    subgraph "API Routes"
        AuthRoutes["Auth Routes"]
        DiskRoutes["Disk Routes"]
        IndexRoutes["Index Routes"]
    end

    %% Middlewares Subgraph
    subgraph "Middlewares"
        AuthMW["Authentication Middleware"]:::middlewares
        ValMW["Validation Middleware"]:::middlewares
        UserMW["User Middleware"]:::middlewares
        ErrorMW["Error Middleware"]:::middlewares
    end

    CombinedMW["Combined Middleware Handler"]

    %% Controllers Subgraph
    subgraph "Controllers"
        UserController["UserController"]:::controllers
        DiskController["DiskController"]:::controllers
        UploadController["UploadController"]:::controllers
        DownloadController["DownloadController"]:::controllers
    end

    %% Services Subgraph
    subgraph "Services"
        FileService["FileService"]:::services
        FolderService["FolderService"]:::services
        UserService["UserService"]:::services
    end

    %% Models Subgraph
    subgraph "Models"
        FileModel["File Model"]:::models
        FolderModel["Folder Model"]:::models
        UserModel["User Model"]:::models
    end

    Database(("Database")):::models

    %% Helpers & Utilities Subgraph
    subgraph "Helpers & Utilities"
        AsyncHandler["Async Handler"]:::helpers
        BucketHelper["Bucket Helper"]:::helpers
        JWTH["JWT Handling"]:::helpers
        PasswordHashing["Password Hashing"]:::helpers
    end

    %% Validators Subgraph
    subgraph "Validators"
        AuthValidator["Auth Validator"]:::validators
        DiskFileValidator["Disk/File Validator"]:::validators
    end

    %% Errors Subgraph
    subgraph "Errors"
        CentralError["Centralized Error Handler"]:::errors
    end

    %% Connections
    Client --> APIGateway
    APIGateway --> AuthRoutes
    APIGateway --> DiskRoutes
    APIGateway --> IndexRoutes

    AuthRoutes --> AuthMW
    DiskRoutes --> ValMW
    IndexRoutes --> UserMW

    AuthMW --> CombinedMW
    ValMW --> CombinedMW
    UserMW --> CombinedMW

    CombinedMW --> UserController
    CombinedMW --> DiskController
    CombinedMW --> UploadController
    CombinedMW --> DownloadController

    UserController --> UserService
    DiskController --> FileService
    DiskController --> FolderService
    UploadController --> FileService
    DownloadController --> FileService

    UserService --> UserModel
    FileService --> FileModel
    FolderService --> FolderModel

    FileModel --> Database
    FolderModel --> Database
    UserModel --> Database

    %% Error handling dashed connections from controllers to Centralized Error Handler
    UserController -.-> CentralError
    DiskController -.-> CentralError
    UploadController -.-> CentralError
    DownloadController -.-> CentralError

    %% Middleware error handling chain
    CombinedMW -.-> ErrorMW
    ErrorMW --> CentralError

    %% Helpers usage (dashed)
    UserController -.-> AsyncHandler
    AuthMW -.-> JWTH
    DiskController -.-> BucketHelper
    UserController -.-> PasswordHashing

    %% Validators usage (dashed)
    UserController -.-> AuthValidator
    DiskController -.-> DiskFileValidator
    UploadController -.-> DiskFileValidator
    DownloadController -.-> DiskFileValidator

    %% Click Events for API Routes
    click AuthRoutes "https://github.com/mrxfr1ends/cloud_drive/blob/main/src/routes/api/auth.routes.js"
    click DiskRoutes "https://github.com/mrxfr1ends/cloud_drive/blob/main/src/routes/api/disk.routes.js"
    click IndexRoutes "https://github.com/mrxfr1ends/cloud_drive/blob/main/src/routes/api/index.js"

    %% Click Events for Controllers
    click UserController "https://github.com/mrxfr1ends/cloud_drive/blob/main/src/controllers/UserController.js"
    click DiskController "https://github.com/mrxfr1ends/cloud_drive/blob/main/src/controllers/DiskController.js"
    click UploadController "https://github.com/mrxfr1ends/cloud_drive/blob/main/src/controllers/UploadController.js"
    click DownloadController "https://github.com/mrxfr1ends/cloud_drive/blob/main/src/controllers/DownloadController.js"

    %% Click Events for Services
    click FileService "https://github.com/mrxfr1ends/cloud_drive/blob/main/src/services/FileService.js"
    click FolderService "https://github.com/mrxfr1ends/cloud_drive/blob/main/src/services/FolderService.js"
    click UserService "https://github.com/mrxfr1ends/cloud_drive/blob/main/src/services/UserService.js"

    %% Click Events for Models
    click FileModel "https://github.com/mrxfr1ends/cloud_drive/blob/main/src/models/File.js"
    click FolderModel "https://github.com/mrxfr1ends/cloud_drive/blob/main/src/models/Folder.js"
    click UserModel "https://github.com/mrxfr1ends/cloud_drive/blob/main/src/models/User.js"

    %% Click Events for Middlewares
    click AuthMW "https://github.com/mrxfr1ends/cloud_drive/blob/main/src/middlewares/authMiddleware.js"
    click ErrorMW "https://github.com/mrxfr1ends/cloud_drive/blob/main/src/middlewares/serverErrorMiddleware.js"
    click UserMW "https://github.com/mrxfr1ends/cloud_drive/blob/main/src/middlewares/userMiddleware.js"
    click ValMW "https://github.com/mrxfr1ends/cloud_drive/blob/main/src/middlewares/validateMiddleware.js"

    %% Click Events for Helpers & Utilities
    click AsyncHandler "https://github.com/mrxfr1ends/cloud_drive/blob/main/src/helpers/asyncHandler.js"
    click BucketHelper "https://github.com/mrxfr1ends/cloud_drive/blob/main/src/helpers/bucketHelper.js"
    click JWTH "https://github.com/mrxfr1ends/cloud_drive/blob/main/src/helpers/jwt.js"
    click PasswordHashing "https://github.com/mrxfr1ends/cloud_drive/blob/main/src/helpers/passwordHash.js"

    %% Click Events for Validators
    click AuthValidator "https://github.com/mrxfr1ends/cloud_drive/blob/main/src/validators/auth.validator.js"
    click DiskFileValidator "https://github.com/mrxfr1ends/cloud_drive/blob/main/src/validators/disk.validator.js"

    %% Click Event for Errors
    click CentralError "https://github.com/mrxfr1ends/cloud_drive/blob/main/src/errors/index.js"

    %% Styles
    classDef controllers fill:#87CEFA,stroke:#333,stroke-width:2px,color:#000;
    classDef services fill:#90EE90,stroke:#333,stroke-width:2px,color:#000;
    classDef models fill:#FFA07A,stroke:#333,stroke-width:2px,color:#000;
    classDef middlewares fill:#D3D3D3,stroke:#333,stroke-width:2px,color:#000;
    classDef helpers fill:#FFFF99,stroke:#333,stroke-width:2px,color:#000;
    classDef validators fill:#FFC0CB,stroke:#333,stroke-width:2px,color:#000;
    classDef errors fill:#F08080,stroke:#333,stroke-width:2px,color:#000;
    
    class Client,APIGateway,AuthRoutes,DiskRoutes,IndexRoutes,CombinedMW,Database default
```
