1.0.0 • Published 7 months ago

@streamkits/nestjs_module_factorydrive v1.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
7 months ago

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://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)
  }
}
1.0.0

7 months ago

0.0.7

8 months ago

0.0.6

8 months ago

0.0.5

8 months ago

0.0.4

8 months ago

0.0.3

8 months ago

0.0.2

8 months ago