返回 AiToEarn
oss.controller.ts
根目录 / project / aitoearn-electron / server / src / lib / oss / oss.controller.ts
1 /*
2 * @Author: nevin
3 * @Date: 2022-03-07 13:37:06
4 * @LastEditors: nevin
5 * @LastEditTime: 2024-12-22 21:58:14
6 * @Description: 文件上传
7 */
8 import {
9 Controller,
10 Post,
11 UploadedFile,
12 UploadedFiles,
13 UseInterceptors,
14 Headers,
15 Body,
16 } from '@nestjs/common';
17 import { FileInterceptor, FilesInterceptor } from '@nestjs/platform-express';
18 import { OssService } from './oss.service';
19 import { ApiBody, ApiConsumes, ApiOperation, ApiTags } from '@nestjs/swagger';
20 import { Public } from '../../auth/auth.guard';
21
22 @ApiTags('OSS - 文件(阿里云)')
23 @Controller('oss')
24 export class OssController {
25 constructor(private readonly ossService: OssService) {}
26
27 @ApiOperation({ description: '存入临时目录', summary: '上传文件' })
28 @ApiConsumes('multipart/form-data')
29 @ApiBody({
30 schema: {
31 type: 'object',
32 properties: {
33 file: {
34 type: 'string',
35 format: 'binary',
36 },
37 },
38 },
39 })
40 @Public()
41 @Post('upload')
42 @UseInterceptors(FileInterceptor('file'))
43 async uploadFile(
44 @UploadedFile() file: Express.Multer.File,
45 @Headers() headers: any,
46 ) {
47 const secondPath: string = headers['second-path'];
48 return await this.ossService.upFileStream(file, secondPath);
49 }
50
51 @ApiOperation({ description: '不存入临时目录', summary: '上传文件' })
52 @Public()
53 @Post('upload/permanent')
54 @UseInterceptors(FileInterceptor('file'))
55 async uploadPermanentFile(
56 @UploadedFile() file: Express.Multer.File,
57 @Headers() headers: any,
58 ) {
59 const secondPath: string = headers['second-path'];
60
61 // 不开启临时目录
62 return await this.ossService.upFileStream(file, secondPath, null, true);
63 }
64
65 @ApiOperation({ description: '存入临时目录', summary: '上传文件数组' })
66 @Public()
67 @Post('upload/list')
68 @UseInterceptors(FilesInterceptor('files'))
69 uploadFileList(
70 @UploadedFiles() files: Express.Multer.File[],
71 @Headers() headers: any,
72 ) {
73 const secondPath: string = headers['second-path'];
74 files.forEach((file) => {
75 this.ossService.upFileStream(file, secondPath);
76 });
77 return { status: 'ok' };
78 }
79
80 @ApiOperation({ description: '上传URL图片', summary: '上传URL图片' })
81 @Public()
82 @Post('upload/url')
83 async uploadFileOfUrl(@Body() body: { path: string; url: string }) {
84 const res = await this.ossService.upFileByUrl(body.url, {
85 path: body.path,
86 });
87 return res;
88 }
89 }
90
90 lines TYPESCRIPT