Webdav
This commit is contained in:
74
backend/src/webdav/fill-file-sizes.ts
Normal file
74
backend/src/webdav/fill-file-sizes.ts
Normal file
@@ -0,0 +1,74 @@
|
||||
// scripts/fill-file-sizes.ts
|
||||
import 'dotenv/config';
|
||||
import { db } from '../../db';
|
||||
import { files } from '../../db/schema';
|
||||
import { eq, isNull } from 'drizzle-orm';
|
||||
import { HeadObjectCommand } from "@aws-sdk/client-s3";
|
||||
import { s3, initS3 } from '../utils/s3';
|
||||
import { loadSecrets, secrets } from '../utils/secrets';
|
||||
|
||||
async function migrate() {
|
||||
console.log("🚀 Starte Migration der Dateigrößen...");
|
||||
|
||||
// 1. Setup
|
||||
await loadSecrets();
|
||||
await initS3();
|
||||
|
||||
// 2. Alle Dateien holen, die noch keine Größe haben (oder alle, um sicherzugehen)
|
||||
// Wir nehmen erstmal ALLE, um sicherzustellen, dass alles stimmt.
|
||||
const allFiles = await db.select().from(files);
|
||||
|
||||
console.log(`📦 ${allFiles.length} Dateien in der Datenbank gefunden.`);
|
||||
|
||||
let successCount = 0;
|
||||
let errorCount = 0;
|
||||
|
||||
// 3. Loop durch alle Dateien
|
||||
for (const file of allFiles) {
|
||||
if (!file.path) {
|
||||
console.log(`⏭️ Überspringe Datei ${file.id} (Kein Pfad)`);
|
||||
continue;
|
||||
}
|
||||
|
||||
try {
|
||||
// S3 fragen (HeadObject lädt nur Metadaten, nicht die ganze Datei -> Schnell)
|
||||
const command = new HeadObjectCommand({
|
||||
Bucket: secrets.S3_BUCKET, // Oder secrets.S3_BUCKET_NAME je nach deiner Config
|
||||
Key: file.path
|
||||
});
|
||||
|
||||
const response = await s3.send(command);
|
||||
const size = response.ContentLength || 0;
|
||||
|
||||
// In DB speichern
|
||||
await db.update(files)
|
||||
.set({ size: size })
|
||||
.where(eq(files.id, file.id));
|
||||
|
||||
process.stdout.write("."); // Fortschrittsanzeige
|
||||
successCount++;
|
||||
|
||||
} catch (error: any) {
|
||||
process.stdout.write("X");
|
||||
// console.error(`\n❌ Fehler bei ${file.path}: ${error.name}`);
|
||||
|
||||
// Optional: Wenn Datei in S3 fehlt, könnten wir sie markieren oder loggen
|
||||
if (error.name === 'NotFound') {
|
||||
// console.error(` -> Datei existiert nicht im Bucket!`);
|
||||
}
|
||||
errorCount++;
|
||||
}
|
||||
}
|
||||
|
||||
console.log("\n\n------------------------------------------------");
|
||||
console.log(`✅ Fertig!`);
|
||||
console.log(`Updated: ${successCount}`);
|
||||
console.log(`Fehler: ${errorCount} (Meistens Dateien, die im Bucket fehlen)`);
|
||||
console.log("------------------------------------------------");
|
||||
process.exit(0);
|
||||
}
|
||||
|
||||
migrate().catch(err => {
|
||||
console.error("Fataler Fehler:", err);
|
||||
process.exit(1);
|
||||
});
|
||||
Reference in New Issue
Block a user