Home

Awesome

CloudFlare File Share

English简体中文

A simple file sharing tool running on Cloudflare Workers, supporting R2 and D1 dual storage solutions.

expample web | view

1.png

Features

Logic

Login process:
User access → Check cookies → No cookies → Display login page → Verify password → Set cookies → Enter home page
                      ↑
              Have cookies → Verify cookies → Enter home page

Upload process:
Select file → Check file size → >25MB → Use R2 storage
                              → ≤25MB → Select storage method → R2 or D1
              ↓
    Generate unique ID → Store file → Return the sharing link

Download process:
Access the sharing link → Parse the file ID → Determine storage location → Retrieve file → Return file content

Deployment Guide

Prerequisites

Step 1: Configure the Environment

  1. Clone the repository:

    git clone https://github.com/joyance-professional/cf-files-sharing
    cd cf-files-sharing
    
  2. Install dependencies:

    npm install
    
  3. Log in to Cloudflare:

    wrangler login
    

Step 2: Create Necessary Cloudflare Resources

  1. Create the R2 bucket:

    wrangler r2 bucket create file-share
    
  2. Create the D1 database:

    wrangler d1 create file-share
    
  3. Update the database ID in the wrangler.toml file:

    [[d1_databases]]
    binding = "DB"
    database_name = "file-share"
    database_id = "your-database-id" # obtained from the previous step
    

Step 3: Initialize the Database

Run the database migration:

wrangler d1 execute file-share --file=./migrations/init.sql --remote

[!NOTE] Ensure that you replace "your-database-id" in the wrangler.toml file with the actual database ID obtained from the previous steps. Also, remember to include the --remote flag when running database migrations to apply them to the remote D1 database.

Step 4: Configure Environment Variables

  1. Set the authentication password:

    wrangler secret put AUTH_PASSWORD
    

    When prompted, enter the password you want to set.

Step 5: Deploy

Deploy to Cloudflare Workers:

wrangler deploy

Usage Guide

Admin Access

  1. Access your Workers domain.
  2. Enter the set AUTH_PASSWORD to log in.
  3. The login status will remain for 30 days.

File Upload

  1. After logging in, select the file to upload.
  2. For files less than 25MB, you can choose the storage method (R2 or D1).
  3. Files larger than 25MB will automatically use R2 storage.
  4. Get the sharing link after the upload is complete.

File Sharing

Technical Details

Storage Mechanism

Database Structure

CREATE TABLE files (
    id TEXT PRIMARY KEY,
    filename TEXT NOT NULL,
    size INTEGER NOT NULL,
    storage_type TEXT NOT NULL,
    created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
    content BLOB NULL
);

Security Features

Configuration Options

Environment Variables

Variable NameDescriptionRequired
AUTH_PASSWORDAdmin interface login passwordYes

wrangler.toml Configuration

name = "file-share-worker"
main = "src/index.js"

[[r2_buckets]]
binding = "FILE_BUCKET"
bucket_name = "file-share"

[[d1_databases]]
binding = "DB"
database_name = "file-share"
database_id = "your-database-id"

Development Guide

Local Development

  1. After cloning the repository, run:

    wrangler dev
    
  2. Visit http://localhost:8787 for testing.

Code Structure

cf-files-sharing/
├── src/
│   ├── index.js        # Main entry file
│   ├── auth.js         # Authentication logic
│   ├── storage/
│   │   ├── r2.js       # R2 storage handling
│   │   ├── d1.js       # D1 storage handling
│   │   └── manager.js  # Storage manager
│   ├── utils/
│   │   ├── response.js # Response utilities
│   │   └── id.js       # File ID generator
│   └── html/
│       └── templates.js # HTML templates
├── wrangler.toml       # Cloudflare configuration
└── migrations/         # D1 database migrations
    └── init.sql

Contribution Guide

  1. Fork this repository.
  2. Create your feature branch (git checkout -b feature/AmazingFeature).
  3. Commit your changes (git commit -m 'Add some AmazingFeature').
  4. Push to the branch (git push origin feature/AmazingFeature).
  5. Open a Pull Request.

Credits

Feedback

If you find any issues or have suggestions for improvements, please create an issue.