DEV Community

Ali Hamza
Ali Hamza

Posted on

Day 70 of Learning MERN Stack

Hello Dev Community! 👋

It is officially Day 70 — completing exactly 10 full days of my intensive standalone practice sprint! Over the last 9 days, I rewrote user modules, parameterized routers, identity locks, and polymorphic forms. Today, on the final day of this consolidation sprint, I crushed a massive architecture obstacle: Implementing Sandboxed Asset Isolation using Dynamic Sub-directories in Multer!

When building real-world e-commerce or rental engines, dump-storing all incoming user uploads inside a single flat uploads/ directory is a major anti-pattern. It creates file-name collisions, security vulnerabilities, and an unmanageable disk infrastructure. Today, I programmed a dynamic folder generation engine to solve this permanently.


🧠 Key Learnings From Day 70 (Dynamic Storage Engines)

Configuring advanced multi-part data routing requires tapping directly into Multer's native disk storage hooks and runtime folder allocation:

1. Dynamic Directory Creation (fs.mkdirSync)

Instead of pointing to a static absolute path string, I customized Multer's destination method. The handler dynamically intercepts incoming multipart parameters, generates a unique crypto tracking token or random string identifier for the specific home/product, and issues an asynchronous check to create a dedicated sub-folder on the fly inside the uploads/ directory.

2. Eliminating File Collisions & Media Mess

As shown in my code review in "Screenshot (162).jpg", each independent house listing now owns its isolated sandboxed folder. If a user uploads banner.jpg for Product A and another uploads banner.jpg for Product B:

  • Product A Storage: uploads/random-string-a/banner.jpg
  • Product B Storage: uploads/random-string-b/banner.jpg Zero file overwrite issues, zero mess, and an instantly scalable folder architecture!

3. Preserving Database Paths

The core engine captures this dynamically computed relative path string (e.g., /uploads/unique-string/filename.jpg) and updates our Mongoose schema properties seamlessly, ensuring image lookups remain perfect on client detail sliders.


🛠️ A Conceptual Look at the Custom Storage Engine

Here is how I structured the dynamic runtime destination tracking using Node's filesystem modules:


javascript
const multer = require('multer');
const fs = require('fs');
const path = require('path');
const crypto = require('crypto');

const fileStorage = multer.diskStorage({
    destination: (req, file, cb) => {
        // Generating a random string per product request to prevent folder cluttering
        const productToken = crypto.randomBytes(8).toString('hex');
        const dir = path.join('uploads', productToken);

        // Enforcing dynamic creation if the sub-directory doesn't exist
        if (!fs.existsSync(dir)) {
            fs.mkdirSync(dir, { recursive: true });
        }
        cb(null, dir);
    },
    filename: (req, file, cb) => {
        const uniqueSuffix = Date.now() + '-' + Math.round(Math.random() * 1E9);
        cb(null, uniqueSuffix + '-' + file.originalname);
    }
});
Enter fullscreen mode Exit fullscreen mode

Top comments (0)