2026-02-14 00:48:55 +00:00
|
|
|
import 'dotenv/config';
|
|
|
|
|
import { prisma } from '../src/lib/prisma';
|
|
|
|
|
import * as nodeCrypto from 'crypto';
|
|
|
|
|
|
|
|
|
|
function generateFileHash(buffer: Buffer): string {
|
2026-02-15 04:10:58 +00:00
|
|
|
return nodeCrypto.createHash('sha256').update(buffer).digest('hex');
|
2026-02-14 00:48:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Replace this with your actual OneDrive download logic!
|
|
|
|
|
*/
|
|
|
|
|
async function getFromOneDrive(oneDriveId: string): Promise<Buffer> {
|
|
|
|
|
// For now, let's pretend we downloaded it to test the DB update
|
|
|
|
|
// DELETE THESE 2 LINES when you add your real OneDrive fetch code:
|
|
|
|
|
console.log(` ⬇️ Downloading ${oneDriveId}...`);
|
|
|
|
|
return Buffer.from(`mock-data-for-${oneDriveId}`);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function backfill() {
|
|
|
|
|
console.log('🏁 Starting backfill with Prisma Adapter...');
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const files = await prisma.fileNode.findMany({
|
2026-02-15 04:10:58 +00:00
|
|
|
where: { isFolder: false
|
|
|
|
|
},
|
2026-02-14 00:48:55 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
console.log(`📂 Found ${files.length} files to process.`);
|
|
|
|
|
|
|
|
|
|
for (const file of files) {
|
|
|
|
|
try {
|
|
|
|
|
process.stdout.write(`Processing: ${file.name}... `);
|
|
|
|
|
|
|
|
|
|
// 1. Get the file content
|
|
|
|
|
const buffer = await getFromOneDrive(file.oneDriveId!);
|
|
|
|
|
|
|
|
|
|
// 2. Generate the hash
|
|
|
|
|
const hash = generateFileHash(buffer);
|
|
|
|
|
|
|
|
|
|
// 3. Update the database
|
|
|
|
|
await prisma.fileNode.update({
|
|
|
|
|
where: { id: file.id },
|
|
|
|
|
data: { hash: hash }
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
console.log(`✅ Success! (Hash: ${hash.substring(0, 8)}...)`);
|
|
|
|
|
} catch (err) {
|
|
|
|
|
console.log(`❌ Failed: ${err instanceof Error ? err.message : err}`);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error('🚨 Script Error:', error);
|
|
|
|
|
} finally {
|
|
|
|
|
await prisma.$disconnect();
|
|
|
|
|
console.log('🏁 Finished.');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
backfill();
|