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,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,
);
}
}