1- import { Injectable , Logger } from '@nestjs/common' ;
1+ import { forwardRef , Inject , Injectable , Logger } from '@nestjs/common' ;
22import { ConfigService } from '@nestjs/config' ;
33import { Client , ItemBucketMetadata } from 'minio' ;
4+ import { ImageService } from './image.service' ;
45
56@Injectable ( )
67export class MinioService {
78 private readonly allowedImageTypes = [
89 'image/jpeg' ,
910 'image/png' ,
1011 'image/tiff' ,
11- 'image/heic' ,
1212 ] ;
1313
1414 private client : Client ;
@@ -21,6 +21,8 @@ export class MinioService {
2121 constructor (
2222 private readonly configService : ConfigService ,
2323 private readonly logger : Logger ,
24+ @Inject ( forwardRef ( ( ) => ImageService ) )
25+ private readonly imageService : ImageService ,
2426 ) {
2527 const accessKey = this . configService . get < string > ( 'MINIO.accessKey' ) ;
2628 const secretKey = this . configService . get < string > ( 'MINIO.secretKey' ) ;
@@ -63,14 +65,22 @@ export class MinioService {
6365 ) ;
6466 }
6567
68+ public async generatePresignedGetUrl ( file : string ) : Promise < string > {
69+ return this . client . presignedGetObject (
70+ this . bucketName ,
71+ file ,
72+ this . downloadExpiry ,
73+ ) ;
74+ }
75+
6676 public async generatePresignedPostUrl (
6777 file : string ,
6878 contentType : string ,
6979 fileSizeMb : number ,
7080 ) : Promise < {
7181 postURL : string ;
7282 formData : {
73- [ key : string ] : any ;
83+ [ key : string ] : unknown ;
7484 } ;
7585 } > {
7686 const policy = this . client . newPostPolicy ( ) ;
@@ -93,7 +103,11 @@ export class MinioService {
93103 return this . client . getObject ( this . bucketName , key ) ;
94104 }
95105
96- public putObject ( key : string , webpBuffer : Buffer , metaData ?: ItemBucketMetadata ) {
106+ public putObject (
107+ key : string ,
108+ webpBuffer : Buffer ,
109+ metaData ?: ItemBucketMetadata ,
110+ ) {
97111 return this . client . putObject (
98112 this . bucketName ,
99113 key ,
@@ -102,4 +116,21 @@ export class MinioService {
102116 metaData ,
103117 ) ;
104118 }
119+
120+ public async deleteObject ( key : string ) {
121+ await this . client . removeObject ( this . bucketName , key ) ;
122+ }
123+
124+ async deleteImages ( entity : string , id : number , imageId : string ) {
125+ await Promise . all ( [
126+ ...ImageService . sizes . map ( async ( size ) =>
127+ this . deleteObject ( `devices/${ id } /images/${ imageId } -webp-${ size } ` ) ,
128+ ) ,
129+ ...ImageService . blurredSizes . map ( async ( size ) =>
130+ this . deleteObject ( `devices/${ id } /images/${ imageId } -webp-${ size } -blur` ) ,
131+ ) ,
132+ this . deleteObject ( `devices/${ id } /images/${ imageId } ` ) ,
133+ this . deleteObject ( `devices/${ id } /images/${ imageId } -webp` ) ,
134+ ] ) ;
135+ }
105136}
0 commit comments