diff --git a/app/src/api.js b/app/src/api.js
index b84713c6..e49a6223 100644
--- a/app/src/api.js
+++ b/app/src/api.js
@@ -425,6 +425,11 @@ export async function getConfig(fileName) {
return (await response.json())['config'];
}
+export async function getConfigText(fileName) {
+ const response = await apiGet(`${API_URI}/config?file_name=${fileName}`);
+ return (await response.json())['config_text'];
+}
+
export async function getConfigBackups(fileName) {
const response = await apiGet(`${API_URI}/config/backups?file_name=${fileName}`);
if (response.ok) {
diff --git a/app/src/components/admin/Configs.js b/app/src/components/admin/Configs.js
index 46427fd5..1519ea80 100644
--- a/app/src/components/admin/Configs.js
+++ b/app/src/components/admin/Configs.js
@@ -3,7 +3,7 @@ import {Button, Header, Icon, Loader, Modal, Popup, Table} from "../Theme";
import React, {useContext, useEffect, useState} from "react";
import {TableBody, TableCell, TableHeader, TableHeaderCell, TableRow} from "semantic-ui-react";
import {TableRowPlaceholder} from "../Placeholder";
-import {getConfig, getConfigBackups, postConfigBackupImport, postConfigBackupPreview} from "../../api";
+import {getConfigText, getConfigBackups, postConfigBackupImport, postConfigBackupPreview} from "../../api";
import {ThemeContext} from "../../contexts/contexts";
function WarningIcon({ok}) {
@@ -300,7 +300,7 @@ function ConfigContentModal({open, onClose, fileName}) {
let cancelled = false;
setLoading(true);
setContent(null);
- getConfig(fileName).then(result => {
+ getConfigText(fileName).then(result => {
if (cancelled) return;
setContent(result);
setLoading(false);
@@ -318,7 +318,7 @@ function ConfigContentModal({open, onClose, fileName}) {
{loading
?
{JSON.stringify(content, null, 2)}
+ ? {content}
: Failed to load config contents.
} diff --git a/modules/archive/lib.py b/modules/archive/lib.py index c380fc25..50ec046d 100644 --- a/modules/archive/lib.py +++ b/modules/archive/lib.py @@ -175,7 +175,11 @@ class DomainsConfig(ConfigFile): # Use to_config to export each collection collections_data = [collection.to_config() for collection in collections] - self._config['collections'] = collections_data + if not collections_data and self._config.get('collections'): + logger.warning('Refusing to save empty domains config because domains exist in the config file!') + return + + self._config['collections'] = collections_data logger.info(f'Dumping {len(collections_data)} domain collections to config') self.save(file, send_events, overwrite) diff --git a/modules/archive/test/test_domain_tagging.py b/modules/archive/test/test_domain_tagging.py index d2d4124f..f82eb029 100644 --- a/modules/archive/test/test_domain_tagging.py +++ b/modules/archive/test/test_domain_tagging.py @@ -1,6 +1,7 @@ """Tests for domain collection tagging functionality.""" import pytest +import yaml from wrolpi.collections import Collection @@ -316,3 +317,43 @@ async def test_untag_domain_without_directory_saves_config(test_session, test_di domain_entry = next((c for c in data.get('collections', []) if c['name'] == 'example.com'), None) assert domain_entry is not None, "Domain collection not found in config" assert domain_entry.get('tag_name') is None, "Tag should be removed from config" + + +@pytest.mark.asyncio +async def test_dump_domains_config_does_not_wipe_config(test_session, test_directory, async_client): + """Dumping domains config when DB is empty must not overwrite an existing config file that has domains. + + Regression test: a fresh DB triggering dump_config would write collections: [] + to the config file, permanently wiping the domain definitions. + """ + from modules.archive.lib import DomainsConfig + from wrolpi.common import get_media_directory + + # Create domain collection and dump config. + domain_dir = get_media_directory() / 'archives' / 'example.com' + domain_dir.mkdir(parents=True, exist_ok=True) + collection = Collection(name='example.com', kind='domain', directory=domain_dir) + test_session.add(collection) + test_session.commit() + + config_file = test_directory / 'config' / 'domains.yaml' + config_file.parent.mkdir(parents=True, exist_ok=True) + domains_config = DomainsConfig() + domains_config.dump_config(config_file, overwrite=True) + + data = yaml.safe_load(config_file.read_text()) + assert len(data['collections']) == 1, 'Config should have a domain' + + # Simulate a fresh DB by deleting all domain collections. + test_session.query(Collection).filter_by(kind='domain').delete() + test_session.commit() + + # Re-initialize the config from the file so in-memory has the collections. + domains_config.initialize() + file_data = yaml.safe_load(config_file.read_text()) + assert len(file_data['collections']) == 1, 'File should still have domains before dump' + + # Dumping with an empty DB should NOT wipe the config file. + domains_config.dump_config(config_file) + data = yaml.safe_load(config_file.read_text()) + assert len(data['collections']) == 1, 'Config was wiped by dumping with empty DB!' diff --git a/modules/videos/lib.py b/modules/videos/lib.py index 09285e0a..1d97e9cf 100644 --- a/modules/videos/lib.py +++ b/modules/videos/lib.py @@ -799,6 +799,10 @@ def save_channels_config(): logger.warning('Refusing to save channels config because test did not initialize a test config!') return + if not config.get('channels') and channels_config.channels: + logger.warning('Refusing to save empty channels config because channels exist in the config file!') + return + channels_config.update(config) logger.info('save_channels_config completed') diff --git a/modules/videos/test/test_common.py b/modules/videos/test/test_common.py index f08f0cbe..86607c66 100644 --- a/modules/videos/test/test_common.py +++ b/modules/videos/test/test_common.py @@ -450,6 +450,36 @@ async def test_import_channel_download_comments(await_switches, test_session, ch assert channel.download_missing_data is False +@pytest.mark.asyncio +async def test_save_channels_config_does_not_wipe_config(await_switches, test_session, channel_factory, + test_channels_config): + """Saving channels config when DB is empty must not overwrite an existing config file that has channels. + + Regression test: a fresh DB (e.g. after repair) triggering save_channels_config would write channels: [] + to the config file, permanently wiping the channel definitions. + """ + # Create a channel and save it to the config file. + channel = channel_factory() + test_session.commit() + save_channels_config() + await await_switches() + assert test_channels_config.is_file() + config_text = test_channels_config.read_text() + assert 'channels: []' not in config_text, 'Config should have a channel' + + # Simulate a fresh DB by deleting all channels. + test_session.query(Channel).delete() + test_session.query(Collection).filter_by(kind='channel').delete() + test_session.commit() + assert test_session.query(Channel).count() == 0 + + # Saving with an empty DB should NOT wipe the config file. + save_channels_config() + await await_switches() + config_text = test_channels_config.read_text() + assert 'channels: []' not in config_text, 'Config was wiped by saving with empty DB!' + + @pytest.mark.asyncio async def test_ffprobe_json(async_client, video_file, corrupted_video_file): content = await common.ffprobe_json(video_file) diff --git a/wrolpi/config_api.py b/wrolpi/config_api.py index 357e8f93..cfa233db 100644 --- a/wrolpi/config_api.py +++ b/wrolpi/config_api.py @@ -17,7 +17,9 @@ config_bp = Blueprint('config', '/api/config') def get_config(request: Request): if file_name := request.args.get('file_name'): config = get_config_by_file_name(file_name) - return json_response(dict(config=config)) + config_file = config.get_file() + config_text = config_file.read_text() if config_file.is_file() else None + return json_response(dict(config=config, config_text=config_text)) else: configs = get_all_configs() configs = {k: v.config_status() for k, v in configs.items()}