feat: add thumbnail upload support to course creation endpoint with multipart form data handling.
This commit is contained in:
parent
19844f343b
commit
cf12ef965e
3 changed files with 54 additions and 44 deletions
|
|
@ -4,6 +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 {
|
||||
CreateCourseInput,
|
||||
UpdateCourseInput,
|
||||
|
|
@ -24,8 +25,22 @@ import {
|
|||
} from "../types/CoursesInstructor.types";
|
||||
|
||||
export class CoursesInstructorService {
|
||||
static async createCourse(courseData: CreateCourseInput, userId: number): Promise<createCourseResponse> {
|
||||
static async createCourse(courseData: CreateCourseInput, userId: number, thumbnailFile?: Express.Multer.File): Promise<createCourseResponse> {
|
||||
try {
|
||||
let thumbnailUrl: string | undefined;
|
||||
|
||||
// Upload thumbnail to MinIO if provided
|
||||
if (thumbnailFile) {
|
||||
const timestamp = Date.now();
|
||||
const uniqueId = Math.random().toString(36).substring(2, 15);
|
||||
const extension = thumbnailFile.originalname.split('.').pop() || 'jpg';
|
||||
const safeFilename = `${timestamp}-${uniqueId}.${extension}`;
|
||||
const filePath = `courses/thumbnails/${safeFilename}`;
|
||||
|
||||
await uploadFile(filePath, thumbnailFile.buffer, thumbnailFile.mimetype || 'image/jpeg');
|
||||
thumbnailUrl = filePath;
|
||||
}
|
||||
|
||||
// Use transaction to create course and instructor together
|
||||
const result = await prisma.$transaction(async (tx) => {
|
||||
// Create the course
|
||||
|
|
@ -35,7 +50,7 @@ export class CoursesInstructorService {
|
|||
title: courseData.title,
|
||||
slug: courseData.slug,
|
||||
description: courseData.description,
|
||||
thumbnail_url: courseData.thumbnail_url,
|
||||
thumbnail_url: thumbnailUrl,
|
||||
price: courseData.price || 0,
|
||||
is_free: courseData.is_free ?? false,
|
||||
have_certificate: courseData.have_certificate ?? false,
|
||||
|
|
|
|||
|
|
@ -221,32 +221,32 @@ export class UserService {
|
|||
// Upload to MinIO
|
||||
await uploadFile(filePath, file.buffer, file.mimetype || 'image/jpeg');
|
||||
|
||||
// Update user profile with avatar URL
|
||||
const avatarUrl = `${config.s3.endpoint}/${config.s3.bucket}/${filePath}`;
|
||||
|
||||
// Update or create profile
|
||||
// Update or create profile - store only file path
|
||||
if (user.profile) {
|
||||
await prisma.userProfile.update({
|
||||
where: { user_id: decoded.id },
|
||||
data: { avatar_url: avatarUrl }
|
||||
data: { avatar_url: filePath }
|
||||
});
|
||||
} else {
|
||||
await prisma.userProfile.create({
|
||||
data: {
|
||||
user_id: decoded.id,
|
||||
avatar_url: avatarUrl,
|
||||
avatar_url: filePath,
|
||||
first_name: '',
|
||||
last_name: ''
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// Generate presigned URL for response
|
||||
const presignedUrl = await this.getAvatarPresignedUrl(filePath);
|
||||
|
||||
return {
|
||||
code: 200,
|
||||
message: 'Avatar uploaded successfully',
|
||||
data: {
|
||||
id: decoded.id,
|
||||
avatar_url: avatarUrl
|
||||
avatar_url: presignedUrl
|
||||
}
|
||||
};
|
||||
} catch (error) {
|
||||
|
|
@ -266,16 +266,13 @@ export class UserService {
|
|||
/**
|
||||
* Get presigned URL for avatar
|
||||
*/
|
||||
private async getAvatarPresignedUrl(avatarUrl: string): Promise<string | null> {
|
||||
private async getAvatarPresignedUrl(avatarPath: string): Promise<string> {
|
||||
try {
|
||||
// Extract file path from stored URL or path
|
||||
const filePath = avatarUrl.includes('/')
|
||||
? `avatars/${avatarUrl.split('/').slice(-2).join('/')}`
|
||||
: avatarUrl;
|
||||
return await getPresignedUrl(filePath, 3600); // 1 hour expiry
|
||||
// avatarPath is now stored as file path directly (e.g., avatars/1/filename.jpg)
|
||||
return await getPresignedUrl(avatarPath, 3600); // 1 hour expiry
|
||||
} catch (error) {
|
||||
logger.warn(`Failed to generate presigned URL for avatar: ${error}`);
|
||||
return null;
|
||||
return '';
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue