# DKI Download

Ứng dụng web quản lý quyền TMS và tải raw files từ Synology NAS.

## Tech Stack

- **Frontend**: React 19 + TypeScript + TailwindCSS + Vite
- **Backend Python**: FastAPI + Python 3.12 (xử lý download)
- **Backend TypeScript**: Express + Node.js (xử lý TMS permission)
- **Database**: MongoDB (metadata) + Supabase PostgreSQL (queue, history, sessions)
- **Storage**: Synology NAS (FileStation API + Sharing Link với Selenium)
- **Download**: Aria2 (download accelerator)

## Features

### 🔐 Cấp quyền TMS

- Cấp quyền hàng loạt (usernames × GE IDs)
- Sử dụng TMS REST API trực tiếp
- Queue management với drag-and-drop ưu tiên
- Real-time status tracking qua Supabase Realtime

### 📥 Tải Raw Files

- **API Mode**: Tải trực tiếp qua NAS FileStation API
- **Sharing Link Mode**: Tải qua Synology sharing link với Selenium
- Browse NAS directories với file browser
- Concurrent download queue (max 10 parallel)
- Smart file type detection và icons

### ✅ Check Upload

- Kiểm tra trạng thái upload của chapters
- Lọc theo status: pending, found, not found, error

### 🔧 Custom Paths

- Lưu và quản lý custom folder paths cho sharing link
- Quick jump đến folder thường dùng

## Quick Start

### Prerequisites

- Node.js 18+ và npm
- Python 3.12+
- MongoDB access
- Supabase project

### Installation

```powershell
# Clone và cài đặt dependencies
git clone <repository-url>
cd dkiDownload
npm install

# Setup Python backend
python -m venv .venv
.\.venv\Scripts\Activate.ps1
pip install -r backend/requirements.txt
```

### Development

```powershell
# Chạy cả 3 services (frontend + 2 backends)
npm run dev

# Hoặc chạy riêng:
npm run dev:frontend          # Frontend (port 5173)
npm run dev:backend-python    # Python backend (port 8000)
npm run dev:backend-typescript # TypeScript backend (port 3002)
```

**URLs:**

- Frontend: http://localhost:5173
- Python API: http://localhost:8000
- TypeScript API: http://localhost:3002

## Environment Variables

Tạo file `.env.local`:

```bash
# MongoDB
MONGODB_URI=mongodb+srv://...
MONGODB_DATABASE=schedule

# Supabase
SUPABASE_URL=https://....supabase.co
SUPABASE_SERVICE_ROLE_KEY=eyJ...

# NAS
NAS_BASE_URL=https://disk.lezhin.com:5001/webapi
NAS_DSM_URL=https://disk.lezhin.com:5001
NAS_USERNAME=geupload2
NAS_PASSWORD=***
NAS_DESTINATION_PATH=\\172.16.14.240\raw

# Chrome profile cho Selenium
NAS_CHROME_PROFILE_PATH=chrome_profile_nas

# TMS
TMS_API_URL=https://tms.kiledel.com
TMS_EMAIL=***
TMS_PASSWORD=***

# Optional
DRIVER_ADMIN_TOKEN=***
```

## Project Structure

