const fsPromises = require('fs').promises; const fs = require('fs'); const path = require('path'); const XLSX = require('xlsx'); class FileManager { /** * Moves a file from sourcePath to destinationPath. * @param {string} sourcePath - Full path to the source file. * @param {string} destinationPath - Destination directory or full file path. */ async moveFile(sourcePath, destinationPath) { try { const destinationStat = await fsPromises.lstat(destinationPath).catch(() => null); const isDirectory = destinationStat && destinationStat.isDirectory(); const finalDestination = isDirectory ? path.join(destinationPath, path.basename(sourcePath)) : destinationPath; await fsPromises.rename(sourcePath, finalDestination); } catch (error) { throw error; } } readExcelFile(filePath) { if (!fs.existsSync(filePath)) { throw new Error(`Datei nicht gefunden: ${filePath}`); } const ext = path.extname(filePath).toLowerCase(); if (ext !== '.xlsx' && ext !== '.xls') { throw new Error(`Ungültiger Dateityp: ${ext}`); } const workbook = XLSX.readFile(filePath); const sheetName = workbook.SheetNames[0]; const worksheet = workbook.Sheets[sheetName]; const data = XLSX.utils.sheet_to_json(worksheet, { defval: null }); return data; } static async readJson(filePath) { try { const data = await fsPromises.readFile(filePath, 'utf-8'); return JSON.parse(data); } catch (error) { throw new Error(`Fehler beim Lesen der JSON-Datei: ${error.message}`); } } static async writeJson(filePath, data) { try { const jsonString = JSON.stringify(data, null, 4); // schön formatiert await fsPromises.writeFile(filePath, jsonString, 'utf-8'); } catch (error) { throw new Error(`Fehler beim Schreiben der JSON-Datei: ${error.message}`); } } static async copyFile(sourcePath, destinationPath) { try { await fsPromises.copyFile(sourcePath, destinationPath); //console.log(`Datei erfolgreich kopiert von ${sourcePath} nach ${destinationPath}`); } catch (error) { throw new Error(`Fehler beim Kopieren der Datei: ${error.message}`); } } static convertDateExcel (excelDate) { // Get the number of milliseconds from Unix epoch. const unixTime = (excelDate - 25569) * 86400 * 1000; return new Date(unixTime); } } module.exports = FileManager;