ba me oi con thanh cong roi
This commit is contained in:
@@ -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,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user