# iyear/tdl architecture

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

- Interactive diagram: https://gitdiagram.com/iyear/tdl
- Repository: https://github.com/iyear/tdl

## Overview

##### Main Components

a. Core Components (from /core directory):
- Downloader System
- Uploader System
- Forwarder System
- Storage Management
- Telegram Client Interface
- Media Handler
- Connection Pool (DCPool)

b. Application Layer (from /app directory):
- Chat Management
- Download Operations
- Upload Operations
- Forward Operations
- Login Handler
- Migration Tools
- Extension System

c. Package Layer (from /pkg directory):
- Configuration Management
- Expression Engine
- Key Management
- Key-Value Store
- Progress Tracking
- Template Functions
- Path Management

**[User CLI Interface]**
- ↓
**[Command Layer (cmd/*)]**
- ↓
**[Application Layer]**
- ↙   ↓   ↘
**[Core Services] ↔ [Storage Layer]**
- ↓
**[Telegram API]**

##### Key Relationships to Highlight

a. Data Flow:
- CLI → Command Processing → Application Services
- Application Services ↔ Core Services
- Core Services ↔ Telegram API
- All Components ↔ Storage Layer

b. Component Dependencies:
- How the extension system integrates with core functionality
- Middleware chain in the DCPool
- Demonstrate how the progress tracking system interfaces with operations

##### Implementation Details

- Authentication Flow
- File Processing Pipeline
- Message Forwarding Logic
- Media Type Handling
- Error Recovery Systems
- Storage Management

**┌─ User Interface Layer ─┐**
- CLI Commands        │
- Progress Display    │
- ┬───────────┘
- ↓
**┌─ Application Layer ──┐**
- Command Handlers   │
- Business Logic     │
- ┬──────────┘
- ↓
**┌─ Core Services ──────┐**
- Download Engine    │
- Upload Engine      │
- Forward Engine     │
- Media Handler      │
- ┬──────────┘
- ↓
**┌─ Infrastructure ────┐**
- Storage          │
- Connection Pool  │
- Telegram Client  │
- ┬─────────┘
- ↓
**┌─ External Services ─┐**
- Telegram API     │
- ┘

##### Additional Notes

- Version information and supported Telegram API version
- Notes about scalability and performance considerations

Remember to maintain a balance between detail and clarity, focusing on the key architectural decisions that make this project unique, such as its efficient download mechanism and extension system.

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Styles
    classDef ui fill:#2196F3,stroke:#1565C0,color:white
    classDef app fill:#4CAF50,stroke:#2E7D32,color:white
    classDef core fill:#FF9800,stroke:#EF6C00,color:white
    classDef pkg fill:#9C27B0,stroke:#6A1B9A,color:white
    classDef ext fill:#607D8B,stroke:#37474F,color:white
    
    %% User Interface Layer
    subgraph CLI
        UserInterface["CLI Interface"]
        Commands["Command Handlers"]:::ui
    end
    
    %% Command Layer
    subgraph CommandLayer
        RootCmd["Root Command"]:::ui
        DLCmd["Download Command"]:::ui
        UPCmd["Upload Command"]:::ui
        FWDCmd["Forward Command"]:::ui
        LoginCmd["Login Command"]:::ui
    end
    
    %% Application Layer
    subgraph AppLayer
        ChatMgmt["Chat Management"]:::app
        DLOps["Download Operations"]:::app
        UPOps["Upload Operations"]:::app
        FWDOps["Forward Operations"]:::app
        LoginHandler["Login Handler"]:::app
        MigrationTools["Migration Tools"]:::app
        ExtSystem["Extension System"]:::app
    end
    
    %% Core Layer
    subgraph CoreLayer
        Downloader["Downloader System"]:::core
        Uploader["Uploader System"]:::core
        Forwarder["Forwarder System"]:::core
        Storage["Storage Management"]:::core
        TClient["Telegram Client"]:::core
        MediaHandler["Media Handler"]:::core
        DCPool["Connection Pool"]:::core
    end
    
    %% Package Layer
    subgraph PkgLayer
        ExprEngine["Expression Engine"]:::pkg
        KeyMgmt["Key Management"]:::pkg
        KVStore["Key-Value Store"]:::pkg
        Progress["Progress Tracking"]:::pkg
        TplFunc["Template Functions"]:::pkg
        PathMgmt["Path Management"]:::pkg
    end
    
    %% External Layer
    TelegramAPI["Telegram API"]:::ext
    
    %% Relationships
    UserInterface --> Commands
    Commands --> CommandLayer
    
    RootCmd --> AppLayer
    DLCmd --> DLOps
    UPCmd --> UPOps
    FWDCmd --> FWDOps
    LoginCmd --> LoginHandler
    
    DLOps --> Downloader
    UPOps --> Uploader
    FWDOps --> Forwarder
    LoginHandler --> TClient
    
    Downloader --> DCPool
    Uploader --> DCPool
    Forwarder --> DCPool
    
    DCPool --> TelegramAPI
    TClient --> TelegramAPI
    
    %% Click Events
    click Downloader "https://github.com/iyear/tdl/blob/master/core/downloader/downloader.go"
    click Uploader "https://github.com/iyear/tdl/blob/master/core/uploader/uploader.go"
    click Forwarder "https://github.com/iyear/tdl/blob/master/core/forwarder/forwarder.go"
    click Storage "https://github.com/iyear/tdl/blob/master/core/storage/storage.go"
    click TClient "https://github.com/iyear/tdl/blob/master/core/tclient/tclient.go"
    click MediaHandler "https://github.com/iyear/tdl/blob/master/core/tmedia/media.go"
    click DCPool "https://github.com/iyear/tdl/blob/master/core/dcpool/dcpool.go"
    click ChatMgmt "https://github.com/iyear/tdl/tree/master/app/chat"
    click DLOps "https://github.com/iyear/tdl/blob/master/app/dl/dl.go"
    click UPOps "https://github.com/iyear/tdl/blob/master/app/up/up.go"
    click FWDOps "https://github.com/iyear/tdl/blob/master/app/forward/forward.go"
    click LoginHandler "https://github.com/iyear/tdl/blob/master/app/login/login.go"
    click MigrationTools "https://github.com/iyear/tdl/blob/master/app/migrate/migrate.go"
    click ExtSystem "https://github.com/iyear/tdl/blob/master/app/extension/extension.go"
    click ExprEngine "https://github.com/iyear/tdl/blob/master/pkg/texpr/expr.go"
    click KeyMgmt "https://github.com/iyear/tdl/blob/master/pkg/key/key.go"
    click KVStore "https://github.com/iyear/tdl/blob/master/pkg/kv/kv.go"
    click Progress "https://github.com/iyear/tdl/blob/master/pkg/prog/prog.go"
    click TplFunc "https://github.com/iyear/tdl/blob/master/pkg/tplfunc/func.go"
    click PathMgmt "https://github.com/iyear/tdl/blob/master/pkg/tpath/tpath.go"
    click Commands "https://github.com/iyear/tdl/tree/master/cmd"
    click RootCmd "https://github.com/iyear/tdl/blob/master/cmd/root.go"
    click DLCmd "https://github.com/iyear/tdl/blob/master/cmd/dl.go"
    click UPCmd "https://github.com/iyear/tdl/blob/master/cmd/up.go"
    click FWDCmd "https://github.com/iyear/tdl/blob/master/cmd/forward.go"
    click LoginCmd "https://github.com/iyear/tdl/blob/master/cmd/login.go"
    
    %% Legend
    classDef legend fill:#f9f9f9,stroke:#333,stroke-width:1px
    subgraph Legend
        UI["User Interface"]:::ui
        APP["Application Layer"]:::app
        CORE["Core Services"]:::core
        PKG["Package Layer"]:::pkg
        EXT["External Services"]:::ext
    end
```
