This project provides a file management system where users can upload, download, list, and delete files. The system uses Express.js to handle API requests and Multer for file uploads. It integrates with a MySQL database to track uploaded files and their metadata, and supports functionalities like file listing, deletion, and fetching download tokens.
- File Upload: Upload files to user-specific directories.
- File Download: Download files by providing a valid token.
- File Listing: List files and directories in a user-defined folder.
- File Deletion: Delete files or entire directories.
- Token-based Authentication: Use unique tokens for secure file access.
- Backend: Node.js, Express.js
- File Handling: Multer
- Database: MySQL (for storing file metadata)
- File Storage: Local filesystem (in
uploads/directory) - Authentication: Token-based system
- Node.js (>=14.x.x)
- MySQL
- NPM or Yarn
git clone https://github.com/Equation-Tracker/custom_cloud.git
cd custom_cloudRun the following command to install the required dependencies:
npm installCreate a MySQL database and run the following SQL commands to set up the required tables.
CREATE DATABASE file_management;
USE file_management;
CREATE TABLE tokens (
id INT AUTO_INCREMENT PRIMARY KEY,
token VARCHAR(255) NOT NULL,
fileName VARCHAR(255) NOT NULL,
fullPath VARCHAR(255) NOT NULL,
createdAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);Edit the config.js file to configure your MySQL connection:
import mysql from "mysql2/promises";
export const pool = mysql.createPool({
host: "localhost",
user: "root",
password: "yourpassword",
database: "file_management",
});To start the server, run:
npm startThe server will be running on http://localhost:4000.
- URL:
POST /upload - Description: Upload a file to the server.
- Headers:
filePath: User-defined path to store the file.
- Body: Form-data with the file field (name
file).
- URL:
POST /download/:fileName - Description: Download a file by providing the file name and a valid token.
- Body: JSON with the token.
- URL:
POST /listFiles - Description: List all files in the root directory (
uploads/). - Body: None
- URL:
POST /listFiles/:dirName - Description: List files in a specific directory.
- Body: None
- URL:
POST /delete - Description: Delete a file or directory.
- Body: JSON with the path of the file or directory to be deleted.
- URL:
POST /getToken - Description: Retrieve a token to access a file.
- Body: JSON with the
pathof the file.
The frontend sends a POST request to /upload to upload a file. The file is uploaded using FormData and headers are sent to specify the path for storage.
The frontend lists the files from the server by sending a POST request to /listFiles. It dynamically renders the file structure and allows the user to navigate through directories and view file names.
To download a file, the frontend sends a POST request to /getToken with the file path to obtain a valid token. This token is then appended to the download URL for secure access.
The frontend provides an option to delete a file or directory. It sends a POST request to /delete with the path of the file or directory to be deleted.
|-- uploads/ # File storage directory
|-- config.js # Database configuration
|-- server.js # Main server file
|-- package.json # Project metadata
|-- README.md # Documentation
-
CORS Issues: If you're encountering CORS-related errors, make sure that the frontend is running on a different port than the backend and ensure the backend has the correct CORS settings.
-
Missing Files or Directories: Make sure that the
uploads/directory exists and that it has the appropriate permissions for file storage. -
Database Issues: Ensure that MySQL is running and the database schema is set up correctly. Check the database connection settings in
config.js.