Add file upload

This commit is contained in:
2023-07-23 16:15:41 +09:00
parent 04487c9b24
commit 95b08935cc
3 changed files with 54 additions and 0 deletions

19
back/package-lock.json generated
View File

@@ -38,6 +38,7 @@
"@nestjs/testing": "^8.0.0",
"@types/express": "^4.17.13",
"@types/jest": "27.4.1",
"@types/multer": "^1.4.7",
"@types/node": "^16.0.0",
"@types/passport-google-oauth20": "^2.0.11",
"@types/supertest": "^2.0.11",
@@ -1959,6 +1960,15 @@
"resolved": "https://registry.npmjs.org/@types/mime/-/mime-1.3.2.tgz",
"integrity": "sha512-YATxVxgRqNH6nHEIsvg6k2Boc1JHI9ZbH5iWFFv/MTkchz3b1ieGDa5T0a9RznNdI0KhVbdbWSN+KWWrQZRxTw=="
},
"node_modules/@types/multer": {
"version": "1.4.7",
"resolved": "https://registry.npmjs.org/@types/multer/-/multer-1.4.7.tgz",
"integrity": "sha512-/SNsDidUFCvqqcWDwxv2feww/yqhNeTRL5CVoL3jU4Goc4kKEL10T7Eye65ZqPNi4HRx8sAEX59pV1aEH7drNA==",
"dev": true,
"dependencies": {
"@types/express": "*"
}
},
"node_modules/@types/node": {
"version": "16.11.33",
"resolved": "https://registry.npmjs.org/@types/node/-/node-16.11.33.tgz",
@@ -10767,6 +10777,15 @@
"resolved": "https://registry.npmjs.org/@types/mime/-/mime-1.3.2.tgz",
"integrity": "sha512-YATxVxgRqNH6nHEIsvg6k2Boc1JHI9ZbH5iWFFv/MTkchz3b1ieGDa5T0a9RznNdI0KhVbdbWSN+KWWrQZRxTw=="
},
"@types/multer": {
"version": "1.4.7",
"resolved": "https://registry.npmjs.org/@types/multer/-/multer-1.4.7.tgz",
"integrity": "sha512-/SNsDidUFCvqqcWDwxv2feww/yqhNeTRL5CVoL3jU4Goc4kKEL10T7Eye65ZqPNi4HRx8sAEX59pV1aEH7drNA==",
"dev": true,
"requires": {
"@types/express": "*"
}
},
"@types/node": {
"version": "16.11.33",
"resolved": "https://registry.npmjs.org/@types/node/-/node-16.11.33.tgz",

View File

@@ -50,6 +50,7 @@
"@nestjs/testing": "^8.0.0",
"@types/express": "^4.17.13",
"@types/jest": "27.4.1",
"@types/multer": "^1.4.7",
"@types/node": "^16.0.0",
"@types/passport-google-oauth20": "^2.0.11",
"@types/supertest": "^2.0.11",

View File

@@ -13,6 +13,10 @@ import {
Patch,
NotFoundException,
Req,
UseInterceptors,
UploadedFile,
HttpStatus,
ParseFilePipeBuilder,
} from '@nestjs/common';
import { AuthService } from './auth.service';
import { JwtAuthGuard } from './jwt-auth.guard';
@@ -34,6 +38,8 @@ import { Setting } from 'src/models/setting';
import { UpdateSettingDto } from 'src/settings/dto/update-setting.dto';
import { SettingsService } from 'src/settings/settings.service';
import { AuthGuard } from '@nestjs/passport';
import { FileInterceptor } from '@nestjs/platform-express';
import { writeFile } from 'fs';
@ApiTags('auth')
@Controller('auth')
@@ -95,6 +101,34 @@ export class AuthController {
return await this.usersService.getProfilePicture(req.user.id);
}
@UseGuards(JwtAuthGuard)
@ApiBearerAuth()
@ApiOkResponse({ description: 'The user profile picture' })
@ApiUnauthorizedResponse({ description: 'Invalid token' })
@Post('me/picture')
@UseInterceptors(FileInterceptor('file'))
async postProfilePicture(
@Request() req: any,
@UploadedFile(
new ParseFilePipeBuilder()
.addFileTypeValidator({
fileType: 'jpeg',
})
.addMaxSizeValidator({
maxSize: 5000,
})
.build({
errorHttpStatusCode: HttpStatus.UNPROCESSABLE_ENTITY,
}),
)
file: Express.Multer.File,
) {
const path = `/data/${req.user.id}.png`;
writeFile(path, file.buffer, (err) => {
if (err) throw err;
});
}
@UseGuards(JwtAuthGuard)
@ApiBearerAuth()
@ApiOkResponse({ description: 'Successfully logged in', type: User })