fix presigned url

This commit is contained in:
2024-10-17 20:58:13 +07:00
parent a9f9861e43
commit 4d908680f0
5 changed files with 80 additions and 31 deletions

View File

@@ -28,9 +28,6 @@ export class CenterStaffSchema extends PothosSchema {
centerId: t.exposeID('centerId', { centerId: t.exposeID('centerId', {
description: 'The ID of the center.', description: 'The ID of the center.',
}), }),
serviceId: t.exposeID('serviceId', {
description: 'The ID of the service.',
}),
staff: t.relation('staff', { staff: t.relation('staff', {
description: 'The staff member.', description: 'The staff member.',
}), }),

View File

@@ -30,27 +30,56 @@ export class MinioService {
return { result, filename, mimetype, actualFileName }; return { result, filename, mimetype, actualFileName };
} }
async getFileUrl(fileName: string, category: string) { async getFileUrl(id: string, category: string) {
if (!fileName) { if (!id) {
return null; 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( const url = await this.minioClient.presignedUrl(
'GET', 'GET',
this.configService.get('BUCKET_NAME') ?? 'epess', this.configService.get('BUCKET_NAME') ?? 'epess',
`${category}/${fileName}`, `${category}/${id}`,
3600, 3600,
); );
// replace the url with the url with actual url
return url; return url;
} }
async deleteFile(fileName: string, category: string) { async deleteFile(id: string, category: string) {
return await this.minioClient.removeObject( return await this.minioClient.removeObject(
this.configService.get('BUCKET_NAME') ?? 'epess', 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() { fileName() {
// generate a unique file name using uuid // generate a unique file name using uuid

View File

@@ -56,14 +56,12 @@ export class ResumeSchema extends PothosSchema {
fileUrl: t.string({ fileUrl: t.string({
description: 'The URL of the resume file.', description: 'The URL of the resume file.',
resolve: async (file, args, ctx) => { resolve: async (file, args, ctx) => {
const fileUrl = await this.minioService.getFileUrl( // check if fileUrl is still valid
return await this.minioService.getNewFileUrl(
file.id, file.id,
'resumes', 'resumes',
file.fileUrl,
); );
if (!fileUrl) {
throw new Error('Cannot retrieve file url');
}
return fileUrl;
}, },
}), }),
actualFileName: t.exposeString('actualFileName', { actualFileName: t.exposeString('actualFileName', {

File diff suppressed because one or more lines are too long