Skip to content

Commit

Permalink
修改自述文件
Browse files Browse the repository at this point in the history
  • Loading branch information
Dr-SummerFlower committed Apr 23, 2024
1 parent 1b9e035 commit 0077933
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 107 deletions.
24 changes: 24 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -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

75 changes: 0 additions & 75 deletions README.md

This file was deleted.

Binary file added demo.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# 使用官方 Node.js 16 版本的镜像作为基础镜像
FROM node:16
FROM node:18

# 设置工作目录,容器内所有后续命令都在这个目录中进行
WORKDIR /usr/src/app
Expand Down
23 changes: 0 additions & 23 deletions public/views/index.html

This file was deleted.

18 changes: 18 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
## 随机图片
> GET /v1/images
返回数据:

**type:Buffer**

![本地图片](./demo.jpg)

## 指定图片
> GET /v1/images/:id
返回数据:

**type:Buffer**

![本地图片](./demo.jpg)

9 changes: 8 additions & 1 deletion src/image/image.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<any> {
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<any> {
return new StreamableFile(await this.imageService.getImage(id));
return new StreamableFile(await this.imageService.getImageById(id));
}
}
21 changes: 14 additions & 7 deletions src/image/image.service.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -9,16 +10,22 @@ export class ImageService {
private readonly sequelize: Sequelize,
) {}

async getImage(id: IdDto): Promise<Buffer> {
async getImages(): Promise<any> {
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<any> {
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;
}
}

0 comments on commit 0077933

Please sign in to comment.