sotre profile avatars in R2..

This commit is contained in:
2026-07-08 11:41:53 +09:00
parent d6f1272afc
commit a195c7334b
8 changed files with 574 additions and 8 deletions
+2 -1
View File
@@ -4,12 +4,13 @@ import { AuthModule } from '../auth/auth.module';
import { ChatModule } from '../chat/chat.module';
import { PrismaModule } from '../prisma/prisma.module';
import { PlatformGatewayClient } from '../platform-control/platform-gateway.client';
import { R2PublicFileService } from '../storage/r2-public-file.service';
import { InstructorController } from './instructor.controller';
import { InstructorService } from './instructor.service';
@Module({
imports: [AuthModule, ChatModule, PrismaModule],
controllers: [InstructorController],
providers: [InstructorService, PlatformGatewayClient],
providers: [InstructorService, PlatformGatewayClient, R2PublicFileService],
})
export class InstructorModule {}
+30 -5
View File
@@ -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(),
};
}