npm package: @romandronov/nest-files
Description: NestJS Files Module
Author: Roman Dronov
Latest version: 1.2.0
Deprecated: Package no longer supported. Contact Support at https://www.npmjs.com/support for more info.

# @romandronov/nest-files

A NestJS module for file operations with support for different storage providers, currently featuring AWS S3 integration.

## Description

This package provides a flexible and extensible file management system for NestJS applications. It uses an adapter pattern to support different storage providers, with AWS S3 implementation included out of the box.

## Features

- Abstract file operations interface
- AWS S3 storage implementation
- Upload, download, and delete file operations
- Configurable through NestJS module system
- TypeScript support with full type definitions

## Installation

```bash
# npm
npm install @romandronov/nest-files @aws-sdk/client-s3

# yarn
yarn add @romandronov/nest-files @aws-sdk/client-s3

# pnpm
pnpm add @romandronov/nest-files @aws-sdk/client-s3
```

## Usage

### Basic Usage with S3

```typescript
import { Module } from '@nestjs/common';
import { FilesModule } from '@romandronov/nest-files';
import { S3Adapter, S3ModuleConfig } from '@romandronov/nest-files';

@Module({
  imports: [
    FilesModule.register({
      adapter: new S3Adapter({
        region: 'us-east-1',
        credentials: {
          accessKeyId: 'YOUR_ACCESS_KEY',
          secretAccessKey: 'YOUR_SECRET_KEY',
        },
      }),
    }),
  ],
})
export class AppModule {}
```

### Using Environment Variables

```typescript
import { Module } from '@nestjs/common';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { FilesModule } from '@romandronov/nest-files';
import { S3Adapter } from '@romandronov/nest-files';

@Module({
  imports: [
    ConfigModule.forRoot(),
    FilesModule.registerAsync({
      imports: [ConfigModule],
      inject: [ConfigService],
      useFactory: (configService: ConfigService) => ({
        adapter: new S3Adapter({
          region: configService.get('AWS_REGION'),
          credentials: {
            accessKeyId: configService.get('AWS_ACCESS_KEY_ID'),
            secretAccessKey: configService.get('AWS_SECRET_ACCESS_KEY'),
          },
        }),
      }),
    }),
  ],
})
export class AppModule {}
```

### Using the FilesService

```typescript
import { Injectable } from '@nestjs/common';
import { FilesService } from '@romandronov/nest-files';

@Injectable()
export class YourService {
  constructor(private readonly filesService: FilesService) {}

  async uploadFile(file: Buffer, bucketName: string, fileName: string) {
    return this.filesService.uploadFile(file, bucketName, fileName);
  }

  async downloadFile(bucketName: string, fileName: string) {
    return this.filesService.downloadFile(bucketName, fileName);
  }

  async deleteFile(bucketName: string, fileName: string) {
    return this.filesService.deleteFile(bucketName, fileName);
  }
}
```

## API Reference

### FilesService

The `FilesService` provides the following methods:

#### uploadFile(file: Buffer, destination: string, name: string): Promise<{ success: boolean }>

Uploads a file to the specified destination with the given name.

- `file`: The file content as a Buffer
- `destination`: The destination (bucket name for S3)
- `name`: The file name (key for S3)
- Returns: A promise that resolves to an object with a success boolean

#### downloadFile(destination: string, name: string): Promise<{ success: boolean, file: Buffer | null }>

Downloads a file from the specified destination with the given name.

- `destination`: The destination (bucket name for S3)
- `name`: The file name (key for S3)
- Returns: A promise that resolves to an object with a success boolean and the file content as a Buffer (or null if not found)

#### deleteFile(destination: string, name: string): Promise<{ success: boolean }>

Deletes a file from the specified destination with the given name.

- `destination`: The destination (bucket name for S3)
- `name`: The file name (key for S3)
- Returns: A promise that resolves to an object with a success boolean

### S3Adapter

The `S3Adapter` accepts all configuration options from the AWS SDK's `S3ClientConfig` interface.

## Creating Custom Adapters

You can create custom adapters by extending the `FilesAdapter` abstract class:

```typescript
import { FilesAdapter, FilesService } from '@romandronov/nest-files';

// Define your configuration type
export interface CustomStorageConfig {
  apiKey: string;
  // other config options
}

// Create your custom service implementation
export class CustomStorageService extends FilesService {
  // Implement the required methods
}

// Create your custom adapter
export class CustomStorageAdapter extends FilesAdapter<CustomStorageConfig> {
  createFilesService(): FilesService {
    // Initialize your storage client with this.config
    const client = new CustomStorageClient(this.config);

    // Return your custom service implementation
    return new CustomStorageService(client);
  }
}
```

## License

MIT