file-uploader-npm
v1.0.4
Published
A lightweight, plug-and-play file upload utility for Node.js that supports: - ποΈ Local Disk Upload - βοΈ AWS S3 Upload - π· Cloudinary Upload
Readme
π file-upload-handler
A lightweight, plug-and-play file upload utility for Node.js that supports:
- ποΈ Local Disk Upload
- βοΈ AWS S3 Upload
- π· Cloudinary Upload
Includes powerful features like:
- β File type & size validation
- β Async/await interface
- β Easy-to-configure options
π¦ Installation
npm install file-upload-handler
π Quick Start
import { handleUpload } from 'file-upload-handler';
// Example: Upload to local storage
const result = await handleUpload(file, {
provider: 'local',
allowedTypes: ['image/jpeg', 'image/png'],
maxSizeMB: 2,
});
console.log(result);π Supported Providers
1. ποΈ Local Upload
await handleUpload(req.file, {
provider: 'local',
allowedTypes: ['image/jpeg'],
maxSizeMB: 2
});Returns:
{
"url": "/uploads/image.jpg",
"path": "uploads/image.jpg"
}2. βοΈ S3 Upload
await handleUpload(req.file, {
provider: 's3',
s3Config: {
key: process.env.AWS_ACCESS_KEY_ID,
secret: process.env.AWS_SECRET_ACCESS_KEY,
region: 'us-east-1',
bucket: 'your-bucket-name'
}
});Returns:
{
"url": "https://your-bucket.s3.amazonaws.com/image.jpg"
}3. π· Cloudinary Upload
await handleUpload(req.file, {
provider: 'cloudinary',
cloudinaryConfig: {
cloud_name: process.env.CLOUDINARY_CLOUD_NAME,
api_key: process.env.CLOUDINARY_API_KEY,
api_secret: process.env.CLOUDINARY_API_SECRET
}
});Returns:
{
"url": "https://res.cloudinary.com/your-cloud/image/upload/v12345/image.jpg",
"public_id": "image"
}π Validation Options
You can pass these optional validation settings:
Option Type Description
allowedTypes Array MIME types allowed (e.g. image/jpeg)
maxSizeMB Number Maximum file size in megabytes
π§ͺ Example (Express.js)
import express from 'express';
import multer from 'multer';
import { handleUpload } from 'file-upload-handler';
const app = express();
const upload = multer(); // in-memory
app.post('/upload', upload.single('file'), async (req, res) => {
try {
const result = await handleUpload(req.file, {
provider: 'cloudinary',
cloudinaryConfig: {
cloud_name: 'your-cloud',
api_key: 'your-api-key',
api_secret: 'your-api-secret'
},
allowedTypes: ['image/png', 'image/jpeg'],
maxSizeMB: 5,
});
res.json(result);
} catch (err) {
res.status(400).json({ error: err.message });
}
});
