Skip to content

Create a File Content Management Feature Inside Folders #17

@abhishek-nexgen-dev

Description

@abhishek-nexgen-dev

📖 Description

This feature allows users to create and manage content files inside folders.
Each file will have:

  • A title (file name)

  • A body (text, markdown, or code)

  • An extension like .txt, .md, .js, etc.

  • A folder where the file lives

This can be used for:

  • Writing notes in folders

  • Code or document editors

  • Markdown documentation

  • Personal knowledge bases

  • Project/task descriptions

It should be class-based, reusable, and easy to plug into any app (just like FastKit style).

🧠 Why It’s Useful

  • 🔥 Every app today needs a place to save user content (notes, docs, code, etc.)

  • 🗂️ Works perfectly with the folder module

  • ♻️ Can be reused in editors, file managers, dashboards, CMSs, and more

  • 🧩 Can store markdown, JSON, HTML, or any plain text

  • ✅ Keeps logic, controllers, and validations separated and easy to test

🧱 File Structure

src/
└── features/
    └── File/
        └── v1/
            ├── File.controller.ts     # Handles requests
            ├── File.service.ts        # Business logic
            ├── File.validators.ts     # Joi/Zod validation
            ├── File.model.ts          # File schema/interface
            ├── File.constant.ts       # Error/success messages
            ├── File.middleware.ts     # (Optional) access control
            ├── File.demo.ts           # Example usage
            └── README.md              # How to use this module

📘 File Model (File.model.ts)

interface File {
  id: string;
  title: string;
  content: string;
  extension: string; // e.g. .md, .txt, .json, .js
  folderId: string;  // Reference to Folder
  authId: string;    // Owner of file
  createdAt: Date;
  updatedAt: Date;
}

✅ Features

📝 CRUD

Create file in a folder

Get files by folderId

Get single file by ID

Update content/title/extension

Delete file (soft or hard)


🧩 Additional Features

  • ✍️ Supports .md, .txt, .json, .js, etc.

  • 🔐 AuthId check: user can only manage their files

  • 🔄 Optional: file versioning

  • 🗑️ Soft delete and restore support

  • 🧠 Link file to folder (folderId required)

🧪 Validations (File.validators.ts)

  • title: required, min 2 characters

  • content: optional (can be empty)

  • extension: must be .md, .txt, .js, .json, etc.

  • folderId: required (must be valid)

🎮 File Controller (File.controller.ts)

export class FileController {
  async create(req, res) {
    const data = { ...req.body, authId: req.authId };
    const file = await FileService.createFile(data);
    return SendResponse.success(res, { file });
  }

  async getByFolder(req, res) {
    const files = await FileService.getFilesByFolder(req.params.folderId, req.authId);
    return SendResponse.success(res, { files });
  }

  async getSingle(req, res) {
    const file = await FileService.getFileById(req.params.id, req.authId);
    return SendResponse.success(res, { file });
  }

  async update(req, res) {
    await FileService.updateFile(req.params.id, req.body, req.authId);
    return SendResponse.success(res, { message: 'File updated' });
  }

  async delete(req, res) {
    await FileService.deleteFile(req.params.id, req.authId);
    return SendResponse.success(res, { message: 'File deleted' });
  }
}

⚙️ File Service (File.service.ts)

class FileService {
  static async createFile(data) { ... }
  static async getFilesByFolder(folderId, authId) { ... }
  static async getFileById(fileId, authId) { ... }
  static async updateFile(fileId, update, authId) { ... }
  static async deleteFile(fileId, authId) { ... }
}

🔐 Middleware (Optional)

  • canAccessFile: check file ownership by authId

  • validateFileInput: zod/joi-based request validation (User can pass validation data)

🚀 API Routes Example

router.post('/files', verifyToken, fileController.create);
router.get('/folders/:folderId/files', verifyToken, fileController.getByFolder);
router.get('/files/:id', verifyToken, fileController.getSingle);
router.patch('/files/:id', verifyToken, fileController.update);
router.delete('/files/:id', verifyToken, fileController.delete);

🧾 Demo Request

{
  "title": "Welcome Note",
  "content": "# Hello\nWelcome to your workspace.",
  "extension": ".md",
  "folderId": "abc123"
}

✨ Final Outcome

[x] Users can create/edit/view files inside folders

[x] All data is cleanly validated

[x] Works perfectly with FastKit’s folder module

[x] Modular — plug into any project instantly

[x] Add-on ready: file history, diff, export, comments


🙋‍♂️ Future Add-ons

  • 📄 File versioning (save on every update)

  • 🧾 File comments (collaborate like Google Docs)

  • 🔄 Export file as .md, .pdf, or .txt

  • 🌐 Share file with others by link or email

  • 🧠 AI summarize file (later with OpenAI/LLM)

  • 🧮 File word count, read time, tags

✅ Final File System Usage Flow

// Get folders
GET /folders

// Create a new file inside folder
POST /files
{ title, content, extension, folderId }

// Get all files inside a folder
GET /folders/:folderId/files

// Update file
PATCH /files/:id
{ title?, content?, extension? }

// Delete file
DELETE /files/:id

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions