-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* [backend] Format schema.prisma * [backend] Add avatarURL to User model, and add avatar module * [backend] Implement GET /avatar/:filename API * [backend] Implement POST /user/:userId/avatar API (Upload) * [backend] Implement DELETE /user/:userId/avatar API (Remove) * [web/backend] Add avatar images volume
- Loading branch information
Showing
21 changed files
with
426 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
backend/prisma/migrations/20231210151218_add_avatar_to_user/migration.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
-- AlterTable | ||
ALTER TABLE "User" ADD COLUMN "avatarURL" TEXT; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { AvatarController } from './avatar.controller'; | ||
import { AvatarService } from './avatar.service'; | ||
import { PrismaService } from 'src/prisma/prisma.service'; | ||
|
||
describe('AvatarController', () => { | ||
let controller: AvatarController; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
controllers: [AvatarController], | ||
providers: [AvatarService, PrismaService], | ||
}).compile(); | ||
|
||
controller = module.get<AvatarController>(AvatarController); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(controller).toBeDefined(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import { | ||
Controller, | ||
Get, | ||
Post, | ||
Body, | ||
Delete, | ||
UseGuards, | ||
Param, | ||
Res, | ||
UseInterceptors, | ||
UploadedFile, | ||
ParseIntPipe, | ||
HttpCode, | ||
} from '@nestjs/common'; | ||
import { AvatarService } from './avatar.service'; | ||
import { JwtAuthGuard } from 'src/auth/jwt-auth.guard'; | ||
import { UserGuard } from 'src/user/user.guard'; | ||
import { | ||
ApiBearerAuth, | ||
ApiConsumes, | ||
ApiNoContentResponse, | ||
ApiTags, | ||
} from '@nestjs/swagger'; | ||
import { Response } from 'express'; | ||
import { FileInterceptor } from '@nestjs/platform-express'; | ||
import * as multer from 'multer'; | ||
import { CreateAvatarDto } from './dto/create-avatar.dto'; | ||
|
||
@Controller() | ||
@ApiTags('avatar') | ||
export class AvatarController { | ||
constructor(private readonly avatarService: AvatarService) {} | ||
|
||
// Public | ||
@Get('avatar/:filename') | ||
findOne(@Param('filename') filename: string, @Res() res: Response) { | ||
// Validate filename | ||
// e.g. 1621234567890-1.png | ||
// e.g. default.png | ||
// e.g. 1621234567890-1.jpeg | ||
if (!filename.match(/^(default|(\d+)-\d+)\.(png|jpeg)$/)) { | ||
return res.status(404).send('Not found'); | ||
} | ||
res.sendFile(filename, { root: 'public/avatar' }); | ||
} | ||
|
||
// Private | ||
@Post('user/:userId/avatar') | ||
@UseInterceptors( | ||
FileInterceptor('avatar', { | ||
// File size limit | ||
limits: { | ||
fileSize: 1024 * 1024, | ||
}, | ||
// File type filter | ||
fileFilter: (req, file, cb) => { | ||
const allowedMimes = ['image/jpeg', 'image/png']; | ||
if (allowedMimes.includes(file.mimetype)) { | ||
cb(null, true); | ||
} else { | ||
cb(new Error('Unsupported file type'), false); | ||
} | ||
}, | ||
// Save file to public/avatar | ||
storage: multer.diskStorage({ | ||
destination: './public/avatar', | ||
filename: (req, file, cb) => { | ||
const ext = file.mimetype.split('/')[1]; | ||
const filename = `${Date.now()}-${req.params.userId}.${ext}`; | ||
cb(null, filename); | ||
}, | ||
}), | ||
}), | ||
) | ||
@UseGuards(JwtAuthGuard, UserGuard) | ||
@ApiConsumes('multipart/form-data') | ||
@ApiBearerAuth() | ||
create( | ||
@Param('userId', ParseIntPipe) userId: number, | ||
@Body() createAvatarDto: CreateAvatarDto, | ||
@UploadedFile() file: Express.Multer.File, | ||
) { | ||
return this.avatarService.create(userId, file); | ||
} | ||
|
||
@Delete('user/:userId/avatar') | ||
@HttpCode(204) | ||
@UseGuards(JwtAuthGuard, UserGuard) | ||
@ApiNoContentResponse() | ||
@ApiBearerAuth() | ||
remove(@Param('userId', ParseIntPipe) userId: number) { | ||
return this.avatarService.remove(userId); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { AvatarService } from './avatar.service'; | ||
import { AvatarController } from './avatar.controller'; | ||
import { PrismaModule } from 'src/prisma/prisma.module'; | ||
|
||
@Module({ | ||
controllers: [AvatarController], | ||
providers: [AvatarService], | ||
imports: [PrismaModule], | ||
}) | ||
export class AvatarModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { AvatarService } from './avatar.service'; | ||
import { PrismaService } from 'src/prisma/prisma.service'; | ||
|
||
describe('AvatarService', () => { | ||
let service: AvatarService; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
providers: [AvatarService, PrismaService], | ||
}).compile(); | ||
|
||
service = module.get<AvatarService>(AvatarService); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(service).toBeDefined(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { PrismaService } from 'src/prisma/prisma.service'; | ||
import * as fs from 'fs'; | ||
|
||
@Injectable() | ||
export class AvatarService { | ||
constructor(private prisma: PrismaService) {} | ||
|
||
async create(userId: number, file: Express.Multer.File) { | ||
const user = await this.prisma.user.findUniqueOrThrow({ | ||
where: { id: userId }, | ||
}); | ||
const avatarURL = `/avatar/${file.filename}`; | ||
return this.prisma.user | ||
.update({ | ||
where: { id: userId }, | ||
data: { avatarURL }, | ||
}) | ||
.then(() => { | ||
// Delete old avatar | ||
if (user.avatarURL) { | ||
fs.rmSync('./public' + user.avatarURL, { force: true }); | ||
} | ||
return { filename: file.filename, url: avatarURL }; | ||
}); | ||
} | ||
|
||
async remove(userId: number) { | ||
const user = await this.prisma.user.findUniqueOrThrow({ | ||
where: { id: userId }, | ||
}); | ||
return this.prisma.user | ||
.delete({ | ||
where: { id: userId }, | ||
}) | ||
.then(() => { | ||
if (!user.avatarURL) return user; | ||
fs.rmSync('./public' + user.avatarURL, { force: true }); | ||
return user; | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { ApiProperty } from '@nestjs/swagger'; | ||
|
||
export class CreateAvatarDto { | ||
@ApiProperty({ | ||
description: 'アップロードするファイル', | ||
type: 'file', | ||
properties: { | ||
file: { | ||
type: 'string', | ||
format: 'binary', | ||
}, | ||
}, | ||
}) | ||
avatar!: Express.Multer.File; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export class Avatar {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.