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
    No fields configured for Feature.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions