ba me oi con thanh cong roi

This commit is contained in:
2024-10-14 23:05:35 +07:00
parent 76ff5d28ac
commit 0ac5868d2d
18 changed files with 365 additions and 138 deletions

View File

@@ -1,5 +1,20 @@
import { Module, Global } from '@nestjs/common';
import { MinioService } from './minio.service';
import { NestMinioModule } from 'nestjs-minio';
import { ConfigModule } from '@nestjs/config';
@Global()
@Module({})
@Module({
imports: [
ConfigModule.forRoot(),
NestMinioModule.register({
endPoint: process.env.MINIO_ENDPOINT ?? '10.0.27.1',
accessKey: process.env.MINIO_ACCESS_KEY ?? 'minioadmin',
secretKey: process.env.MINIO_SECRET_KEY ?? 'minioadmin',
useSSL: false,
port: 9000,
}),
],
providers: [MinioService],
exports: [MinioService],
})
export class MinioModule {}

View File

@@ -1,9 +1,45 @@
// import { Injectable } from '@nestjs/common';
// import { NestMinioService } from 'nestjs-minio';
// import { ConfigService } from '@nestjs/config';
// @Injectable()
// export class MinioService extends NestMinioService {
// constructor(configService: ConfigService) {
// super(configService);
// }
// }
import { Inject, Injectable } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { FileUpload } from 'graphql-upload/processRequest.js';
import { Client } from 'Minio';
import { MINIO_CONNECTION } from 'nestjs-minio';
@Injectable()
export class MinioService {
constructor(
private readonly configService: ConfigService,
@Inject(MINIO_CONNECTION) private readonly minioClient: Client,
) {}
async uploadFile(file: FileUpload, category: string) {
const { filename, mimetype, createReadStream, encoding } = await file;
const Name = `${category}/${filename}`;
const fileBuffer = createReadStream();
const result = await this.minioClient.putObject(
this.configService.get('BUCKET_NAME') ?? 'epess',
Name,
fileBuffer,
undefined,
{
'Content-Type': mimetype,
},
);
return { result, filename, mimetype };
}
async getFileUrl(fileName: string, category: string) {
return await this.minioClient.presignedUrl(
'GET',
this.configService.get('BUCKET_NAME') ?? 'epess',
`${category}/${fileName}`,
3600,
);
}
async deleteFile(fileName: string) {
return await this.minioClient.removeObject(
this.configService.get('BUCKET_NAME') ?? 'epess',
fileName,
);
}
}