Skip to content

Commit

Permalink
Added right file extensions and names on upload
Browse files Browse the repository at this point in the history
  • Loading branch information
TannerGabriel committed Jul 25, 2019
1 parent af010bb commit 7066695
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
1 change: 1 addition & 0 deletions nest-file-uploading/.gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# compiled output
/dist
/node_modules
/files

# Logs
logs
Expand Down
Binary file not shown.
18 changes: 15 additions & 3 deletions nest-file-uploading/src/app.controller.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Controller, Get, Post, UseInterceptors, UploadedFile, UploadedFiles, Res, Param } from '@nestjs/common';
import { AppService } from './app.service';
import { FilesInterceptor } from '@nestjs/platform-express';
import { FileInterceptor } from '@nestjs/platform-express';
import { diskStorage } from 'multer';
import { extname } from 'path';

@Controller()
export class AppController {
Expand All @@ -12,9 +14,19 @@ export class AppController {
}

@Post()
@UseInterceptors(FilesInterceptor('image'))
@UseInterceptors(FileInterceptor('image', {
storage: diskStorage({
destination: './files'
, filename: (req, file, cb) => {
const name = file.originalname.split('.')[0];
const fileExtName = extname(file.originalname);
const randomName = Array(4).fill(null).map(() => (Math.round(Math.random() * 16)).toString(16)).join('');
cb(null, `${name}-${randomName}${fileExtName}`);
},
}),
}))
UploadedFile(@UploadedFiles() file) {
console.log(file);
// console.log(file);
}

@Get(':imgpath')
Expand Down

0 comments on commit 7066695

Please sign in to comment.