feat: add aliyun oss storage config
This commit is contained in:
53
src/app/api/v1/storage/config/route.ts
Normal file
53
src/app/api/v1/storage/config/route.ts
Normal file
@@ -0,0 +1,53 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { requireRequestSession } from "@/lib/boss-auth";
|
||||
import {
|
||||
getAttachmentStorageConfig,
|
||||
upsertAttachmentStorageConfig,
|
||||
} from "@/lib/boss-data";
|
||||
import {
|
||||
type AttachmentStorageConfigPatch,
|
||||
applyAttachmentStorageConfigPatch,
|
||||
} from "@/lib/boss-storage-config";
|
||||
import {
|
||||
normalizeStorageError,
|
||||
sanitizeAttachmentStorageConfig,
|
||||
} from "@/lib/boss-storage";
|
||||
|
||||
export const runtime = "nodejs";
|
||||
|
||||
export async function GET(request: NextRequest) {
|
||||
const session = await requireRequestSession(request);
|
||||
if (!session) {
|
||||
return NextResponse.json({ ok: false, message: "UNAUTHORIZED" }, { status: 401 });
|
||||
}
|
||||
|
||||
const config = await getAttachmentStorageConfig(session.account);
|
||||
return NextResponse.json({
|
||||
ok: true,
|
||||
config: sanitizeAttachmentStorageConfig(config),
|
||||
});
|
||||
}
|
||||
|
||||
export async function PATCH(request: NextRequest) {
|
||||
const session = await requireRequestSession(request);
|
||||
if (!session) {
|
||||
return NextResponse.json({ ok: false, message: "UNAUTHORIZED" }, { status: 401 });
|
||||
}
|
||||
|
||||
const body = (await request.json()) as AttachmentStorageConfigPatch;
|
||||
|
||||
try {
|
||||
const existing = await getAttachmentStorageConfig(session.account);
|
||||
const nextConfig = await applyAttachmentStorageConfigPatch(existing, body);
|
||||
const saved = await upsertAttachmentStorageConfig(nextConfig);
|
||||
return NextResponse.json({
|
||||
ok: true,
|
||||
config: sanitizeAttachmentStorageConfig(saved),
|
||||
});
|
||||
} catch (error) {
|
||||
return NextResponse.json(
|
||||
{ ok: false, message: normalizeStorageError(error) },
|
||||
{ status: 400 },
|
||||
);
|
||||
}
|
||||
}
|
||||
49
src/app/api/v1/storage/config/validate/route.ts
Normal file
49
src/app/api/v1/storage/config/validate/route.ts
Normal file
@@ -0,0 +1,49 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { requireRequestSession } from "@/lib/boss-auth";
|
||||
import {
|
||||
getAttachmentStorageConfig,
|
||||
upsertAttachmentStorageConfig,
|
||||
} from "@/lib/boss-data";
|
||||
import {
|
||||
type AttachmentStorageConfigPatch,
|
||||
applyAttachmentStorageConfigPatch,
|
||||
} from "@/lib/boss-storage-config";
|
||||
import {
|
||||
normalizeStorageError,
|
||||
sanitizeAttachmentStorageConfig,
|
||||
validateAttachmentStorageConfig,
|
||||
} from "@/lib/boss-storage";
|
||||
|
||||
export const runtime = "nodejs";
|
||||
|
||||
export async function POST(request: NextRequest) {
|
||||
const session = await requireRequestSession(request);
|
||||
if (!session) {
|
||||
return NextResponse.json({ ok: false, message: "UNAUTHORIZED" }, { status: 401 });
|
||||
}
|
||||
|
||||
const body = (await request.json().catch(() => ({}))) as AttachmentStorageConfigPatch;
|
||||
|
||||
try {
|
||||
const existing = await getAttachmentStorageConfig(session.account);
|
||||
const draft = await applyAttachmentStorageConfigPatch(existing, body);
|
||||
const result = await validateAttachmentStorageConfig(draft);
|
||||
const saved = await upsertAttachmentStorageConfig({
|
||||
...draft,
|
||||
validatedAt: new Date().toISOString(),
|
||||
});
|
||||
return NextResponse.json({
|
||||
ok: true,
|
||||
provider: result.provider,
|
||||
bucket: result.bucket,
|
||||
endpoint: result.endpoint,
|
||||
region: result.region,
|
||||
config: sanitizeAttachmentStorageConfig(saved),
|
||||
});
|
||||
} catch (error) {
|
||||
return NextResponse.json(
|
||||
{ ok: false, message: normalizeStorageError(error) },
|
||||
{ status: 400 },
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user