Skip to content

devnguyen0111/CRN-cdn

Repository files navigation

CDN Server với Giao Diện Quản Lý

CDN Server hoàn chỉnh với giao diện admin để quản lý file, ảnh, và media. Hỗ trợ MongoDB để lưu trữ metadata và có thể triển khai độc lập trên Proxmox.

Tính năng

🎯 Quản Lý File

  • ✅ Giao diện admin để upload và quản lý files
  • ✅ Lưu trữ metadata trong MongoDB (name, ext, hash, size, category, tags, description)
  • ✅ Hash SHA256 cho mỗi file (unique, tự động loại bỏ trùng lặp)
  • ✅ Drag & drop upload
  • ✅ Clipboard paste upload (Ctrl+V để paste ảnh trực tiếp)
  • ✅ Bulk operations (chọn nhiều file và xóa cùng lúc)
  • ✅ CDN URL với hash để truy xuất file

🎨 Gallery Công Khai

  • ✅ Trang gallery công khai để xem tất cả files
  • ✅ Phân trang (20 files/trang)
  • ✅ Lọc theo category và tags
  • ✅ Tìm kiếm theo tên, mô tả, tags
  • ✅ Lightbox viewer cho ảnh và video (fullscreen, keyboard navigation)
  • ✅ Copy URL trực tiếp
  • ✅ Mobile responsive

🏷️ Categories & Tags

  • ✅ 6 categories được định nghĩa sẵn (Hình ảnh, Video, Tài liệu, Audio, Archive, Khác)
  • ✅ 20 tags phổ biến (design, marketing, product, banner, logo, screenshot, etc.)
  • ✅ Dropdown select cho category
  • ✅ Autocomplete cho tags với datalist
  • ✅ Có thể thêm tags mới tùy ý

👥 User Management

  • ✅ Hệ thống đăng nhập admin
  • ✅ Quản lý nhiều users với roles (admin/user/viewer)
  • ✅ CRUD operations cho users
  • ✅ Session management

🚀 Triển Khai

  • ✅ Docker & Docker Compose support
  • ✅ Sẵn sàng cho production trên Proxmox
  • ✅ Helmet security middleware
  • ✅ CORS configuration

Cài đặt

1. Cài đặt Dependencies

npm install

2. Tạo mật khẩu Admin

node -e "console.log(require('bcryptjs').hashSync('your-password', 10))"

Copy hash và cập nhật vào file .env:

ADMIN_USERNAME=admin
ADMIN_PASSWORD=$2a$10$...YourHashHere...

3. Chạy MongoDB

Cách 1: Docker

docker run -d -p 27017:27017 --name mongodb mongo:7

Cách 2: Local MongoDB Cài đặt MongoDB trên máy local

4. Khởi chạy Server

Development:

npm run dev

Production:

npm start

Server sẽ chạy tại: http://localhost:3000

Triển khai với Docker

Sử dụng Docker Compose

# Build và khởi chạy
docker-compose up -d

# Xem logs
docker-compose logs -f

# Dừng
docker-compose down

Sử dụng

Trang Chính

Gallery (Trang chủ): http://localhost:3000/gallery

  • Xem tất cả files đã upload
  • Lọc theo category/tags
  • Tìm kiếm files
  • Click vào ảnh/video để xem fullscreen với lightbox

Admin Dashboard

  1. Truy cập: http://localhost:3000/admin/login
  2. Đăng nhập với username/password đã cấu hình
  3. Upload files từ dashboard:
    • Drag & drop file
    • Click để chọn file
    • Ctrl+V để paste ảnh từ clipboard
  4. Chọn category và nhập tags
  5. Bulk operations:
    • Chọn nhiều files bằng checkbox
    • Click "Xóa đã chọn" để xóa hàng loạt
  6. Quản lý users tại /users

CDN URLs

  • Xem file: http://localhost:3000/cdn/{hash}
  • Gallery: http://localhost:3000/gallery
  • Admin: http://localhost:3000/admin/dashboard
  • Users: http://localhost:3000/users (admin only)

API Endpoints

File Model

