Skip to main content

File Upload & Storage

Multer Configuration

Local Storage (temporary):

// config/multer.js
const storage = multer.diskStorage({
  destination: (req, file, cb) => {
    cb(null, req.tempFolder);  // Dynamic temp folder
  },
  filename: (req, file, cb) => {
    cb(null, `${Date.now()}-${file.originalname}`);
  }
});

Minio Object Storage

Configuration (config/minio.js):

const minioClient = new Minio.Client({
  endPoint: process.env.MINIO_ENDPOINT,
  port: parseInt(process.env.MINIO_PORT),
  useSSL: process.env.MINIO_USE_SSL === 'true',
  accessKey: process.env.MINIO_ACCESS_KEY,
  secretKey: process.env.MINIO_SECRET_KEY
});

Upload Flow:

  1. File uploaded to temp directory via Multer
  2. Image processed/compressed with Sharp
  3. File uploaded to Minio
  4. Temp file deleted
  5. Minio URL stored in database

Image Processing

const sharp = require('sharp');

// Compress and resize
await sharp(inputPath)
  .resize(800, 800, { fit: 'inside' })
  .jpeg({ quality: 80 })
  .toFile(outputPath);