-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from siwonpada/master
add reactions and upload documents
- Loading branch information
Showing
11 changed files
with
351 additions
and
11 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { | ||
Controller, | ||
Post, | ||
UploadedFiles, | ||
UseInterceptors, | ||
} from '@nestjs/common'; | ||
import { ApiBody, ApiConsumes, ApiResponse, ApiTags } from '@nestjs/swagger'; | ||
import { DocumentService } from './document.service'; | ||
import { FilesInterceptor } from '@nestjs/platform-express'; | ||
|
||
@ApiTags('document') | ||
@Controller('document') | ||
export class DocumentController { | ||
constructor(private readonly documentService: DocumentService) {} | ||
|
||
@ApiBody({ | ||
required: true, | ||
type: 'multipart/form-data', | ||
schema: { | ||
type: 'object', | ||
properties: { | ||
documents: { | ||
type: 'array', | ||
items: { | ||
type: 'string', | ||
format: 'binary', | ||
}, | ||
}, | ||
}, | ||
}, | ||
}) | ||
@ApiConsumes('multipart/form-data') | ||
@ApiResponse({ type: [String], status: 201 }) | ||
@Post('upload') | ||
@UseInterceptors(FilesInterceptor('documents')) | ||
async uploadDocument( | ||
@UploadedFiles() files: Express.Multer.File[], | ||
): Promise<string[]> { | ||
return this.documentService.uploadDocuments(files); | ||
} | ||
} |
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,10 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { DocumentController } from './document.controller'; | ||
import { DocumentService } from './document.service'; | ||
|
||
@Module({ | ||
controllers: [DocumentController], | ||
providers: [DocumentService], | ||
exports: [DocumentService], | ||
}) | ||
export class DocumentModule {} |
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,121 @@ | ||
import { | ||
DeleteObjectCommand, | ||
PutObjectCommand, | ||
PutObjectTaggingCommand, | ||
S3Client, | ||
} from '@aws-sdk/client-s3'; | ||
import { | ||
BadRequestException, | ||
Injectable, | ||
InternalServerErrorException, | ||
Logger, | ||
} from '@nestjs/common'; | ||
import { ConfigService } from '@nestjs/config'; | ||
import path from 'path'; | ||
|
||
@Injectable() | ||
export class DocumentService { | ||
private readonly logger = new Logger(DocumentService.name); | ||
constructor(private readonly configService: ConfigService) {} | ||
|
||
async uploadDocuments(files: Express.Multer.File[]): Promise<string[]> { | ||
if (!files) { | ||
throw new BadRequestException('No documents sent'); | ||
} | ||
return Promise.all(files.map((file) => this.uploadDocument(file))); | ||
} | ||
|
||
async validateDocuments(documentKeys: string[]): Promise<void> { | ||
if (!documentKeys) { | ||
throw new BadRequestException('No documents sent'); | ||
} | ||
|
||
await Promise.all( | ||
documentKeys.map((documentKey) => | ||
this.validateUploadedDocument(documentKey), | ||
), | ||
); | ||
} | ||
|
||
async deleteDocuments(documentKeys: string[]): Promise<void> { | ||
if (!documentKeys) { | ||
return; | ||
} | ||
|
||
await Promise.all( | ||
documentKeys.map((documentKey) => this.deleteDocument(documentKey)), | ||
); | ||
} | ||
|
||
private async uploadDocument(file: Express.Multer.File): Promise<string> { | ||
const s3 = new S3Client({ | ||
region: this.configService.get<string>('AWS_S3_REGION'), | ||
}); | ||
const key = `${new Date().toISOString()}-${Math.random() | ||
.toString(36) | ||
.substring(2)}.${path.extname(file.originalname)}`; | ||
|
||
const command = new PutObjectCommand({ | ||
Bucket: this.configService.get<string>('AWS_S3_BUCKET_NAME'), | ||
Key: key, | ||
Body: file.buffer, | ||
Tagging: 'expiration=true', | ||
Metadata: { | ||
originalName: file.originalname, | ||
}, | ||
}); | ||
|
||
try { | ||
await s3.send(command); | ||
return key; | ||
} catch (error) { | ||
this.logger.error('error uploading document'); | ||
this.logger.debug(error); | ||
throw new InternalServerErrorException("Couldn't uploading document"); | ||
} | ||
} | ||
|
||
private async validateUploadedDocument(documentKey: string): Promise<void> { | ||
const s3 = new S3Client({ | ||
region: this.configService.get<string>('AWS_S3_REGION'), | ||
}); | ||
const command = new PutObjectTaggingCommand({ | ||
Bucket: this.configService.get<string>('AWS_S3_BUCKET_NAME'), | ||
Key: documentKey, | ||
Tagging: { | ||
TagSet: [ | ||
{ | ||
Key: 'expiration', | ||
Value: 'false', | ||
}, | ||
], | ||
}, | ||
}); | ||
|
||
try { | ||
await s3.send(command); | ||
} catch (error) { | ||
this.logger.error('error validating document'); | ||
this.logger.debug(error); | ||
throw new BadRequestException('Invalid document'); | ||
} | ||
} | ||
|
||
private async deleteDocument(documentKey: string): Promise<void> { | ||
const s3 = new S3Client({ | ||
region: this.configService.get<string>('AWS_S3_REGION'), | ||
}); | ||
const command = new DeleteObjectCommand({ | ||
Bucket: this.configService.get<string>('AWS_S3_BUCKET_NAME'), | ||
Key: documentKey, | ||
}); | ||
|
||
try { | ||
await s3.send(command); | ||
} catch (error) { | ||
this.logger.error('error deleting document'); | ||
this.logger.debug(error); | ||
throw new InternalServerErrorException("Couldn't delete document"); | ||
} | ||
} | ||
} |
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,12 @@ | ||
import { ApiProperty } from '@nestjs/swagger'; | ||
import { IsString } from 'class-validator'; | ||
|
||
export class ReactionDto { | ||
@ApiProperty({ | ||
example: '👍', | ||
description: '반응할 이모지', | ||
required: true, | ||
}) | ||
@IsString() | ||
emoji: string; | ||
} |
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
Oops, something went wrong.