import { Body, Controller, Get, Param, Post, Query, Res, UploadedFile, UseInterceptors, } from '@nestjs/common'; import { FileInterceptor } from '@nestjs/platform-express'; import { AccessoryService } from './accessory/accessory.service'; import stream = require('stream'); import urlencode = require('urlencode'); import { ArticleService } from './article/article.service'; import { CommentService } from './comment/comment.service'; import { AddCommentByKeyDto } from './add-comment-by-key-dto'; import { ApiBody, ApiResponse, ApiTags } from '@nestjs/swagger'; import { ArticleSearchDto } from './article-search-dto'; @Controller('api/article') export class ArticleController { constructor( private accessoryService: AccessoryService, private commentService: CommentService, private articleService: ArticleService, ) {} @Get() getHome(): any { return 'article'; } @Post('upload') @UseInterceptors( FileInterceptor('upload', { limits: { fileSize: 2 * 1024 * 1024 } }), ) async uploadFile(@UploadedFile() file) { const accessory = await this.accessoryService.create({ originalFilename: file.originalname, uploadFile: file.buffer, uploadDate: new Date(), }); const ret = { id: accessory.id, uploaded: 1, url: '/api/article/down/' + accessory.id, }; return ret; } @Post('upload-file') @UseInterceptors( FileInterceptor('file', { limits: { fileSize: 2 * 1024 * 1024 } }), ) async uploadFile2(@UploadedFile() file) { const accessory = await this.accessoryService.create({ originalFilename: file.originalname, uploadFile: file.buffer, uploadDate: new Date(), }); const ret = { id: accessory.id, uploaded: 1, url: '/api/article/down/' + accessory.id, }; return ret; } @Get('down/:id') findOne(@Res() res, @Param() params) { const promise = this.accessoryService.findOneById(params.id); promise.then(function (accessory) { const filename = urlencode(accessory.originalFilename, 'utf8'); const readStream = new stream.PassThrough(); readStream.end(accessory.uploadFile); res.set('Content-disposition', 'attachment; filename=' + filename); res.set('Content-Type', 'text/plain'); readStream.pipe(res); }); } @Post('upload-avatar/:id') @UseInterceptors( FileInterceptor('file', { limits: { fileSize: 2 * 1024 * 1024 } }), ) async uploadFile3(@UploadedFile() file, @Param() params) { const article = await this.articleService.findOneById(params.id); const accessory = await this.accessoryService.create({ originalFilename: file.originalname, uploadFile: file.buffer, uploadDate: new Date(), }); const updateResult = await this.articleService.update(article, { avatar: accessory, }); const ret = { id: accessory.id, uploaded: 1, url: '/api/article/down/' + accessory.id, }; return ret; } @ApiBody({ description: '通用查询接口', type: ArticleSearchDto, }) @ApiTags('特别处理api') @Get('search') search(@Query() query: ArticleSearchDto) { const ret = this.articleService.findAllByParams(query); return ret; } @ApiBody({ description: '评论BY文章KEY推送接口', type: AddCommentByKeyDto, }) @ApiTags('特别处理api') @ApiResponse({ status: 201, description: 'The record has been successfully created.', }) @Post('comment/add') async addCommentByKey(@Body() body: AddCommentByKeyDto) { if (body['articleKey']) { const article = await this.articleService.findOneByKey(body.articleKey); const commentEntityDto = { key: body.key, content: body.content, publishTime: body.publishTime, article: { id: article.id }, }; const comment = await this.commentService.create(commentEntityDto); return comment; } else { return {}; } } }