Skip to content

StreamKITS/nestjs_module_factorydrive

Repository files navigation

Nest Logo

Factory drive module for NestJS framework

NPM Version Package License Publish Package to npmjs


NestJS Factory drive Module

Factory drive module for NestJS Framework

Install dependencies

yarn add @streamkits/nestjs_module_factorydrive

Instanciate

// app.module.ts
import { FactorydriveModule, FactorydriveService } from '@streamkits/nestjs_module_factorydrive'

@Module({
  imports: [
    // ...
    FactorydriveModule.forRootAsync({
      imports: [ConfigModule],
      inject: [ConfigService],
      useFactory: async (config: ConfigService) => ({
        ...config.get('factorydrive.options'),
      }),
    }),
    // ...
  ],
})
export class AppModule {
  public constructor(storage: FactorydriveService) {
    // If you want to add a new driver you can use the registerDriver method
    storage.registerDriver('s3', AwsS3Storage)
  }
}

//config.ts
export default {
  // ...
  factorydrive: {
    options: {
      default: 'local',
      disks: {
        local: {
          driver: 'local',
          config: {
            root: process.cwd() + '/storage',
          },
        },
        s3: {
          driver: 's3',
          config: {
            credentials: {
              accessKeyId: '******',
              secretAccessKey: '******',
            },
            endpoint: 'http:https://minio:9000/',
            region: 'us-east-1',
            bucket: 'example',
            forcePathStyle: true,
          },
        },
      },
    },
  },
  // ...
}

Usage

// filestorage.service.ts
import { FactorydriveService } from '@streamkits/nestjs_module_factorydrive'

@Injectable()
export class FileStorageService {
  public constructor(
    @InjectFactorydrive() private readonly factorydrive: FactorydriveService,
  ) {}

  public async uploadFile(path: string, file: Express.Multer.File): Promise<string> {
    const res = await this.factorydrive.getDisk('s3').put(path, file.buffer)
    return res.raw
  }

  public async deleteFile(path: string): Promise<void> {
    await this.factorydrive.getDisk('s3').delete(path)
  }

  public async moveFile(path: string, target: string): Promise<void> {
    await this.factorydrive.getDisk('s3').delete(path, target)
  }

  public async copyFile(path: string, target: string): Promise<void> {
    await this.factorydrive.getDisk('s3').copy(path, target)
  }

  public async listFiles(path: string): Promise<void> {
    await this.factorydrive.getDisk('s3').flatList(path)
  }
}