1.0.1 • Published 8 months ago

multermate-es v1.0.1

Weekly downloads
-
License
MIT
Repository
github
Last release
8 months ago

Multer Mate

A robust and flexible file upload utility built on top of Multer, providing advanced file handling capabilities for Node.js applications.

Features

  • 📁 Flexible file storage configuration
  • 🔒 Built-in file type validation
  • 📦 Single and multiple file uploads
  • 🎯 Field-specific file type restrictions
  • 🗑️ File deletion utility
  • ⚡ Configurable file size limits
  • 🎨 Custom MIME type support
  • 🔄 Unique file naming with UUID
  • 🛡️ Path sanitization
  • 📝 Comprehensive error handling

Installation

npm install multermate

Basic Usage

import { uploadSingle, uploadMultiple, deleteFile } from "multermate-es";

Upload Configurations

Single File Upload

// Basic single file upload
app.post("/upload", uploadSingle(), (req, res) => {
  res.json({ file: req.file });
});

// Advanced single file upload
app.post(
  "/upload/advanced",
  uploadSingle({
    destination: "uploads/images",
    filename: "profile",
    fileTypes: ["images"],
    fileSizeLimit: 5 * 1024 * 1024, // 5MB
    preservePath: false,
  }),
  (req, res) => {
    res.json({ file: req.file });
  }
);

Multiple Files Upload

// Multiple fields with different configurations
app.post(
  "/upload/multiple",
  uploadMultiple({
    fields: [
      {
        name: "avatar",
        maxCount: 1,
        fileTypes: ["images"],
      },
      {
        name: "documents",
        maxCount: 5,
        fileTypes: ["pdfs"],
      },
      {
        name: "media",
        maxCount: 3,
        fileTypes: ["images", "videos"],
      },
    ],
    destination: "uploads/mixed",
    fileSizeLimit: 10 * 1024 * 1024, // 10MB per file
  }),
  (req, res) => {
    res.json({ files: req.files });
  }
);

Custom MIME Types

app.post(
  "/upload/custom",
  uploadSingle({
    destination: "uploads/custom",
    customMimeTypes: [
      "application/vnd.ms-excel",
      "application/json",
      "text/csv",
    ],
    fileSizeLimit: 1024 * 1024, // 1MB
  })
);

File Deletion

// Simple file deletion
app.delete("/files/:filename", async (req, res) => {
  const isDeleted = await deleteFile(`uploads/${req.params.filename}`);
  res.json({ success: isDeleted });
});

// Advanced file deletion with error handling
app.delete("/files/:type/:filename", async (req, res) => {
  try {
    const filePath = path.join("uploads", req.params.type, req.params.filename);
    const isDeleted = await deleteFile(filePath);

    if (isDeleted) {
      res.json({
        success: true,
        message: "File deleted successfully",
      });
    } else {
      res.status(404).json({
        success: false,
        message: "File not found or unable to delete",
      });
    }
  } catch (error) {
    res.status(500).json({
      success: false,
      message: error.message,
    });
  }
});

API Reference

uploadSingle(options)

Configures single file upload with the following options:

OptionTypeDefaultDescription
destinationstring'uploads'Upload directory path
filenamestring'file'Form field name
fileTypesstring[]'all'Allowed file types
customMimeTypesstring[][]Custom MIME types
fileSizeLimitnumber50MBMax file size in bytes
preservePathbooleanfalsePreserve original path

uploadMultiple(options)

Configures multiple file uploads with the following options:

OptionTypeDefaultDescription
fieldsField[][]Field configurations
destinationstring'uploads'Upload directory
customMimeTypesstring[][]Custom MIME types
fileSizeLimitnumber50MBMax file size
preservePathbooleanfalsePreserve paths

Field Configuration

OptionTypeDefaultDescription
namestring-Field name (required)
maxCountnumber10Max files per field
fileTypesstring[]'all'Allowed types

deleteFile(filePath)

Deletes a file from the filesystem:

ParameterTypeDescription
filePathstringPath to file
ReturnsPromiseDeletion success

Supported File Types

const ALLOWED_MIME_TYPES = {
  images: ["image/jpeg", "image/jpg", "image/png", "image/gif"],
  videos: ["video/mp4", "video/mpeg", "video/ogg", "video/webm", "video/avi"],
  pdfs: ["application/pdf"],
  all: [
    /* combination of all above types */
  ],
};

Error Handling

app.post("/upload", uploadSingle(), (req, res) => {
  try {
    // File size validation
    if (req.fileValidationError) {
      return res.status(400).json({
        error: req.fileValidationError,
      });
    }

    // File existence check
    if (!req.file) {
      return res.status(400).json({
        error: "No file uploaded",
      });
    }

    // Success response
    res.json({
      success: true,
      file: {
        filename: req.file.filename,
        path: req.file.path,
        size: req.file.size,
        mimetype: req.file.mimetype,
      },
    });
  } catch (error) {
    res.status(500).json({
      error: error.message,
    });
  }
});

Best Practices

  1. Always implement proper error handling
  2. Set appropriate file size limits
  3. Validate file types on the server
  4. Use custom storage destinations for different file types
  5. Implement file cleanup mechanisms
  6. Consider implementing file type verification beyond MIME types

License

MIT

Contributing

Contributions are welcome! Please feel free to submit issues and pull requests.

Author

Your Name

Support

For support, please open an issue in the GitHub repository.

1.0.1

8 months ago

1.0.0

8 months ago