Skip to content

Latest commit

 

History

History
346 lines (258 loc) · 9.24 KB

File metadata and controls

346 lines (258 loc) · 9.24 KB

image

File Server

Status Node.js License Version

A simple yet powerful file sharing web application built with Node.js and Express. Upload files through a modern web interface and share them instantly with others.

image

🚀 Features

  • Drag & Drop File Upload: Modern, intuitive file upload interface
  • Instant File Sharing: Upload and share files immediately
  • File Management: View and download all uploaded files
  • Database Logging: Optional MySQL/MariaDB integration for upload tracking
  • CORS Enabled: Cross-origin requests supported
  • Responsive Design: Works on desktop and mobile devices
  • Real-time Updates: File list refreshes automatically after uploads
  • Has Auth for Security: check if login first then only allowed in page
  • Recovery: just in case if you for got your password

🛠️ Technology Stack

  • Backend: Node.js, Express.js
  • Database: MySQL/MariaDB (optional)
  • File Handling: Multer middleware
  • Frontend: HTML5, CSS3, JavaScript (ES6+)
  • Storage: Local filesystem

Note

New update!! v1.2.7: New recovery mode!!!

📦 Installation

Prerequisites

  • Node.js (version 14 or higher)
  • npm or yarn
  • MySQL/MariaDB (optional, for database logging)

Quick Start

  1. Clone the repository:

    git clone https://github.com/ZayanMuhammed/fileshare.git
    cd fileshare
  2. Install dependencies:

    npm install
  3. Create uploads directory:

    mkdir uploads
  4. Start the server:

    npm start
  5. Open your browser and navigate to:

    http://localhost:3000/fileshare.htm
    

Tip

If you are lazy like me just use the setup.sh file

🗄️ Database Setup (Optional)

If you want to enable database logging of file uploads:

Install MariaDB/MySQL

For Debian/Ubuntu:

sudo apt update
sudo apt install mariadb-server mariadb-client

For Arch Linux:

sudo pacman -Syu
sudo pacman -S mariadb

Start and Configure Database

  1. Start MariaDB service:

    sudo systemctl start mariadb
    sudo systemctl enable mariadb
  2. Secure installation:

    sudo mysql_secure_installation
  3. Create database and table:

    mariadb -u root -p
    CREATE DATABASE fileshare;
    USE fileshare;
    
    CREATE TABLE files (
        id INT AUTO_INCREMENT PRIMARY KEY,
        filePath VARCHAR(255) NOT NULL,
        originalname VARCHAR(255) NOT NULL,
        mimetype VARCHAR(100) NOT NULL,
        size BIGINT NOT NULL,
        uploaded_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
    );
    
    -- Create a user for the application
    CREATE USER 'fileshare_user'@'localhost' IDENTIFIED BY 'your_password';
    GRANT ALL PRIVILEGES ON fileshare.* TO 'fileshare_user'@'localhost';
    FLUSH PRIVILEGES;
  4. Update database configuration in server.js:

    const db = mysql.createPool({
        host: 'localhost',
        user: 'fileshare_user',     // your mariadb user
        password: 'your_password',   // your mariadb password
        database: 'fileshare',       // your mariadb database
        acquireTimeout: 60000,
        timeout: 60000,
        reconnect: true
    });

🚀 Usage

If you want a login splash that can also start the server, use the login module described below.

Uploading Files

  1. Open the web interface at http://localhost:3000/fileshare.htm
  2. Click on the upload area or drag and drop files
  3. Select your file(s)
  4. Click the "Upload" button
  5. Files will be uploaded to the uploads/ directory

Downloading Files

  • All uploaded files are automatically listed on the main page
  • Click on any filename to download the file
  • Files are served statically from the /uploads endpoint

API Endpoints

The server provides the following REST API endpoints:

Upload File

POST /upload
Content-Type: multipart/form-data

Form field: server (file)

List Files

GET /files

Response:

["file1.txt", "file2.jpg", "document.pdf"]

Download File

GET /uploads/{filename}

📁 Project Structure

fileshare/
├── .env                    # Credentials and vars
├── input.css
├── output.css          # tailwind css file
├── tailwind.config.js  #tailwind config
├── auth-failed.htm    # auth fail page
├── warning.htm        # warning page for log out
├── auth-failed.css    # style for fail page
├── server.js          # Main server file
├── fileshare.htm           # Main web interface
├── fileshare.css           # Styles for the interface
├── setup.sh                # for lazy devs like me 
├── client.js             # Frontend JavaScript
├── install-mariadb.sh      # Database installation script
├── package.json            # Node.js dependencies
├── uploads/                # Directory for uploaded files
├── assets/                 # Static assets (favicon, etc.)
├── login/                  # Login gateway
│   ├── login.htm           # Login page
│   ├── login.css           # Login styles
│   ├── login.js            # Login client logic
|   ├── showPassword.htm    # forgot password show here
│   └── README.md           # This module's docs
└── README.md              # Root documentation

🔐 Login Module

image

A minimal login gateway is available under login/

  • Install and run:
    cd login
    npm install
    node startweb.js
  • Open http://localhost:8080/login.htm
  • Default demo credentials: admin / Password123 (change in login/login.js)

Important: /run-script executes a shell script. Keep it for local/dev. For production, use a proper auth flow and a process manager (pm2/systemd) instead of executing shell from HTTP.

⚙️ Configuration

Server Configuration

Edit server.js to modify:

  • Port: Change const port = 3000; to your preferred port
  • Upload Directory: Modify the destination in multer configuration
  • File Size Limits: Add size limits to multer configuration
  • CORS Settings: Modify CORS headers as needed

Frontend Customization

  • Styling: Edit fileshare.css for visual customization
  • Behavior: Modify sql-data.js for frontend functionality
  • Interface: Update fileshare.htm for layout changes

🔧 Advanced Configuration

Adding File Size Limits

const upload = multer({ 
    storage: storage,
    limits: {
        fileSize: 10 * 1024 * 1024, // 10MB limit
    }
});

Adding File Type Restrictions

const upload = multer({ 
    storage: storage,
    fileFilter: (req, file, cb) => {
        const allowedTypes = ['image/jpeg', 'image/png', 'application/pdf'];
        if (allowedTypes.includes(file.mimetype)) {
            cb(null, true);
        } else {
            cb(new Error('Invalid file type'), false);
        }
    }
});

Environment Variables

Important

Note that make a .env file to configure to your liking!

PORT=3000
DB_HOST=localhost
DB_USER=fileshare_user
DB_PASSWORD=your_password
DB=db_name
DISABLE_RECOVERY=false

🤝 Contributing

  1. Fork the 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

📝 License

This project is licensed under the MIT License. See the LICENSE file for details.

👤 Author

Zayan Muhammed

🙏 Acknowledgments

  • Express.js team for the excellent web framework
  • Multer developers for file upload handling
  • The Node.js community for continuous support

📊 Project Status

This project is actively maintained. Feel free to report issues or suggest improvements!


Caution

This application is intended for development and testing purposes and local use. For >production use, please consider:

  • Implementing file type validation
  • Adding virus scanning for uploads
  • Setting up proper HTTPS
  • Configuring proper file storage and backup
  • Adding rate limiting and other security measures