diff --git a/server.js b/server.js index 1afa700..56a442b 100644 --- a/server.js +++ b/server.js @@ -5,7 +5,8 @@ const express = require('express'); const path = require('path'); -const fs = require('fs').promises; +const fsPromises = require('fs').promises; +const fs = require('fs'); // Regular fs module for synchronous operations const bodyParser = require('body-parser'); const cors = require('cors'); const morgan = require('morgan'); @@ -215,7 +216,7 @@ async function loadConfig() { try { // Try to read existing config from primary location console.log(`Trying to load config from: ${DEFAULT_CONFIG_PATH}`); - const configData = await fs.readFile(DEFAULT_CONFIG_PATH, 'utf8'); + const configData = await fsPromises.readFile(DEFAULT_CONFIG_PATH, 'utf8'); const loadedConfig = JSON.parse(configData); // Use recursive merge function to merge configs @@ -236,7 +237,7 @@ async function loadConfig() { console.log(`Config not found at ${DEFAULT_CONFIG_PATH}, trying fallback location...`); try { - const fallbackData = await fs.readFile(FALLBACK_CONFIG_PATH, 'utf8'); + const fallbackData = await fsPromises.readFile(FALLBACK_CONFIG_PATH, 'utf8'); const fallbackConfig = JSON.parse(fallbackData); // Merge configs @@ -251,8 +252,8 @@ async function loadConfig() { // Try to save to primary location, but don't fail if we can't try { // Create directory if it doesn't exist - await fs.mkdir(path.dirname(DEFAULT_CONFIG_PATH), { recursive: true }); - await fs.writeFile(DEFAULT_CONFIG_PATH, JSON.stringify(mergedConfig, null, 2), 'utf8'); + await fsPromises.mkdir(path.dirname(DEFAULT_CONFIG_PATH), { recursive: true }); + await fsPromises.writeFile(DEFAULT_CONFIG_PATH, JSON.stringify(mergedConfig, null, 2), 'utf8'); console.log(`Migrated config from ${FALLBACK_CONFIG_PATH} to ${DEFAULT_CONFIG_PATH}`); } catch (saveError) { console.warn(`Could not save config to ${DEFAULT_CONFIG_PATH}: ${saveError.message}`); @@ -268,15 +269,15 @@ async function loadConfig() { // Try to save to primary location first try { - await fs.mkdir(path.dirname(DEFAULT_CONFIG_PATH), { recursive: true }); - await fs.writeFile(DEFAULT_CONFIG_PATH, JSON.stringify(defaultConfig, null, 2), 'utf8'); + await fsPromises.mkdir(path.dirname(DEFAULT_CONFIG_PATH), { recursive: true }); + await fsPromises.writeFile(DEFAULT_CONFIG_PATH, JSON.stringify(defaultConfig, null, 2), 'utf8'); console.log(`Created default config at ${DEFAULT_CONFIG_PATH}`); } catch (saveError) { console.warn(`Could not save config to ${DEFAULT_CONFIG_PATH}: ${saveError.message}`); console.warn('Saving to fallback location instead'); // Save to fallback location instead - await fs.writeFile(FALLBACK_CONFIG_PATH, JSON.stringify(defaultConfig, null, 2), 'utf8'); + await fsPromises.writeFile(FALLBACK_CONFIG_PATH, JSON.stringify(defaultConfig, null, 2), 'utf8'); console.log(`Created default config at ${FALLBACK_CONFIG_PATH}`); } @@ -351,8 +352,8 @@ async function saveConfig(config) { // Always try to save to the primary config location first try { // Make sure directory exists - await fs.mkdir(path.dirname(DEFAULT_CONFIG_PATH), { recursive: true }); - await fs.writeFile(DEFAULT_CONFIG_PATH, JSON.stringify(config, null, 2), 'utf8'); + await fsPromises.mkdir(path.dirname(DEFAULT_CONFIG_PATH), { recursive: true }); + await fsPromises.writeFile(DEFAULT_CONFIG_PATH, JSON.stringify(config, null, 2), 'utf8'); console.log(`Configuration saved to ${DEFAULT_CONFIG_PATH}`); return; } catch (primaryError) { @@ -360,7 +361,7 @@ async function saveConfig(config) { console.warn('Trying fallback location...'); // If we couldn't save to the primary location, try the fallback - await fs.writeFile(FALLBACK_CONFIG_PATH, JSON.stringify(config, null, 2), 'utf8'); + await fsPromises.writeFile(FALLBACK_CONFIG_PATH, JSON.stringify(config, null, 2), 'utf8'); console.log(`Configuration saved to fallback location: ${FALLBACK_CONFIG_PATH}`); } } catch (error) { @@ -386,8 +387,8 @@ async function startServer() { config.securitySettings?.sslKeyPath) { try { const sslOptions = { - key: await fs.readFile(config.securitySettings.sslKeyPath), - cert: await fs.readFile(config.securitySettings.sslCertPath) + key: await fsPromises.readFile(config.securitySettings.sslKeyPath), + cert: await fsPromises.readFile(config.securitySettings.sslCertPath) }; server = https.createServer(sslOptions, app); @@ -1086,17 +1087,17 @@ async function getMediaLibrary(searchQuery) { try { // Check if directory exists - await fs.access(destinationPath); + await fsPromises.access(destinationPath); // Get directory listing - const files = await fs.readdir(destinationPath, { withFileTypes: true }); + const files = await fsPromises.readdir(destinationPath, { withFileTypes: true }); // Process each file/directory for (const file of files) { const fullPath = path.join(destinationPath, file.name); try { - const stats = await fs.stat(fullPath); + const stats = await fsPromises.stat(fullPath); // Create an item object const item = { @@ -1298,7 +1299,7 @@ app.get('/api/system/check-updates', authenticateJWT, async (req, res) => { isGitAvailable = true; // Then check if directory is a git repository - isGitRepo = await fs.access(path.join(__dirname, '.git')) + isGitRepo = await fsPromises.access(path.join(__dirname, '.git')) .then(() => true) .catch(() => false); } catch (error) { @@ -1413,7 +1414,7 @@ app.post('/api/system/update', authenticateJWT, async (req, res) => { isGitAvailable = true; // Then check if directory is a git repository - isGitRepo = await fs.access(path.join(__dirname, '.git')) + isGitRepo = await fsPromises.access(path.join(__dirname, '.git')) .then(() => true) .catch(() => false); } catch (error) {