{
  name: String,          // Tên file gốc
  ext: String,           // Phần mở rộng (.jpg, .png, ...)
  hash: String,          // SHA256 hash (unique, indexed)
  size: Number,          // Kích thước file (bytes)
  path: String,          // Đường dẫn file trên disk
  mimetype: String,      // MIME type
  category: String,      // Category (Hình ảnh, Video, Tài liệu, etc.)
  tags: [String],        // Array of tags
  description: String,   // Mô tả file
  uploaded_by: ObjectId, // Reference to User
  created_at: Date,      // Ngày tạo
  access_at: Date,       // Lần truy cập cuối
  lookup: Virtual        // Virtual field: /cdn/{hash}
}

User Model

{
  username: String,      // Unique username
  password: String,      // Bcrypt hashed password
  email: String,         // Email
  role: String,          // 'admin', 'user', or 'viewer'
  created_at: Date       // Ngày tạo
}

Categories & Tags Mặc Định

Categories (6)

  • Hình ảnh
  • Video
  • Tài liệu
  • Audio
  • Archive
  • Khác

Tags (20)

  • design, marketing, product, banner, logo
  • screenshot, presentation, report, tutorial, demo
  • music, background, template, icon, font
  • backup, project, client, internal, publicGET /gallery` - Trang gallery công khai
  • Query params: ?page=1&category=...&tag=...&search=...

CDN Routes (Public)

  • GET /cdn/:hash - Truy xuất file

User Routes (Admin only)

  • GET /users - Giao diện quản lý users
  • POST /users - Tạo user mới
  • PUT /users/:id - Cập nhật user
  • DELETE /users/:id - Xóa user

Database Schema

{
  name: String,          // Tên file gốc
  ext: String,           // Phần mở rộng (.jpg, .png, ...)
  hash: String,          // SHA256 hash (unique)
  size: Number,          // Kích thước file (bytes)
  path: String,          // Đường dẫn file trên disk
  mimetype: String,      // MIME type
  download_count: Number,// Số lượt download
  downlo├── File.js           # File metadata schema
      └── User.js           # User authentication schema
   ├── routes/
      ├── admin.js          # Admin dashboard & upload
      ├── cdn.js            # File serving
      ├── gallery.js        # Public gallery
      └── users.js          # User management
   ├── middleware/
      └── auth.js           # Authentication middleware
   ├── utils/
      ├── hash.js           # SHA256 hashing
      └── seedCategories.js # Predefined categories & tags
   ├── views/
      ├── login.ejs         # Admin login page
      ├── dashboard.ejs     # Admin dashboard
      ├── gallery.ejs       # Public gallery with lightbox
      └── users.ejs         # User management interface
   └── server.js             # Main application entry
├── uploads/                  # Uploaded files storage
├── .env                      # Environment variables
├── package.json
├── Dockerfile
├── docker-compose.yml
└── README.md

Công Nghệ Sử Dụng

  • Backend: Node.js + Express.js 4.18.2
  • Database: MongoDB + Mongoose 7.6.3
  • Template Engine: EJS 3.1.9
  • File Upload: Multer 1.4.5
  • Authentication: bcryptjs 2.4.3 + express-session 1.17.3
  • Security: Helmet 7.1.0 + CORS 2.8.5
  • Containerization: Docker + Docker Compose
  • Thay đổi SESSION_SECRET trong .env
  • Sử dụng mật khẩu mạnh và hash bcrypt
  • Cấu hình HTTPS khi deploy production
  • Giới hạn file size trong multer config
  • Cấu hình CORS cho phù hợp

Cấu trúc Project

cdn-server/
├── src/
│   ├── models/
│   │   └── File.js
│   ├── routes/
│   │   ├── admin.js
│   │   └── cdn.js
│   ├── middleware/
│   │   └── auth.js
│   ├── utils/
│   │   └── hash.js
│   ├── views/
│   │   ├── login.ejs
│   │   └── dashboard.ejs
│   └── server.js
├── uploads/
├── .env
├── package.json
├── Dockerfile
└── docker-compose.yml

License

MIT

About

CDN (Content Delivery Network)

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors