fastify-static icon indicating copy to clipboard operation
fastify-static copied to clipboard

abstract the plugin to provide support of virtual file system

Open climba03003 opened this issue 1 year ago • 2 comments

Prerequisites

  • [X] I have written a descriptive issue title
  • [X] I have searched existing issues to ensure the feature has not already been requested

🚀 Feature Proposal

I believe it is a good idea to provide an interface for file serving through different storage engine. For example,

  1. File System (which is currently)
  2. MongoDB GridFS
  3. S3 compatible interface

Motivation

Manage of Range header is hard and maybe problematic. Instead, we can provide a generic interface for the user to choose storage engine. Then, we provide all the others function.

It enables the user to highly customize how to serve file based on their application logic and provide a proper storage engine. Even file system serving can be customize in this case.

Example

import Fastify from 'fastify'
import FastifyStatic from '@fastify/static'
import FastifyMongoDB from '@fastify/mongodb'
import { MongoDBGridFS } from '@fastify/static/engine'

const fastify = Fastify()

fastify.register(FastifyMongoDB, {
  // proper configuration here
  // not to demo how to use mongodb
})
fastify.register(FastifyStatic, {
  engine: MongoDBGridFS,
  db: fastify.mongo.db,
  bucketName: 'vfs'
})

// you can serve the file like currently
// but it is backed by other engine

climba03003 avatar Feb 02 '24 09:02 climba03003

I would love this to happen, but first we should refactor this module and get a better send utility (fix https://github.com/fastify/send/issues/15 among other things).

Then, it would mean that we should just swap the internal "send" with "send-mongodb".

mcollina avatar Feb 02 '24 09:02 mcollina