fix presigned url
This commit is contained in:
@@ -28,9 +28,6 @@ export class CenterStaffSchema extends PothosSchema {
|
||||
centerId: t.exposeID('centerId', {
|
||||
description: 'The ID of the center.',
|
||||
}),
|
||||
serviceId: t.exposeID('serviceId', {
|
||||
description: 'The ID of the service.',
|
||||
}),
|
||||
staff: t.relation('staff', {
|
||||
description: 'The staff member.',
|
||||
}),
|
||||
|
||||
@@ -30,27 +30,56 @@ export class MinioService {
|
||||
return { result, filename, mimetype, actualFileName };
|
||||
}
|
||||
|
||||
async getFileUrl(fileName: string, category: string) {
|
||||
if (!fileName) {
|
||||
async getFileUrl(id: string, category: string) {
|
||||
if (!id) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const url = await this.minioClient.presignedUrl(
|
||||
'GET',
|
||||
this.configService.get('BUCKET_NAME') ?? 'epess',
|
||||
`${category}/${id}`,
|
||||
3600,
|
||||
);
|
||||
return url;
|
||||
}
|
||||
|
||||
async getNewFileUrl(id: string, category: string, fileUrl: string) {
|
||||
if (await this.checkFileUrl(fileUrl)) {
|
||||
return fileUrl;
|
||||
}
|
||||
const url = await this.minioClient.presignedUrl(
|
||||
'GET',
|
||||
this.configService.get('BUCKET_NAME') ?? 'epess',
|
||||
`${category}/${fileName}`,
|
||||
`${category}/${id}`,
|
||||
3600,
|
||||
);
|
||||
// replace the url with the url with actual url
|
||||
return url;
|
||||
}
|
||||
|
||||
async deleteFile(fileName: string, category: string) {
|
||||
async deleteFile(id: string, category: string) {
|
||||
return await this.minioClient.removeObject(
|
||||
this.configService.get('BUCKET_NAME') ?? 'epess',
|
||||
`${category}/${fileName}`,
|
||||
`${category}/${id}`,
|
||||
);
|
||||
}
|
||||
|
||||
async checkFileUrl(fileUrl: string) {
|
||||
// check if the file url is still valid by get X-Amz-Date= from the url and X-Amz-Expires from the url and compare with the current time
|
||||
const url = new URL(fileUrl);
|
||||
const _xAmzDate = url.searchParams.get('X-Amz-Date');
|
||||
const _xAmzExpires = url.searchParams.get('X-Amz-Expires');
|
||||
if (!_xAmzDate || !_xAmzExpires) {
|
||||
return false;
|
||||
}
|
||||
const __xAmzDate = new Date(_xAmzDate);
|
||||
const __xAmzExpires = new Date(
|
||||
__xAmzDate.getTime() + parseInt(_xAmzExpires),
|
||||
);
|
||||
const currentTime = new Date();
|
||||
return currentTime.getTime() < __xAmzExpires.getTime(); // true if the file url is still valid
|
||||
}
|
||||
|
||||
//
|
||||
fileName() {
|
||||
// generate a unique file name using uuid
|
||||
|
||||
@@ -56,14 +56,12 @@ export class ResumeSchema extends PothosSchema {
|
||||
fileUrl: t.string({
|
||||
description: 'The URL of the resume file.',
|
||||
resolve: async (file, args, ctx) => {
|
||||
const fileUrl = await this.minioService.getFileUrl(
|
||||
// check if fileUrl is still valid
|
||||
return await this.minioService.getNewFileUrl(
|
||||
file.id,
|
||||
'resumes',
|
||||
file.fileUrl,
|
||||
);
|
||||
if (!fileUrl) {
|
||||
throw new Error('Cannot retrieve file url');
|
||||
}
|
||||
return fileUrl;
|
||||
},
|
||||
}),
|
||||
actualFileName: t.exposeString('actualFileName', {
|
||||
|
||||
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user