Last active
July 17, 2024 11:22
-
-
Save alexwhin/ed13f34d0bae0449ddd9fb7e82cfeacd to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { diskStorage } from "multer"; | |
import { FileInterceptor } from "@nestjs/platform-express"; | |
import { ApiResponse, ApiConsumes, ApiBody } from "@nestjs/swagger"; | |
import { | |
Res, | |
Get, | |
Post, | |
Param, | |
Controller, | |
UploadedFile, | |
UseInterceptors, | |
} from "@nestjs/common"; | |
@Controller("image") | |
export class ImageController { | |
@Get(":name") | |
@ApiResponse({ type: Buffer }) | |
async serveAvatar(@Param("name") name, @Res() response): Promise<any> { | |
response.sendFile(name); | |
} | |
@Post() | |
@UseInterceptors( | |
FileInterceptor("file", { | |
storage: diskStorage({ | |
filename: (_request, file, callback) => | |
callback(null, `${new Date().getTime()}-${file.originalname}`), | |
}), | |
}), | |
) | |
@ApiBody({ | |
required: true, | |
type: "multipart/form-data", | |
schema: { | |
type: "object", | |
properties: { | |
file: { | |
type: "string", | |
format: "binary", | |
}, | |
}, | |
}, | |
}) | |
@ApiConsumes("multipart/form-data") | |
async post(@UploadedFile() file): Promise<void> { | |
console.log(file); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
or smth like this
and then
this way you don't have the file in the dto, but swagger knows about it