<reponame>fantastas/file-upload
import {
  Controller,
  Get,
  Param,
  Post,
  Res,
  UploadedFile,
  UploadedFiles,
  UseInterceptors,
} from '@nestjs/common';
import { FileInterceptor, FilesInterceptor } from '@nestjs/platform-express';
import { AppService } from './app.service';

@Controller()
export class AppController {
  constructor(private readonly appService: AppService) {}

  @Get()
  async getFiles(@Res() res) {
    const files = await this.appService.sgetAllFilePaths(res);
    return files;
  }

  // upload a single file
  @Post('files')
  @UseInterceptors(FileInterceptor('file'))
  async uploadFile(@UploadedFile() file: Express.Multer.File, @Res() res) {
    res.json(file);
  }
  // upload multiple files
  @Post('upload')
  @UseInterceptors(FilesInterceptor('files'))
  async uploadFiles(
    @UploadedFiles() files: Array<Express.Multer.File>,
    @Res() res,
  ) {
    res.json(files);
  }

  @Get(':filePath')
  async getFile(@Param('filePath') path: string, @Res() res) {
    return res.sendFile(path, { root: './uploaded' });
  }
}
