feat: add presigned URL generation for course thumbnails across all course services.

This commit is contained in:
JakkrapartXD 2026-01-28 15:31:21 +07:00
parent b28dd410e2
commit 10821d093c
3 changed files with 131 additions and 27 deletions

View file

@ -4,7 +4,7 @@ import { config } from '../config';
import { logger } from '../config/logger';
import { UnauthorizedError, ValidationError, ForbiddenError, NotFoundError } from '../middleware/errorHandler';
import jwt from 'jsonwebtoken';
import { uploadFile, deleteFile } from '../config/minio';
import { uploadFile, deleteFile, getPresignedUrl } from '../config/minio';
import {
CreateCourseInput,
UpdateCourseInput,
@ -94,7 +94,22 @@ export class CoursesInstructorService {
}
});
const courses = courseInstructors.map(ci => ci.course);
const courses = await Promise.all(
courseInstructors.map(async (ci) => {
let thumbnail_presigned_url: string | null = null;
if (ci.course.thumbnail_url) {
try {
thumbnail_presigned_url = await getPresignedUrl(ci.course.thumbnail_url, 3600);
} catch (err) {
logger.warn(`Failed to generate presigned URL for thumbnail: ${err}`);
}
}
return {
...ci.course,
thumbnail_url: thumbnail_presigned_url,
};
})
);
return {
code: 200,
@ -141,10 +156,23 @@ export class CoursesInstructorService {
throw new ForbiddenError('You are not an instructor of this course');
}
// Generate presigned URL for thumbnail
let thumbnail_presigned_url: string | null = null;
if (courseInstructor.course.thumbnail_url) {
try {
thumbnail_presigned_url = await getPresignedUrl(courseInstructor.course.thumbnail_url, 3600);
} catch (err) {
logger.warn(`Failed to generate presigned URL for thumbnail: ${err}`);
}
}
return {
code: 200,
message: 'Course retrieved successfully',
data: courseInstructor.course
data: {
...courseInstructor.course,
thumbnail_url: thumbnail_presigned_url,
}
};
} catch (error) {
logger.error('Failed to retrieve course', { error });
@ -162,10 +190,24 @@ export class CoursesInstructorService {
},
data: courseData
});
// Generate presigned URL for thumbnail
let thumbnail_presigned_url: string | null = null;
if (course.thumbnail_url) {
try {
thumbnail_presigned_url = await getPresignedUrl(course.thumbnail_url, 3600);
} catch (err) {
logger.warn(`Failed to generate presigned URL for thumbnail: ${err}`);
}
}
return {
code: 200,
message: 'Course updated successfully',
data: course
data: {
...course,
thumbnail_url: thumbnail_presigned_url,
}
};
} catch (error) {
logger.error('Failed to update course', { error });