update something can update

This commit is contained in:
2024-11-08 17:27:09 +07:00
parent 7eabba1868
commit d7c7f8a782
10 changed files with 93 additions and 19 deletions

View File

@@ -3,6 +3,7 @@ import { ConfigService } from '@nestjs/config'
import { FileUpload } from 'graphql-upload/processRequest.js'
import { Client } from 'minio'
import { MINIO_CONNECTION } from 'nestjs-minio'
import { DateTimeUtils } from 'src/common/utils/datetime.utils'
import { v4 as uuidv4 } from 'uuid'
@Injectable()
export class MinioService {
@@ -12,7 +13,7 @@ export class MinioService {
) {}
async uploadFile(file: FileUpload, category: string) {
// sonar ignore next
// @ts-nocheck
const { mimetype, createReadStream, filename: actualFileName } = await file
const filename = this.fileName()
const Name = `${category}/${filename}`
@@ -30,24 +31,55 @@ export class MinioService {
return { result, filename, mimetype, actualFileName }
}
async getFileUrl(id: string, category: string) {
async getFileUrl(id: string, category: string, presignUrl?: string) {
if (!id) {
return null
}
let url = null
// check if presignUrl is provided and not expired else get new presignUrl
if (
presignUrl &&
!DateTimeUtils.isExpired(presignUrl.split('&X-Amz-Date=')[1])
) {
url = presignUrl
} else {
try {
url = await this.minioClient.presignedUrl(
'GET',
this.configService.get('BUCKET_NAME') ?? 'epess',
`${category}/${id}`,
60 * 60 * 24 * 7,
)
} catch (error) {
console.log(error)
}
}
console.log(url)
return url
}
try {
url = await this.minioClient.presignedUrl(
async updatePresignUrl(id: string, category: string, presignUrl?: string) {
// check if presignUrl is expired else get new presignUrl
const date = presignUrl?.split('&X-Amz-Date=')[1].split('&')[0]
if (presignUrl && !DateTimeUtils.isExpired(date ?? '')) {
return presignUrl
}
// if presignUrl is not provided, get new presignUrl
if (!presignUrl) {
return await this.minioClient.presignedUrl(
'GET',
this.configService.get('BUCKET_NAME') ?? 'epess',
`${category}/${id}`,
60 * 60 * 24 * 7,
)
} catch (error) {
console.log(error)
}
console.log(url)
return url
// if presignUrl is expired, renew it and return
return await this.minioClient.presignedUrl(
'GET',
this.configService.get('BUCKET_NAME') ?? 'epess',
`${category}/${id}`,
60 * 60 * 24 * 7,
)
}
async deleteFile(id: string, category: string) {