124_webcalibre2/src/app/dashboard/sync-actions.ts

95 lines
2.8 KiB
TypeScript
Raw Normal View History

'use server';
import { auth } from "@/auth";
import { prisma } from "@/lib/prisma";
import { revalidatePath } from "next/cache";
import { getFreshAccessToken } from "@/lib/auth-utils";
export async function syncOneDrive() {
const session = await auth();
if (!session?.user?.id) throw new Error("Unauthorized");
try {
const accessToken = await getFreshAccessToken(session.user.id);
if (!accessToken) {
throw new Error("Microsoft account not linked properly or session expired.");
}
const response = await fetch("https://graph.microsoft.com/v1.0/me/drive/root:/WebCalibre:/children", {
headers: {
Authorization: `Bearer ${accessToken}`,
"Content-Type": "application/json"
},
});
if (!response.ok) {
const errorData = await response.json();
if (response.status === 404) {
return { success: true, count: 0 };
}
throw new Error(errorData.error?.message || "Failed to fetch data from OneDrive");
}
const data = await response.json();
let syncedCount = 0;
// Regular expression to identify if a string is a UUID
const uuidRegex = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
for (const item of data.value) {
const isFolder = !!item.folder;
/**
* FILTER: If the item is a folder and the name is a UUID, skip it.
* These are storage containers created by the upload action, not user-facing folders.
*/
if (isFolder && uuidRegex.test(item.name)) {
continue;
}
const extension = isFolder
? 'FOLDER'
: (item.name.split('.').pop()?.toUpperCase() || 'UNKNOWN');
const fileSize = item.size ? BigInt(item.size) : BigInt(0);
await prisma.fileNode.upsert({
where: { oneDriveId: item.id },
update: {
name: item.name,
size: fileSize,
isFolder: isFolder,
metadata: {
type: extension,
mimeType: item.file?.mimeType || null
},
path: item.parentReference?.path + '/' + item.name,
updatedAt: new Date(),
},
create: {
id: crypto.randomUUID(),
oneDriveId: item.id,
name: item.name,
size: fileSize,
isFolder: isFolder,
path: item.parentReference?.path + '/' + item.name,
ownerId: session.user.id,
metadata: {
type: extension,
mimeType: item.file?.mimeType || null
},
}
});
syncedCount++;
}
revalidatePath('/dashboard');
return { success: true, count: syncedCount };
} catch (error: any) {
console.error("Sync Process Failure:", error.message);
throw new Error(error.message || "An unexpected error occurred during sync.");
}
}