debugging metadata extraction

This commit is contained in:
stephen 2026-01-31 23:58:22 +11:00
parent 85c068399e
commit 9552846a0f
5 changed files with 5046 additions and 10 deletions

Binary file not shown.

File diff suppressed because it is too large Load diff

Binary file not shown.

2873
docs/notes_tmp.html Normal file

File diff suppressed because it is too large Load diff

View file

@ -18,7 +18,10 @@ import {
import { getEnrichedMetadataFromCloud } from "@/data-access/file-nodes"; import { getEnrichedMetadataFromCloud } from "@/data-access/file-nodes";
import { getFreshAccessToken } from "@/lib/auth-utils";
import { getOneDriveFileBuffer } from "@/services/onedrive"; // Import your working service
import { extractMetadata } from "@/lib/metadata-extractor";
/** /**
* 1. FETCH: Get all file nodes * 1. FETCH: Get all file nodes
* Now simply calls the DAL. Error handling is left to the caller (the UI). * Now simply calls the DAL. Error handling is left to the caller (the UI).
@ -117,22 +120,134 @@ export async function updateFileNodeAction(id: string, formData: FormData) {
} }
} }
// export async function getMetadataPreviewAction(fileId: string) {
// const session = await auth();
// if (!session?.user?.id) throw new Error("Unauthorized");
// try {
// console.log(`🔍 Starting enhancement for file: ${fileId}`);
// // This calls the DAL -> which calls the Service -> which calls OneDrive
// const data = await getEnrichedMetadataFromCloud(fileId);
// // This log will show you exactly what we found in your terminal!
// console.log("✅ Extracted Metadata Result:", data);
// return { success: true, data };
// } catch (error: any) {
// console.error("❌ Enhancement Action Error:", error.message);
// return { success: false, error: error.message };
// }
// }
/**
* 5. ENHANCE (Magic Fill): Extracts deep metadata from the actual file binary
*/
// export async function getMetadataPreviewAction(fileId: string) {
// const session = await auth();
// if (!session?.user?.id) throw new Error("Unauthorized");
// console.log(`\n--- 🔍 Magic Fill Started for File ID: ${fileId} ---`);
// try {
// // 1. Get the record from DB to get the oneDriveId and Name
// const node = await getFileNodeById(fileId);
// if (!node || !node.oneDriveId) {
// throw new Error("File not found or not synced with OneDrive");
// }
// // 2. Get fresh token
// const token = await getFreshAccessToken(session.user.id);
// // 3. Fetch the actual binary content from Microsoft Graph
// console.log(`📡 Fetching binary from Microsoft Graph...`);
// let response;
// try {
// response = await fetch(
// `https://graph.microsoftonline.com/v1.0/me/drive/items/${node.oneDriveId}/content`,
// {
// headers: { Authorization: `Bearer ${token}` },
// cache: 'no-store' // Ensure we aren't hitting a stale server cache
// }
// );
// } catch (err: any) {
// console.error("❌ THE ACTUAL NETWORK ERROR:");
// console.error("Message:", err.message);
// console.error("Cause/Stack:", err.cause || err.stack); // This is the gold mine
// throw new Error(`Server-side fetch failed: ${err.message}`);
// }
// if (!response.ok) {
// throw new Error(`Failed to fetch file content: ${response.statusText}`);
// }
// const arrayBuffer = await response.arrayBuffer();
// const buffer = Buffer.from(arrayBuffer);
// console.log(`📦 Downloaded ${buffer.length} bytes.`);
// // 4. Run the Metadata Utility
// const extractedData = await extractMetadata(buffer, node.name);
// // --- 🏁 THE TERMINAL LOG YOU REQUESTED ---
// console.log("✅ RAW DATA EXTRACTED FROM FILE:");
// console.dir(extractedData, { depth: null, colors: true });
// console.log(`--- 🏁 Magic Fill Finished ---\n`);
// return { success: true, data: extractedData };
// } catch (error: any) {
// console.error("❌ Magic Fill Error:", error.message);
// return { success: false, error: error.message };
// }
// }
export async function getMetadataPreviewAction(fileId: string) { export async function getMetadataPreviewAction(fileId: string) {
const session = await auth(); const session = await auth();
if (!session?.user?.id) throw new Error("Unauthorized"); if (!session?.user?.id) throw new Error("Unauthorized");
console.log(`\n--- 🔍 Magic Fill Started for File ID: ${fileId} ---`);
try { try {
console.log(`🔍 Starting enhancement for file: ${fileId}`); const node = await getFileNodeById(fileId);
if (!node || !node.oneDriveId) throw new Error("File not found");
// 1. Get the token from our utility
const token = await getFreshAccessToken(session.user.id);
// LOG: Just check the length to be sure it's not empty
console.log(`🔑 Token retrieved (Length: ${token.length})`);
console.log(`📡 Fetching binary for: ${node.name}...`);
// 2. Fetch the content from Microsoft Graph
const response = await fetch(
`https://graph.microsoft.com/v1.0/me/drive/items/${node.oneDriveId}/content`,
{
headers: {
'Authorization': `Bearer ${token}`,
'Accept': '*/*'
},
}
);
if (!response.ok) {
// If it fails here, we'll see the real reason from Microsoft
const errorText = await response.text();
console.error("❌ Microsoft Graph Error Response:", errorText);
throw new Error(`OneDrive Download Failed: ${response.status}`);
}
const arrayBuffer = await response.arrayBuffer();
const buffer = Buffer.from(arrayBuffer);
console.log(`📦 Success! Downloaded ${buffer.length} bytes.`);
// 3. Extract Metadata
const extractedData = await extractMetadata(buffer, node.name);
// This calls the DAL -> which calls the Service -> which calls OneDrive console.log("✅ RAW DATA EXTRACTED:");
const data = await getEnrichedMetadataFromCloud(fileId); console.dir(extractedData, { depth: null, colors: true });
// This log will show you exactly what we found in your terminal! return { success: true, data: extractedData };
console.log("✅ Extracted Metadata Result:", data);
return { success: true, data };
} catch (error: any) { } catch (error: any) {
console.error("❌ Enhancement Action Error:", error.message); console.error("❌ Magic Fill Error:", error.message);
return { success: false, error: error.message }; return { success: false, error: error.message };
} }
} }