diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml new file mode 100644 index 0000000..7726060 --- /dev/null +++ b/.github/workflows/ci.yaml @@ -0,0 +1,24 @@ +name: picture_api + +on: [push] + +jobs: + deploy: + runs-on: ubuntu-latest + steps: + - name: 检出代码 + uses: actions/checkout@v2 + - name: 设置Docker Buildx + uses: docker/setup-buildx-action@v1 + - name: 构建镜像 + run: | + docker build -t picture_api -f ./dockerfile . + - name: 导出镜像 + run: | + docker save picture_api | gzip > picture_api.tar.gz + - name: 上传镜像 + uses: actions/upload-artifact@v2 + with: + name: picture_api + path: picture_api.tar.gz + diff --git a/README.md b/README.md deleted file mode 100644 index 11d3f51..0000000 --- a/README.md +++ /dev/null @@ -1,75 +0,0 @@ -

- Nest Logo -

- -[circleci-image]: https://img.shields.io/circleci/build/github/nestjs/nest/master?token=abc123def456 - -[circleci-url]: https://circleci.com/gh/nestjs/nest - -

A progressive Node.js framework for building efficient and scalable server-side applications.

-

-NPM Version -Package License -NPM Downloads -CircleCI -Coverage -Discord -Backers on Open Collective -Sponsors on Open Collective - - Support us - -

- - -## Description - -[Nest](https://github.com/nestjs/nest) framework TypeScript starter repository. - -## Installation - -```bash -$ npm install -``` - -## Running the app - -```bash -# development -$ npm run start - -# watch mode -$ npm run start:dev - -# production mode -$ npm run start:prod -``` - -## Test - -```bash -# unit tests -$ npm run test - -# e2e tests -$ npm run test:e2e - -# test coverage -$ npm run test:cov -``` - -## Support - -Nest is an MIT-licensed open source project. It can grow thanks to the sponsors and support by the amazing backers. If -you'd like to join them, please [read more here](https://docs.nestjs.com/support). - -## Stay in touch - -- Author - [Kamil Myśliwiec](https://kamilmysliwiec.com) -- Website - [https://nestjs.com](https://nestjs.com/) -- Twitter - [@nestframework](https://twitter.com/nestframework) - -## License - -Nest is [MIT licensed](LICENSE). diff --git a/demo.jpg b/demo.jpg new file mode 100644 index 0000000..bf8e23a Binary files /dev/null and b/demo.jpg differ diff --git a/dockerfile b/dockerfile index 5b81c37..d703ed9 100644 --- a/dockerfile +++ b/dockerfile @@ -1,5 +1,5 @@ # 使用官方 Node.js 16 版本的镜像作为基础镜像 -FROM node:16 +FROM node:18 # 设置工作目录,容器内所有后续命令都在这个目录中进行 WORKDIR /usr/src/app diff --git a/public/views/index.html b/public/views/index.html deleted file mode 100644 index 9e5f991..0000000 --- a/public/views/index.html +++ /dev/null @@ -1,23 +0,0 @@ - - - - - - - 图片接口 - - - - -
-

图库中有 {{ picture.max }} 张图片

-
- - - - diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..5fa64ba --- /dev/null +++ b/readme.md @@ -0,0 +1,18 @@ +## 随机图片 +> GET /v1/images + +返回数据: + +**type:Buffer** + +![本地图片](./demo.jpg) + +## 指定图片 +> GET /v1/images/:id + +返回数据: + +**type:Buffer** + +![本地图片](./demo.jpg) + diff --git a/src/image/image.controller.ts b/src/image/image.controller.ts index 37c03eb..6d64bbc 100644 --- a/src/image/image.controller.ts +++ b/src/image/image.controller.ts @@ -14,11 +14,18 @@ import { IdPipe } from './image.pipe'; export class ImageController { constructor(private readonly imageService: ImageService) {} + @Get('/') + @Header('Content-Type', 'image/jpeg') + @Header('Content-Disposition', 'inline') + async getImages(): Promise { + return new StreamableFile(await this.imageService.getImages()); + } + @Get(':id') @UsePipes(IdPipe) @Header('Content-Type', 'image/jpeg') @Header('Content-Disposition', 'inline') async reqGetImage(@Param('id') id: IdDto): Promise { - return new StreamableFile(await this.imageService.getImage(id)); + return new StreamableFile(await this.imageService.getImageById(id)); } } diff --git a/src/image/image.service.ts b/src/image/image.service.ts index 9cb593d..080f9b2 100644 --- a/src/image/image.service.ts +++ b/src/image/image.service.ts @@ -1,6 +1,7 @@ -import { HttpException, HttpStatus, Inject, Injectable } from '@nestjs/common'; +import { Inject, Injectable } from '@nestjs/common'; import { Sequelize } from 'sequelize-typescript'; import { IdDto } from './dto/image.dto'; +import { Image } from './entities/image.entity'; @Injectable() export class ImageService { @@ -9,16 +10,22 @@ export class ImageService { private readonly sequelize: Sequelize, ) {} - async getImage(id: IdDto): Promise { + async getImages(): Promise { + const allImages: Image[] = + (await this.sequelize.models.Image.findAll()) as Image[]; + const randomIndex: number = Math.floor( + Math.random() * allImages.length, + ); + const randomImage: Image = allImages[randomIndex]; + return randomImage.image; + } + + async getImageById(id: IdDto): Promise { const image: any = await this.sequelize.models.Image.findOne({ where: { id, }, }); - if (image.dataValues.image) { - return image.dataValues.image; - } else { - throw new HttpException('图片未找到', HttpStatus.NOT_FOUND); - } + return image.image; } }