Skip to content

A lightweight and serverless CDN utilizing MEGA for file storage and delivery.

Notifications You must be signed in to change notification settings

IRON-M4N/MegaCDN

Repository files navigation

MegaCDN

A lightweight and serverless CDN utilizing MEGA for file storage and delivery.

Features

  • File upload and delivery via MEGA storage
  • Multiple account support (load balancing)
  • Multiple Files and folder Upload Support
  • Cool and Professional UI
  • Optional upload-protection using Authorization Header
  • Optional auto-deletion with configurable time periods
  • Supports both MongoDB and Json
  • Serverless deployment ready

Installation

Clone the Repository

git clone https://github.com/IRON-M4N/MegaCDN.git
cd MegaCDN
npm install

Deployments

Deploy To Heroku

  1. If You Don't Have a Heroku Account, Create An Account.
  2. Now Deploy To Heroku.
    Heroku

Deploy on Vercel

  1. Create Vercel Account If You Don't Have.
  2. Deploy on Vercel.
    Vercel

Deploy To Railway

  1. If You Don't Have Railway Account, Create First.
  2. Now Deploy The Repo.
    Railway

Deploy on Render

  1. Create Render Account If You Don't Have Any.
  2. Deploy Now.
    Render

Deploy on Koyeb

  1. Create Account If You Don't Have.
  2. Now Deploy.
    Koyeb

Configuration

Modify config.js or use environment variables. Example .env file:

PORT=3000                           # Port to run the app
MEGA_ACCOUNT=email:pass;email:pass  # Multiple accounts for load balancing
TEMP=memory                         # Upload storage option
AUTO_DELETE=true                    # Enable/disable auto-deletion
DELETE_TIME=1440                    # Minutes until deletion (default: 1 day)
MONGODB_URI=null                    # Optional MongoDB connection string
AUTHORIZATION=true                  # Enable/disable secure uploading
AUTH_TOKEN=YOUR_BEARER_TOKEN        # Bearer token for authentication
MAX_FILES=10                        # Maximum files per upload
MAX_FILE_SIZE=50                    # Maximum file size in MB
CACHE_TTL=3600                      # Cache duration in seconds
MAX_REQUESTS=100                    # Max upload requests in specific time
RATE_LIMIT=1 minute                 # 100 req per minute

Note: If you change AUTH_TOKEN then update it in public/script.js at line 189

Starting the Server

Using PM2 for process management:

npm start

To stop or restart:

npm stop
npm restart

Uploading Files

Send a POST request to /upload with a multipart form containing a file.

Curl - Single Mode
# Single file without Authorization
 curl -X POST -F "[email protected]" -F "mode=single" http://yourdomain.com/upload

# Multiple Files With Authorizatoin
 curl -X POST  -H "Authorization: Bearer YOUR_BEARER_TOKEN"  -F "[email protected]" -F "[email protected]" -F "mode=single" http://yourdomain.com/upload
Curl - Dual Mode
# Single file without Authorization
curl -X POST -F "[email protected]" -F "mode=dual" -F "[email protected]" http://yourdomain.com/upload

# Multiple Files With Authorizatoin
curl -X POST -H "Authorization: Bearer YOUR_BEARER_TOKEN"  -F "[email protected]" -F "[email protected]" -F "[email protected]" -F "mode=dual" -F "[email protected]" http://yourdomain.com/upload
Node.js - Single Mode
// Single file without Authorization
const fs = require("fs");
const axios = require("axios");
const FormData = require("form-data");

async function uploadSingle() {
  const form = new FormData();
  form.append("file", fs.createReadStream("image.jpg"));
  form.append("mode", "single");

  const res = await axios.post("http://yourdomain.com/upload", form, {
    headers: form.getHeaders(),
  });

  console.log(res.data);
}

uploadSingle();

// Multiple Files with Authorization
async function uploadMultiple() {
  const form = new FormData();

  form.append("file", fs.createReadStream("image1.jpg"));
  form.append("file", fs.createReadStream("image2.png"));

  form.append("mode", "single");

  const headers = {
    ...form.getHeaders(),
    Authorization: "Bearer YOUR_BEARER_TOKEN",
  };

  try {
    const res = await axios.post("http://yourdomain.com/upload", form, { headers });
    console.log("Upload successful:", res.data);
  } catch (error) {
    console.error("Upload failed:", error.response?.data || error.message);
  }
}

uploadMultiple();
Node.js - Dual Mode
// Single file without Authorization
const fs = require("fs");
const axios = require("axios");
const FormData = require("form-data");

async function uploadDual() {
  const form = new FormData();
  form.append("file", fs.createReadStream("image.jpg"));
  form.append("mode", "dual");
  form.append("email", "[email protected]");

  const res = await axios.post("http://yourdomain.com/upload", form, {
    headers: form.getHeaders(),
  });

  console.log(res.data);
}

uploadDual();

// Multiple Files with Authorization
async function uploadMultiple() {
  const form = new FormData();

  form.append("file", fs.createReadStream("image1.jpg"));
  form.append("file", fs.createReadStream("image2.png"));

  form.append("mode", "dual");
  form.append("email", "[email protected]");

  const headers = {
    ...form.getHeaders(),
    Authorization: "Bearer YOUR_BEARER_TOKEN",
  };

  try {
    const res = await axios.post("http://yourdomain.com/upload", form, { headers });
    console.log("Upload successful:", res.data);
  } catch (error) {
    console.error("Upload failed:", error.response?.data || error.message);
  }
}

uploadMultiple();

Parameters Explained:

Parameter Description
file The file to upload.
mode Upload mode: single (default) or dual (requires email).
email (Optional) Required if mode=dual, specifies which account to use.

Response Example

{
  "success": true,
  "files": [
    {
      "url": "https://your_domain.com/media/your_file",
      "name": "your_file.png",
      "size": 51470,
      "formattedSize": "50.26 KB",
      "expires": "86400 sec",
      "formattedExpires": "1 day"
    }
  ]
}

To-Do

  • Add custom file name
  • Proper logging (error and alerts)

Contributing

  1. Fork the repository
  2. Create a new branch (feature-web)
  3. Commit your changes
  4. Open a pull request

© IRON-M4N

About

A lightweight and serverless CDN utilizing MEGA for file storage and delivery.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published