# alihmaidi1/easychunckupload architecture

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

- Interactive diagram: https://gitdiagram.com/alihmaidi1/easychunckupload
- Repository: https://github.com/alihmaidi1/easychunckupload

## Overview

##### Project Type and Purpose
- This is a .NET 9.0 class library (“EasyChunkUpload”) that implements a resumable, concurrent chunk-based file-upload service.
- It provides an injectable `IChunkUpload` API for ASP.NET controllers, uses EF Core to track upload sessions, stores chunk files in a temp folder, merges them atomically, and runs a background cleanup service.

##### File Structure Analysis
- Top-level solution: `EasyChunckUpload.sln`
- Library project: `EasyChunkUpload/`
- `ChunkExtension/` holds DTOs and helper logic (`ChunkHelper`, `ChunkResponse`)
- `Enum/`: `UploadStatus` enum
- `Model/`: `FileModel` EF Core entity
- `Services/`:
– `ChunkUpload/` implements `IChunkUpload` session logic, chunk validation, merging
– `Cleanup/` contains `CleanupService` and `BackgroundCleanupHostedService` for temp-file GC
– `FileHelper/` abstracts file-system operations
– `FileService/` wraps higher-level file interactions
- `DependencyInjection.cs` wires up settings (`ChunkUploadSettings`) and DI registrations
- Test projects:
- `EasyChunkUploadTest/`: unit tests with Moq stubs
- `EasyChunkUpload.IntegrationTest/`: end-to-end tests

##### Key Architectural Insights from README
- Core interface: `IChunkUpload` with methods to start session, upload chunks (stream or byte[]), query status, complete/merge, cancel.
- Configurable settings via `ChunkUploadSettings` (temp folder, cleanup interval).
- Uses `SemaphoreSlim` for concurrency control.
- EF Core `DbContext` (user-provided) persists `FileModel` records.
- Background hosted service performs auto-cleanup on a schedule.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% ASP.NET Core App Layer
    subgraph "ASP.NET Core App"
        Controller["UploadController"]:::publicAPI
        DI["DependencyInjection.cs"]:::publicAPI
    end

    %% Library Layer
    subgraph "EasyChunkUpload Library"
        subgraph "Public API"
            UploadAPI["IChunkUpload"]:::publicAPI
        end
        subgraph "Services"
            ChunkService["ChunkUpload Service"]:::core
            subgraph "FileService"
                FileServiceInterface["IFileService"]:::core
                FileServiceImpl["FileService"]:::core
            end
            subgraph "FileHelper"
                FileHelperInterface["IFileHelper"]:::core
                FileHelperImpl["FileHelper"]:::core
            end
            subgraph "Cleanup"
                CleanupInterface["ICleanupService"]:::core
                CleanupServiceNode["CleanupService"]:::core
                HostedService["BackgroundCleanupHostedService"]:::background
            end
        end
        subgraph "Utilities"
            ChunkHelper["ChunkHelper"]:::util
            ChunkResponse["ChunkResponse"]:::util
            ChunkSettings["ChunkUploadSettings"]:::util
            UploadStatus["UploadStatus"]:::util
            FileModelNode["FileModel"]:::util
        end
    end

    %% Infrastructure
    subgraph "Infrastructure"
        FileSystem["File System (temp folder)"]:::infra
        Database["Database (EF Core)"]:::infra
    end

    %% External Client
    Client["Client (HTTP)"]:::publicAPI

    %% Data Flows
    Client -->|POST StartUpload| Controller
    Controller -->|calls StartUploadAsync| UploadAPI
    UploadAPI -->|create session| Database

    Client -->|POST UploadChunk| Controller
    Controller -->|calls UploadChunkAsync| UploadAPI
    UploadAPI -->|writes chunk| FileHelperImpl
    FileHelperImpl -->|stores chunk| FileSystem
    UploadAPI -->|update status| Database

    Client -->|GET PollStatus| Controller
    Controller -->|calls GetLastChunk / GetLostChunkNumber| UploadAPI
    UploadAPI -->|query session| Database

    Client -->|POST CompleteUpload| Controller
    Controller -->|calls ChunkUploadCompleted| UploadAPI
    UploadAPI -->|merge chunks| FileSystem
    UploadAPI -->|update final record| Database

    HostedService -->|triggers cleanup| CleanupServiceNode
    CleanupServiceNode -->|delete expired chunks| FileSystem
    CleanupServiceNode -->|delete expired sessions| Database

    %% Dependency Injection
    DI -->|registers IChunkUpload| UploadAPI
    DI -->|registers ChunkUploadService| ChunkService
    DI -->|registers IFileService| FileServiceInterface
    DI -->|registers FileService| FileServiceImpl
    DI -->|registers IFileHelper| FileHelperInterface
    DI -->|registers FileHelper| FileHelperImpl
    DI -->|registers ICleanupService| CleanupInterface
    DI -->|registers CleanupService| CleanupServiceNode
    DI -->|registers BackgroundCleanupHostedService| HostedService
    DI -->|configures ChunkUploadSettings| ChunkSettings

    %% Concurrency Note
    ChunkService ---|uses SemaphoreSlim| ChunkService

    %% Click Events
    click UploadAPI "https://github.com/alihmaidi1/easychunckupload/blob/master/EasyChunkUpload/Services/ChunkUpload/IChunkUpload.cs"
    click ChunkService "https://github.com/alihmaidi1/easychunckupload/blob/master/EasyChunkUpload/Services/ChunkUpload/ChunkUpload.cs"
    click FileServiceInterface "https://github.com/alihmaidi1/easychunckupload/blob/master/EasyChunkUpload/Services/FileService/IFileService.cs"
    click FileServiceImpl "https://github.com/alihmaidi1/easychunckupload/blob/master/EasyChunkUpload/Services/FileService/FileService.cs"
    click FileHelperInterface "https://github.com/alihmaidi1/easychunckupload/blob/master/EasyChunkUpload/Services/FileHelper/IFileHelper.cs"
    click FileHelperImpl "https://github.com/alihmaidi1/easychunckupload/blob/master/EasyChunkUpload/Services/FileHelper/FileHelper.cs"
    click CleanupInterface "https://github.com/alihmaidi1/easychunckupload/blob/master/EasyChunkUpload/Services/Cleanup/ICleanupService.cs"
    click CleanupServiceNode "https://github.com/alihmaidi1/easychunckupload/blob/master/EasyChunkUpload/Services/Cleanup/CleanupService.cs"
    click HostedService "https://github.com/alihmaidi1/easychunckupload/blob/master/EasyChunkUpload/Services/Cleanup/BackgroundCleanupHostedService.cs"
    click ChunkHelper "https://github.com/alihmaidi1/easychunckupload/blob/master/EasyChunkUpload/ChunkExtension/ChunkHelper.cs"
    click ChunkResponse "https://github.com/alihmaidi1/easychunckupload/blob/master/EasyChunkUpload/ChunkExtension/ChunkResponse.cs"
    click ChunkSettings "https://github.com/alihmaidi1/easychunckupload/blob/master/EasyChunkUpload/ChunkExtension/ChunkUploadSettings.cs"
    click UploadStatus "https://github.com/alihmaidi1/easychunckupload/blob/master/EasyChunkUpload/Enum/UploadStatus.cs"
    click FileModelNode "https://github.com/alihmaidi1/easychunckupload/blob/master/EasyChunkUpload/Model/FileModel.cs"
    click DI "https://github.com/alihmaidi1/easychunckupload/blob/master/EasyChunkUpload/DependencyInjection.cs"
    click Controller "https://github.com/alihmaidi1/easychunckupload/blob/master/EasyChunckUpload.sln"

    %% Styles
    classDef publicAPI fill:#ADD8E6,stroke:#000;
    classDef core fill:#90EE90,stroke:#000;
    classDef infra fill:#D3D3D3,stroke:#000;
    classDef background fill:#FFA500,stroke:#000;
    classDef util fill:#FFFACD,stroke:#000;
    classDef test fill:#FFB6C1,stroke:#000;
```