```
dkiDownload/
├── App.tsx                      # Main React component
├── index.tsx                    # React entry point
├── types.ts                     # TypeScript definitions
│
├── components/                  # React components (41 files)
│   ├── SubmissionForm.tsx       # TMS permission form
│   ├── SubmissionHistory.tsx    # Permission history
│   ├── RawDownloadForm.tsx      # Raw download form
│   ├── DownloadHistory.tsx      # Download history
│   ├── CheckPage.tsx            # Check upload page
│   └── ...
│
├── hooks/                       # React hooks
│   ├── use-tab-visibility.ts    # Tab visibility tracking
│   └── index.ts
│
├── utils/                       # Utilities
│   ├── supabase.ts              # Supabase client
│   ├── use-realtime-downloads.ts
│   ├── use-realtime-submissions.ts
│   └── sort-utils.ts
│
├── src/                         # TypeScript backend (Express)
│   ├── server.ts                # Express server entry
│   ├── config.ts                # Configuration
│   ├── api/
│   │   └── submissions.ts       # Submissions API
│   └── services/
│       ├── auth.service.ts      # TMS authentication
│       ├── tms-api.service.ts   # TMS API calls
│       ├── supabase.service.ts  # Supabase operations
│       └── worker.service.ts    # Permission worker
│
├── backend/                     # Python backend (FastAPI)
│   ├── main.py                  # FastAPI app
│   ├── worker.py                # TMS permission worker
│   ├── worker_downloads.py      # Download worker
│   ├── routes/
│   │   ├── tms_routes.py        # TMS endpoints
│   │   ├── raw_api_routes.py    # API download endpoints
│   │   ├── raw_sharing_routes.py # Sharing link endpoints
│   │   ├── downloads_routes.py  # Download management
│   │   └── custom_paths_routes.py
│   └── services/
│       ├── mongodb_service.py
│       ├── supabase_service.py
│       ├── nas_service.py
│       ├── downloads_service.py
│       ├── nas_api/             # NAS API package
│       ├── nas_sharing_api/     # Selenium sharing package
│       └── aria2/               # Aria2 client
│
└── aria2/                       # Aria2 download tool
    └── aria2c.exe
```

## Database Schema (Supabase)

| Table          | Mô tả                                |
| -------------- | ------------------------------------ |
| `sessions`     | Auth sessions (TMS, NAS FileStation) |
| `submissions`  | TMS permission submissions           |
| `downloads`    | File download queue & history        |
| `custom_paths` | Custom folder paths cho sharing      |
| `check_list`   | Check upload requests                |
| `chapters`     | Chapter management                   |
| `users`        | User list for autocomplete           |

## Architecture

```
┌─────────────────┐     ┌──────────────────┐     ┌─────────────────┐
│   React App     │────▶│  Python Backend  │────▶│  Synology NAS   │
│   (Vite)        │     │  (FastAPI:8000)  │     │  (FileStation)  │
└────────┬────────┘     └────────┬─────────┘     └─────────────────┘
         │                       │
         │              ┌────────▼─────────┐
         │              │  Download Worker │
         │              │  (Aria2 + API)   │
         │              └──────────────────┘
         │
         │              ┌──────────────────┐     ┌─────────────────┐
         └─────────────▶│   TS Backend     │────▶│    TMS API      │
                        │  (Express:3002)  │     │  (REST API)     │
                        └────────┬─────────┘     └─────────────────┘
                                 │
                        ┌────────▼─────────┐
                        │ Permission Worker│
                        └──────────────────┘
                                 │
         ┌───────────────────────┴───────────────────────┐
         │                   Supabase                     │
         │  (Realtime subscriptions + PostgreSQL)        │
         └───────────────────────────────────────────────┘
```

## Key API Endpoints

### TMS Permission

- `POST /api/tms/submit` - Submit permission request
- `GET /api/tms/submissions` - Get submission history
- `DELETE /api/tms/submissions/:id` - Delete submission

### Raw Download (API Mode)

- `POST /api/raw-files/list` - List files from NAS
- `POST /api/raw-files/download` - Create download job

### Raw Download (Sharing Link)

- `POST /api/sharing-link/get-from-db` - Get sharing link from MongoDB
- `POST /api/sharing-link/process` - Process sharing link
- `POST /api/sharing-link/download` - Download from sharing link

### Download Management

- `GET /api/downloads` - Get download queue/history
- `DELETE /api/downloads/:id` - Cancel/delete download

### Check Upload

- `POST /api/check/submit` - Submit check request
- `GET /api/check/history` - Get check history

## License

Internal tool for Lezhin Entertainment.
