Files
epess-web-backend/src/Minio/minio.service.ts
2024-10-14 23:44:10 +07:00

48 lines
1.4 KiB
TypeScript

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) {
const url = await this.minioClient.presignedUrl(
'GET',
this.configService.get('BUCKET_NAME') ?? 'epess',
`${category}/${fileName}`,
3600,
);
// replace the url with the url with actual url
return url;
}
async deleteFile(fileName: string) {
return await this.minioClient.removeObject(
this.configService.get('BUCKET_NAME') ?? 'epess',
fileName,
);
}
}