fastify-file-interceptor
Advanced tools
Comparing version 1.0.4 to 1.0.5
{ | ||
"name": "fastify-file-interceptor", | ||
"version": "1.0.4", | ||
"version": "1.0.5", | ||
"main": "dist/index.js", | ||
@@ -5,0 +5,0 @@ "types": "dist/index.d.ts", |
110
README.MD
**Installation** | ||
```npm install fastify-file-interceptor``` | ||
`npm install fastify-file-interceptor` | ||
</br> | ||
```yarn add fastify-file-interceptor``` | ||
`yarn add fastify-file-interceptor` | ||
</br> | ||
@@ -10,3 +10,4 @@ **FileFastifyInterceptor() may not be compatible with third party cloud providers like Google Firebase or others.** | ||
>main.ts | ||
> main.ts | ||
```javascript | ||
@@ -32,6 +33,4 @@ import { contentParser } from 'fastify-multer'; | ||
> app.controller.ts | ||
**Example | ||
NOTE diskStorage you can import from "multer" or "fastify-file-interceptor" is the same** | ||
>app.controller.ts | ||
```typescript | ||
@@ -45,6 +44,6 @@ import { | ||
UseInterceptors, | ||
} from '@nestjs/common'; | ||
import { ApiConsumes, ApiTags } from '@nestjs/swagger'; | ||
import { diskStorage } from 'multer'; | ||
import { AppService } from './app.service'; | ||
} from "@nestjs/common"; | ||
import { ApiConsumes, ApiTags } from "@nestjs/swagger"; | ||
import { diskStorage } from "multer"; | ||
import { AppService } from "./app.service"; | ||
import { | ||
@@ -55,4 +54,4 @@ MultipleFileDto, | ||
FieldsFileDto, | ||
} from './dto/re-export-dto'; | ||
import { editFileName, imageFileFilter } from './utils/file-upload-util'; | ||
} from "./dto/re-export-dto"; | ||
import { editFileName, imageFileFilter } from "./utils/file-upload-util"; | ||
import { | ||
@@ -63,22 +62,22 @@ AnyFilesFastifyInterceptor, | ||
FilesFastifyInterceptor, | ||
} from 'fastify-file-interceptor'; | ||
} from "fastify-file-interceptor"; | ||
@Controller() | ||
@ApiTags('Upload File ') | ||
@ApiTags("Upload File ") | ||
export class AppController { | ||
constructor(private readonly appService: AppService) {} | ||
@ApiConsumes('multipart/form-data') | ||
@Post('single-file') | ||
@ApiConsumes("multipart/form-data") | ||
@Post("single-file") | ||
@UseInterceptors( | ||
FileFastifyInterceptor('photo_url', { | ||
FileFastifyInterceptor("photo_url", { | ||
storage: diskStorage({ | ||
destination: './upload/single', | ||
destination: "./upload/single", | ||
filename: editFileName, | ||
}), | ||
fileFilter: imageFileFilter, | ||
}), | ||
}) | ||
) | ||
single( | ||
@UploadedFile() file: Express.Multer.File, | ||
@Body() body: SingleFileDto, | ||
@Body() body: SingleFileDto | ||
) { | ||
@@ -89,16 +88,16 @@ console.log({ ...body, photo_url: file }); | ||
@ApiConsumes('multipart/form-data') | ||
@Post('multiple-file') | ||
@ApiConsumes("multipart/form-data") | ||
@Post("multiple-file") | ||
@UseInterceptors( | ||
FilesFastifyInterceptor('photo_url', 10, { | ||
FilesFastifyInterceptor("photo_url", 10, { | ||
storage: diskStorage({ | ||
destination: './upload/multiple', | ||
destination: "./upload/multiple", | ||
filename: editFileName, | ||
}), | ||
fileFilter: imageFileFilter, | ||
}), | ||
}) | ||
) | ||
multiple( | ||
@UploadedFiles() files: Express.Multer.File[], | ||
@Body() body: MultipleFileDto, | ||
@Body() body: MultipleFileDto | ||
) { | ||
@@ -109,16 +108,16 @@ console.log({ ...body, photo_url: files }); | ||
@ApiConsumes('multipart/form-data') | ||
@Post('any-file') | ||
@ApiConsumes("multipart/form-data") | ||
@Post("any-file") | ||
@UseInterceptors( | ||
AnyFilesFastifyInterceptor({ | ||
storage: diskStorage({ | ||
destination: './upload/any', | ||
destination: "./upload/any", | ||
filename: editFileName, | ||
}), | ||
fileFilter: imageFileFilter, | ||
}), | ||
}) | ||
) | ||
anyFile( | ||
@UploadedFiles() files: Express.Multer.File, | ||
@Body() body: AnyFileDto, | ||
@Body() body: AnyFileDto | ||
) { | ||
@@ -129,4 +128,4 @@ console.log({ ...body, photo_url: files }); | ||
@ApiConsumes('multipart/form-data') | ||
@Post('fields-file') | ||
@ApiConsumes("multipart/form-data") | ||
@Post("fields-file") | ||
@UseInterceptors( | ||
@@ -136,7 +135,7 @@ FileFieldsFastifyInterceptor( | ||
{ | ||
name: 'photo_url', | ||
name: "photo_url", | ||
maxCount: 10, | ||
}, | ||
{ | ||
name: 'images', | ||
name: "images", | ||
maxCount: 10, | ||
@@ -147,8 +146,8 @@ }, | ||
storage: diskStorage({ | ||
destination: './upload/fields', | ||
destination: "./upload/fields", | ||
filename: editFileName, | ||
}), | ||
fileFilter: imageFileFilter, | ||
}, | ||
), | ||
} | ||
) | ||
) | ||
@@ -160,13 +159,14 @@ fields(@UploadedFiles() { photo_url, images }, @Body() body: FieldsFileDto) { | ||
} | ||
``` | ||
``` | ||
**NOTE property destination inside distStorage is the location in our project directory where we want to store the image** | ||
</br> | ||
> -Why we using FastifyAdatper then define interface Request from "express" instead interface FastifyRequest from "fastify" ? because function fileFiler and filename inside diskStorage use Request interface Express | ||
>file-upload-util.ts | ||
> -Why we using FastifyAdatper then define interface Request from "express" instead interface FastifyRequest from "fastify" ? because function fileFiler and filename inside diskStorage use Request interface Express | ||
> file-upload-util.ts | ||
```javascript | ||
import { Request } from 'express'; | ||
import { extname } from 'path'; | ||
import { Request } from "express"; | ||
import { extname } from "path"; | ||
@@ -178,3 +178,3 @@ export const editFileName = ( | ||
) => { | ||
const name = file.originalname.split('.')[0]; | ||
const name = file.originalname.split(".")[0]; | ||
const fileExtName = extname(file.originalname); | ||
@@ -190,3 +190,3 @@ callback(null, `${name}${fileExtName}`); | ||
if (!file.originalname.match(/\.(jpg|jpeg|png|gif)$/)) { | ||
return callback(new Error('Only image files are allowed!'), false); | ||
return callback(new Error("Only image files are allowed!"), false); | ||
} | ||
@@ -196,7 +196,10 @@ callback(null, true); | ||
``` | ||
**If you want to create as CDN from for image and display in browser look at this** | ||
**This page we use interface FastifyRequest instead of interface Request Express because out application use FastifyAdapter and request object is fastify not express** | ||
>file-mapper.ts | ||
**This page we use interface FastifyRequest instead of interface Request Express because out application use FastifyAdapter and request object is fastify not express** | ||
> file-mapper.ts | ||
```javascript | ||
import { FastifyRequest } from 'fastify'; | ||
import { FastifyRequest } from "fastify"; | ||
@@ -233,5 +236,7 @@ interface FileMapper { | ||
``` | ||
**Usage** | ||
```typescript | ||
"fastify-file-interceptor" | ||
"fastify-file-interceptor"; | ||
import { | ||
@@ -242,4 +247,5 @@ AnyFilesFastifyInterceptor, | ||
FilesFastifyInterceptor, | ||
} from 'fastify-file-interceptor'; | ||
} from "fastify-file-interceptor"; | ||
``` | ||
```typescript | ||
@@ -250,3 +256,3 @@ "fastify-file-interceptor" using same as "@nestjs/platform-express" a little different with some interface more document on Nest official page | ||
```typescript | ||
"@nestjs/platform-express" | ||
"@nestjs/platform-express"; | ||
import { | ||
@@ -253,0 +259,0 @@ AnyFilesInterceptor, |
252
44058