mirror of
https://github.com/RatzzFatzz/MKVAudioSubtitleChanger.git
synced 2026-02-10 17:55:57 +01:00
Make path of lastFileExecution platform dependent
This commit is contained in:
@@ -6,6 +6,10 @@ import at.pcgamingfreaks.mkvaudiosubtitlechanger.util.ProjectUtil;
|
|||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.apache.logging.log4j.Level;
|
import org.apache.logging.log4j.Level;
|
||||||
|
import org.apache.logging.log4j.LogManager;
|
||||||
|
import org.apache.logging.log4j.core.LoggerContext;
|
||||||
|
import org.apache.logging.log4j.core.appender.RollingFileAppender;
|
||||||
|
import org.apache.logging.log4j.core.config.Configuration;
|
||||||
import org.apache.logging.log4j.core.config.Configurator;
|
import org.apache.logging.log4j.core.config.Configurator;
|
||||||
import picocli.CommandLine;
|
import picocli.CommandLine;
|
||||||
|
|
||||||
@@ -39,7 +43,14 @@ public class CommandRunner implements Runnable {
|
|||||||
System.out.println("Safemode active. No files will be changed!");
|
System.out.println("Safemode active. No files will be changed!");
|
||||||
}
|
}
|
||||||
|
|
||||||
LastExecutionHandler lastExecutionHandler = config.isOnlyNewFiles() ? new LastExecutionHandler("./last-executions.yml") : null;
|
String userLocal = getLogDirectory();
|
||||||
|
if (userLocal == null) {
|
||||||
|
log.error("Could not load log4j2 log info");
|
||||||
|
System.out.println("Could not load log4j2 log info");
|
||||||
|
System.exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
LastExecutionHandler lastExecutionHandler = config.isOnlyNewFiles() ? new LastExecutionHandler(userLocal) : null;
|
||||||
FileFilter fileFilter = new FileFilter(config.getExcluded(), config.getIncludePattern(), config.getFilterDate(), lastExecutionHandler);
|
FileFilter fileFilter = new FileFilter(config.getExcluded(), config.getIncludePattern(), config.getFilterDate(), lastExecutionHandler);
|
||||||
FileProcessor fileProcessor = new CachedFileProcessor(new MkvFileProcessor(config.getMkvToolNix(), fileFilter));
|
FileProcessor fileProcessor = new CachedFileProcessor(new MkvFileProcessor(config.getMkvToolNix(), fileFilter));
|
||||||
AttributeChangeProcessor attributeChangeProcessor = new AttributeChangeProcessor(config.getPreferredSubtitles().toArray(new String[0]), config.getForcedKeywords(), config.getCommentaryKeywords(), config.getHearingImpaired());
|
AttributeChangeProcessor attributeChangeProcessor = new AttributeChangeProcessor(config.getPreferredSubtitles().toArray(new String[0]), config.getForcedKeywords(), config.getCommentaryKeywords(), config.getHearingImpaired());
|
||||||
@@ -49,4 +60,19 @@ public class CommandRunner implements Runnable {
|
|||||||
: new SingleFileAttributeUpdater(config, fileProcessor, attributeChangeProcessor, lastExecutionHandler);
|
: new SingleFileAttributeUpdater(config, fileProcessor, attributeChangeProcessor, lastExecutionHandler);
|
||||||
kernel.execute();
|
kernel.execute();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String getLogDirectory() {
|
||||||
|
LoggerContext context = (LoggerContext) LogManager.getContext(false);
|
||||||
|
Configuration config = context.getConfiguration();
|
||||||
|
|
||||||
|
for (org.apache.logging.log4j.core.Appender appender : config.getAppenders().values()) {
|
||||||
|
if (appender instanceof RollingFileAppender rollingFileAppender) {
|
||||||
|
String fileName = rollingFileAppender.getFileName();
|
||||||
|
return new java.io.File(fileName).getParentFile().getParent();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
log.warn("No file appender found in configuration");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,11 +11,12 @@ import java.util.Properties;
|
|||||||
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public class LastExecutionHandler {
|
public class LastExecutionHandler {
|
||||||
|
private static final String FILE_NAME = "last-execution.properties";
|
||||||
private final File file;
|
private final File file;
|
||||||
private final Properties lastFileExecution;
|
private final Properties lastFileExecution;
|
||||||
|
|
||||||
public LastExecutionHandler(String path) {
|
public LastExecutionHandler(String path) {
|
||||||
file = new File(path);
|
file = new File(path + "/" + FILE_NAME);
|
||||||
lastFileExecution = loadLastFileExecution(file);
|
lastFileExecution = loadLastFileExecution(file);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -24,7 +25,7 @@ public class LastExecutionHandler {
|
|||||||
try (FileInputStream in = new FileInputStream(file)) {
|
try (FileInputStream in = new FileInputStream(file)) {
|
||||||
properties.load(in);
|
properties.load(in);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
log.warn("Couldn't find or read {}", file.getPath(), e);
|
log.info("Couldn't find or read {}", file.getPath(), e);
|
||||||
}
|
}
|
||||||
return properties;
|
return properties;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,62 +11,63 @@ import java.time.Instant;
|
|||||||
import static org.junit.jupiter.api.Assertions.*;
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
class LastExecutionHandlerTest {
|
class LastExecutionHandlerTest {
|
||||||
private static final String LAST_EXECUTION_YML = "./last-execution.properties";
|
private static final String LAST_EXECUTION_PATH = ".";
|
||||||
|
private static final String LAST_EXECUTION_FILE = "./last-execution.properties";
|
||||||
private static final String TEST_MKV_FILE = "/arst/file.mkv";
|
private static final String TEST_MKV_FILE = "/arst/file.mkv";
|
||||||
|
|
||||||
@AfterEach
|
@AfterEach
|
||||||
void destruct() {
|
void destruct() {
|
||||||
File file = new File(LAST_EXECUTION_YML);
|
File file = new File(LAST_EXECUTION_FILE);
|
||||||
if (file.exists()) file.delete();
|
if (file.exists()) file.delete();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void missingFile() throws IOException {
|
void missingFile() throws IOException {
|
||||||
LastExecutionHandler underTest = new LastExecutionHandler(LAST_EXECUTION_YML);
|
LastExecutionHandler underTest = new LastExecutionHandler(LAST_EXECUTION_PATH);
|
||||||
assertNull(underTest.get(TEST_MKV_FILE));
|
assertNull(underTest.get(TEST_MKV_FILE));
|
||||||
underTest.update(TEST_MKV_FILE);
|
underTest.update(TEST_MKV_FILE);
|
||||||
assertNotNull(underTest.get(TEST_MKV_FILE));
|
assertNotNull(underTest.get(TEST_MKV_FILE));
|
||||||
underTest.persist();
|
underTest.persist();
|
||||||
File file = new File(LAST_EXECUTION_YML);
|
File file = new File(LAST_EXECUTION_FILE);
|
||||||
assertTrue(file.exists());
|
assertTrue(file.exists());
|
||||||
assertTrue(Files.readString(file.toPath()).contains(TEST_MKV_FILE + "="));
|
assertTrue(Files.readString(file.toPath()).contains(TEST_MKV_FILE + "="));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void emptyFile() throws IOException {
|
void emptyFile() throws IOException {
|
||||||
File file = new File(LAST_EXECUTION_YML);
|
File file = new File(LAST_EXECUTION_FILE);
|
||||||
file.createNewFile();
|
file.createNewFile();
|
||||||
missingFile(); // does the checks needed for empty file case
|
missingFile(); // does the checks needed for empty file case
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void existingFileNoChanges() throws IOException {
|
void existingFileNoChanges() throws IOException {
|
||||||
File file = new File(LAST_EXECUTION_YML);
|
File file = new File(LAST_EXECUTION_FILE);
|
||||||
file.createNewFile();
|
file.createNewFile();
|
||||||
Files.writeString(file.toPath(), TEST_MKV_FILE + "=" + Instant.now());
|
Files.writeString(file.toPath(), TEST_MKV_FILE + "=" + Instant.now());
|
||||||
String expected = Files.readString(file.toPath()).replace(":", "\\:");
|
String expected = Files.readString(file.toPath()).replace(":", "\\:");
|
||||||
|
|
||||||
LastExecutionHandler underTest = new LastExecutionHandler(LAST_EXECUTION_YML);
|
LastExecutionHandler underTest = new LastExecutionHandler(LAST_EXECUTION_PATH);
|
||||||
assertNotNull(underTest.get(TEST_MKV_FILE));
|
assertNotNull(underTest.get(TEST_MKV_FILE));
|
||||||
underTest.persist();
|
underTest.persist();
|
||||||
File file1 = new File(LAST_EXECUTION_YML);
|
File file1 = new File(LAST_EXECUTION_FILE);
|
||||||
assertTrue(file1.exists());
|
assertTrue(file1.exists());
|
||||||
assertTrue(Files.readString(file.toPath()).contains(expected), "File contains expected value");
|
assertTrue(Files.readString(file.toPath()).contains(expected), "File contains expected value");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void existingFileWithChanges() throws IOException {
|
void existingFileWithChanges() throws IOException {
|
||||||
File file = new File(LAST_EXECUTION_YML);
|
File file = new File(LAST_EXECUTION_FILE);
|
||||||
file.createNewFile();
|
file.createNewFile();
|
||||||
Files.writeString(file.toPath(), TEST_MKV_FILE + "=" + Instant.now());
|
Files.writeString(file.toPath(), TEST_MKV_FILE + "=" + Instant.now());
|
||||||
String expected = Files.readString(file.toPath());
|
String expected = Files.readString(file.toPath());
|
||||||
|
|
||||||
LastExecutionHandler underTest = new LastExecutionHandler(LAST_EXECUTION_YML);
|
LastExecutionHandler underTest = new LastExecutionHandler(LAST_EXECUTION_PATH);
|
||||||
assertNotNull(underTest.get(TEST_MKV_FILE));
|
assertNotNull(underTest.get(TEST_MKV_FILE));
|
||||||
underTest.update(TEST_MKV_FILE);
|
underTest.update(TEST_MKV_FILE);
|
||||||
assertNotNull(underTest.get(TEST_MKV_FILE));
|
assertNotNull(underTest.get(TEST_MKV_FILE));
|
||||||
underTest.persist();
|
underTest.persist();
|
||||||
File file1 = new File(LAST_EXECUTION_YML);
|
File file1 = new File(LAST_EXECUTION_FILE);
|
||||||
assertTrue(file1.exists());
|
assertTrue(file1.exists());
|
||||||
assertNotEquals(expected, Files.readString(file.toPath()));
|
assertNotEquals(expected, Files.readString(file.toPath()));
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user