sotre profile avatars in R2..
This commit is contained in:
@@ -17,6 +17,7 @@ import { AuthService, type AuthResponseUser } from '../auth/auth.service';
|
||||
import { ChatGateway } from '../chat/chat.gateway';
|
||||
import { PrismaService } from '../prisma/prisma.service';
|
||||
import { PlatformGatewayClient, type GatewayAccessAssignment, type GatewayAgent } from '../platform-control/platform-gateway.client';
|
||||
import { R2PublicFileService } from '../storage/r2-public-file.service';
|
||||
import { CreateInstructorPlatformDto } from './dto/create-instructor-platform.dto';
|
||||
import { CreateInstructorStudentDto } from './dto/create-instructor-student.dto';
|
||||
import { SaveInstructorGroupDto } from './dto/save-instructor-group.dto';
|
||||
@@ -64,6 +65,7 @@ export class InstructorService {
|
||||
private readonly prisma: PrismaService,
|
||||
private readonly configService: ConfigService,
|
||||
private readonly platformGatewayClient: PlatformGatewayClient,
|
||||
private readonly r2PublicFileService: R2PublicFileService,
|
||||
) {}
|
||||
|
||||
async listCourses(accessToken: string | undefined) {
|
||||
@@ -1534,9 +1536,27 @@ export class InstructorService {
|
||||
const kind = input.kind ?? 'REFERENCE';
|
||||
|
||||
const originalFileName = this.normalizeUploadedFileName(file.originalname);
|
||||
await mkdir(COURSE_REFERENCE_UPLOAD_ROOT, { recursive: true });
|
||||
const storedFileName = `${randomUUID()}${extname(originalFileName || '')}`;
|
||||
await writeFile(join(COURSE_REFERENCE_UPLOAD_ROOT, storedFileName), file.buffer);
|
||||
const fileId = randomUUID();
|
||||
const extension = extname(originalFileName || '');
|
||||
let storedFileName = `${fileId}${extension}`;
|
||||
if (this.r2PublicFileService.isConfigured()) {
|
||||
storedFileName = this.r2PublicFileService.createLmsCourseFileKey({
|
||||
courseId: course.id,
|
||||
week: targetWeek.week,
|
||||
kind,
|
||||
fileId,
|
||||
extension,
|
||||
});
|
||||
await this.r2PublicFileService.uploadPublicFile({
|
||||
key: storedFileName,
|
||||
body: file.buffer,
|
||||
contentType: file.mimetype,
|
||||
fileName: originalFileName,
|
||||
});
|
||||
} else {
|
||||
await mkdir(COURSE_REFERENCE_UPLOAD_ROOT, { recursive: true });
|
||||
await writeFile(join(COURSE_REFERENCE_UPLOAD_ROOT, storedFileName), file.buffer);
|
||||
}
|
||||
|
||||
const maxSortOrder = await this.prisma.courseWeekReference.aggregate({
|
||||
where: { weekId: targetWeek.id, kind },
|
||||
@@ -1581,7 +1601,11 @@ export class InstructorService {
|
||||
}
|
||||
|
||||
await this.prisma.courseWeekReference.delete({ where: { id: reference.id } });
|
||||
await unlink(join(COURSE_REFERENCE_UPLOAD_ROOT, reference.storedFileName)).catch(() => undefined);
|
||||
if (this.r2PublicFileService.isPublicKey(reference.storedFileName)) {
|
||||
await this.r2PublicFileService.deletePublicFile(reference.storedFileName).catch(() => undefined);
|
||||
} else {
|
||||
await unlink(join(COURSE_REFERENCE_UPLOAD_ROOT, reference.storedFileName)).catch(() => undefined);
|
||||
}
|
||||
|
||||
return this.getCurriculum(accessToken, null, reference.week.courseId);
|
||||
}
|
||||
@@ -2837,6 +2861,7 @@ export class InstructorService {
|
||||
kind: string;
|
||||
title: string;
|
||||
originalFileName: string;
|
||||
storedFileName: string;
|
||||
contentType: string | null;
|
||||
fileSizeBytes: number;
|
||||
createdAt: Date;
|
||||
@@ -2853,7 +2878,7 @@ export class InstructorService {
|
||||
fileSizeBytes: reference.fileSizeBytes,
|
||||
week,
|
||||
weekNumber: week,
|
||||
downloadUrl: `/curriculum/references/${reference.id}/download`,
|
||||
downloadUrl: this.r2PublicFileService.publicUrl(reference.storedFileName) ?? `/curriculum/references/${reference.id}/download`,
|
||||
createdAt: reference.createdAt.toISOString(),
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user