124_webcalibre2/src/app/upload/_actions.ts

76 lines
2.8 KiB
TypeScript
Raw Normal View History

'use server';
import { auth } from "@/auth";
import { getFreshAccessToken } from "@/lib/auth-utils";
import { prisma } from "@/lib/prisma";
import { revalidatePath } from "next/cache";
2026-01-12 11:53:20 +00:00
// createFolderAction remains the same...
export async function uploadFileAction(formData: FormData) {
const session = await auth();
if (!session?.user?.id) throw new Error("Unauthorized");
const file = formData.get("file") as File;
const description = formData.get("description") as string || "";
const parentId = formData.get("parentId") as string | null;
2026-01-12 11:53:20 +00:00
// Parse the dynamic metadata from the client
const customMetadataRaw = formData.get("customMetadata") as string;
const customMetadata = customMetadataRaw ? JSON.parse(customMetadataRaw) : {};
if (!file) throw new Error("No file selected");
const accessToken = await getFreshAccessToken(session.user.id);
const rootFolder = "WebCalibre";
const internalId = crypto.randomUUID();
2026-01-12 11:53:20 +00:00
// 1. Create OneDrive Storage Folder
const createSubFolderRes = await fetch(`https://graph.microsoft.com/v1.0/me/drive/root:/${rootFolder}:/children`, {
method: "POST",
2026-01-12 11:53:20 +00:00
headers: { Authorization: `Bearer ${accessToken}`, "Content-Type": "application/json" },
body: JSON.stringify({ name: internalId, folder: {} })
});
2026-01-12 11:53:20 +00:00
if (!createSubFolderRes.ok) throw new Error("Storage directory creation failed");
const subFolderData = await createSubFolderRes.json();
2026-01-12 11:53:20 +00:00
// 2. Upload Session & File Transfer (Existing logic is fine)
const sessionUrl = `https://graph.microsoft.com/v1.0/me/drive/items/${subFolderData.id}:/${encodeURIComponent(file.name)}:/createUploadSession`;
const sessionRes = await fetch(sessionUrl, {
method: "POST",
2026-01-12 11:53:20 +00:00
headers: { Authorization: `Bearer ${accessToken}`, "Content-Type": "application/json" },
body: JSON.stringify({ item: { "@microsoft.graph.conflictBehavior": "replace" } })
});
const { uploadUrl } = await sessionRes.json();
const buffer = Buffer.from(await file.arrayBuffer());
2026-01-12 11:53:20 +00:00
await fetch(uploadUrl, {
method: "PUT",
2026-01-12 11:53:20 +00:00
headers: { "Content-Length": `${file.size}`, "Content-Range": `bytes 0-${file.size - 1}/${file.size}` },
body: buffer
});
2026-01-12 11:53:20 +00:00
// 3. Final Database Record with Merged Metadata
const extension = file.name.split('.').pop()?.toUpperCase() || "UNKNOWN";
await prisma.fileNode.create({
data: {
2026-01-12 11:53:20 +00:00
id: internalId,
name: file.name,
description: description,
size: BigInt(file.size),
isFolder: false,
path: `/${rootFolder}/${internalId}/${file.name}`,
ownerId: session.user.id,
2026-01-12 11:53:20 +00:00
parentId: parentId || null,
metadata: {
2026-01-12 11:53:20 +00:00
...customMetadata, // User's dynamic keys (Latitude, Author, etc.)
type: extension, // System keys (preserved for UI icons)
mimeType: file.type
}
}
});
revalidatePath("/dashboard");
return { success: true };
}