Skip to content

Commit

Permalink
feat: upload file with parameter bucket(#77)
Browse files Browse the repository at this point in the history
  • Loading branch information
dayuy authored and Carrotzpc committed Jun 9, 2023
1 parent 6c29846 commit d245dbf
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 8 deletions.
3 changes: 3 additions & 0 deletions src/common/filters/all-exception.filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ export class AllExceptionFilter implements GqlExceptionFilter {
if (exception.extensions?.exception) {
exception.extensions.exception.stacktrace = exception.stack.split('\n');
}
if (exception.response) {
exception.originalError = exception.response;
}
}
const req = httpHost.getRequest();
Logger.error(
Expand Down
11 changes: 11 additions & 0 deletions src/minio/dto/upload.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { IsEnum } from 'class-validator';

enum BucketName {
bestchains = 'bestchains',
depository = 'depository',
}

export class UploadDto {
@IsEnum(BucketName, { message: `the value of 'bucket' is not supported.` })
bucket: BucketName;
}
18 changes: 10 additions & 8 deletions src/minio/minio.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ import {
StreamableFile,
UseInterceptors,
UploadedFile,
Body,
} from '@nestjs/common';
import { MinioService } from './minio.service';
import * as archiver from 'archiver';
import { Response } from 'src/types';
import { FileInterceptor } from '@nestjs/platform-express';
import { Express } from 'express';
import { genContentHash, DEPOSITORY_BUCKET_NAME } from 'src/common/utils';
import { UploadDto } from './dto/upload.dto';

@Controller('minio')
export class MinioController {
Expand Down Expand Up @@ -78,22 +80,22 @@ export class MinioController {

@Post('/upload')
@UseInterceptors(FileInterceptor('file', { limits: { fileSize: 1000000 } }))
async upload(@UploadedFile() file: Express.Multer.File) {
const exist = await this.minioService.bucketExists(DEPOSITORY_BUCKET_NAME);
async upload(
@Body() body: UploadDto,
@UploadedFile() file: Express.Multer.File,
) {
const { bucket = DEPOSITORY_BUCKET_NAME } = body;
const exist = await this.minioService.bucketExists(bucket);
if (!exist) {
await this.minioService.makeBucket(DEPOSITORY_BUCKET_NAME);
await this.minioService.makeBucket(bucket);
}

const { originalname, buffer } = file;
const lastN = originalname.lastIndexOf('.');
const suffix = lastN > 0 ? originalname.substring(lastN) : '';
const hashFilename = genContentHash(buffer) + suffix;

await this.minioService.putObject(
DEPOSITORY_BUCKET_NAME,
hashFilename,
buffer,
);
await this.minioService.putObject(bucket, hashFilename, buffer);
return {
id: hashFilename,
};
Expand Down

0 comments on commit d245dbf

Please sign in to comment.