Initial commit

This commit is contained in:
theshy
2025-07-31 17:05:07 +08:00
parent 8fab3b19cc
commit 24f21144ab
91 changed files with 16311 additions and 159 deletions

View File

@ -0,0 +1,60 @@
import { NextRequest } from "next/server";
import { getServerSession } from "next-auth/next";
import { authOptions } from "@/lib/auth";
import { RecordingService } from "@/lib/services/recording.service";
import { UserSettingsService } from "@/lib/services/user-settings.service";
import { UserService } from "@/lib/services/user.service";
import { ApiResponseHandler } from "@/lib/utils/api-response";
import { AuthenticationError } from "@/lib/errors/app-error";
export async function GET() {
try {
const session = await getServerSession(authOptions);
if (!session?.user?.id) {
throw new AuthenticationError();
}
// 获取用户数据
const [userProfile, userSettings, recordings] = await Promise.all([
UserService.getUserById(session.user.id),
UserSettingsService.getUserSettings(session.user.id),
RecordingService.getRecordingsByUserId(session.user.id),
]);
// 构建导出数据
const exportData = {
user: {
id: userProfile?.id,
name: userProfile?.name,
email: userProfile?.email,
createdAt: userProfile?.createdAt,
updatedAt: userProfile?.updatedAt,
},
settings: userSettings,
recordings: recordings.map((recording) => ({
id: recording.id,
title: recording.title,
audioUrl: recording.audioUrl,
duration: recording.duration,
fileSize: recording.fileSize,
mimeType: recording.mimeType,
createdAt: recording.createdAt.toISOString(),
})),
exportDate: new Date().toISOString(),
version: "1.0.0",
};
// 返回 JSON 文件
const response = new Response(JSON.stringify(exportData, null, 2), {
headers: {
"Content-Type": "application/json",
"Content-Disposition": `attachment; filename="recorder-export-${Date.now()}.json"`,
},
});
return response;
} catch (error) {
return ApiResponseHandler.error(error as Error);
}
}

View File

@ -0,0 +1,51 @@
import { NextRequest } from "next/server";
import { getServerSession } from "next-auth/next";
import { authOptions } from "@/lib/auth";
import { UserService } from "@/lib/services/user.service";
import { ApiResponseHandler } from "@/lib/utils/api-response";
import { AuthenticationError, ValidationError } from "@/lib/errors/app-error";
export async function GET() {
try {
const session = await getServerSession(authOptions);
if (!session?.user?.id) {
throw new AuthenticationError();
}
const userProfile = await UserService.getUserById(session.user.id);
if (!userProfile) {
throw new AuthenticationError("用户不存在");
}
return ApiResponseHandler.success(userProfile);
} catch (error) {
return ApiResponseHandler.error(error as Error);
}
}
export async function PUT(request: NextRequest) {
try {
const session = await getServerSession(authOptions);
if (!session?.user?.id) {
throw new AuthenticationError();
}
const body = await request.json();
const { name } = body;
if (!name || typeof name !== "string") {
throw new ValidationError("名称不能为空");
}
const updatedProfile = await UserService.updateUser(session.user.id, {
name,
});
return ApiResponseHandler.success(updatedProfile);
} catch (error) {
return ApiResponseHandler.error(error as Error);
}
}

View File

@ -0,0 +1,81 @@
import { NextRequest } from "next/server";
import { getServerSession } from "next-auth/next";
import { authOptions } from "@/lib/auth";
import { UserSettingsService } from "@/lib/services/user-settings.service";
import { ApiResponseHandler } from "@/lib/utils/api-response";
import { AuthenticationError, ValidationError } from "@/lib/errors/app-error";
export async function GET() {
try {
const session = await getServerSession(authOptions);
if (!session?.user?.id) {
throw new AuthenticationError();
}
// 获取或创建用户设置
const settings = await UserSettingsService.getOrCreateUserSettings(
session.user.id
);
return ApiResponseHandler.success(settings);
} catch (error) {
return ApiResponseHandler.error(error as Error);
}
}
export async function PUT(request: NextRequest) {
try {
const session = await getServerSession(authOptions);
if (!session?.user?.id) {
throw new AuthenticationError();
}
const { defaultQuality, publicProfile, allowDownload } =
await request.json();
// 验证音频质量
if (
defaultQuality &&
!["low", "medium", "high", "lossless"].includes(defaultQuality)
) {
throw new ValidationError("无效的音频质量设置");
}
// 更新用户设置
const settings = await UserSettingsService.updateUserSettings(
session.user.id,
{
defaultQuality,
publicProfile:
typeof publicProfile === "boolean" ? publicProfile : undefined,
allowDownload:
typeof allowDownload === "boolean" ? allowDownload : undefined,
}
);
return ApiResponseHandler.success(settings);
} catch (error) {
return ApiResponseHandler.error(error as Error);
}
}
export async function DELETE() {
try {
const session = await getServerSession(authOptions);
if (!session?.user?.id) {
throw new AuthenticationError();
}
// 重置用户设置为默认值
const settings = await UserSettingsService.resetUserSettings(
session.user.id
);
return ApiResponseHandler.success(settings);
} catch (error) {
return ApiResponseHandler.error(error as Error);
}
}