forked from keyshade-xyz/keyshade
-
Notifications
You must be signed in to change notification settings - Fork 0
/
minio.provider.ts
123 lines (109 loc) · 3.62 KB
/
minio.provider.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import { InternalServerErrorException, Logger, Provider } from '@nestjs/common'
import * as Minio from 'minio'
import { exit } from 'process'
export const MINIO_CLIENT = 'MinioClient'
export const MinioProvider: Provider = {
provide: MINIO_CLIENT,
useFactory: async () => {
let minioClient: Minio.Client
let isServiceLoaded: boolean = false
const logger = new Logger('MinioProvider')
if (
!process.env.MINIO_ENDPOINT ||
!process.env.MINIO_PORT ||
!process.env.MINIO_ACCESS_KEY ||
!process.env.MINIO_SECRET_KEY ||
!process.env.MINIO_BUCKET_NAME
) {
logger.warn(
'Missing one or more MinIO environment variables. Skipping initialization...'
)
return
}
try {
minioClient = new Minio.Client({
endPoint: process.env.MINIO_ENDPOINT,
port: Number(process.env.MINIO_PORT),
useSSL: process.env.MINIO_USE_SSL === 'true',
accessKey: process.env.MINIO_ACCESS_KEY,
secretKey: process.env.MINIO_SECRET_KEY
})
} catch (error) {
logger.error('Error initializing Minio Client', error)
exit(1)
}
const bucketName = process.env.MINIO_BUCKET_NAME
await createBucketIfNotExists()
if (isServiceLoaded) {
logger.log('Minio Provider Loaded Successfully.')
}
async function createBucketIfNotExists() {
if (!minioClient) return
let bucketExists: boolean = false
try {
bucketExists = await minioClient.bucketExists(bucketName)
} catch (error) {
logger.error('Error while checking if bucket exists in Minio', error)
throw new InternalServerErrorException(
'Error while checking if bucket exists in Minio'
)
}
if (!bucketExists) {
logger.warn(`Bucket ${bucketName} does not exist. Creating it.`)
try {
await minioClient.makeBucket(bucketName, 'ap-south-1')
} catch (error) {
logger.error('Error creating bucket in Minio', error)
throw new InternalServerErrorException(
'Error creating bucket in Minio'
)
}
logger.log(`Bucket ${bucketName} created.`)
}
isServiceLoaded = true
}
async function uploadFile(file) {
if (!isServiceLoaded) {
return new InternalServerErrorException('Minio Client has not loaded')
}
const fileName = `${Date.now()}-${file.originalname}`
try {
await minioClient.putObject(
bucketName,
fileName,
file.buffer,
file.size
)
} catch (error) {
logger.error('Error uploading file to Minio', error)
throw new InternalServerErrorException('Error uploading file to Minio')
}
return fileName
}
async function getFileUrl(fileName: string) {
if (!isServiceLoaded) {
return new InternalServerErrorException('Minio Client has not loaded')
}
try {
return await minioClient.presignedUrl('GET', bucketName, fileName)
} catch (error) {
logger.error('Error generating file URL from Minio', error)
throw new InternalServerErrorException(
'Error generating file URL from Minio'
)
}
}
async function deleteFile(fileName: string) {
if (!isServiceLoaded) {
return new InternalServerErrorException('Minio Client has not loaded')
}
try {
await minioClient.removeObject(bucketName, fileName)
return true
} catch (error) {
logger.error('Error deleting file from Minio', error)
throw new InternalServerErrorException('Error deleting file from Minio')
}
}
}
}