Migrate last execution handler to properties

This commit is contained in:
RatzzFatzz
2026-01-12 19:02:06 +01:00
parent 8dbfb22ed8
commit 3c57eb44ef
5 changed files with 48 additions and 53 deletions

View File

@@ -1,7 +1,6 @@
package at.pcgamingfreaks.mkvaudiosubtitlechanger.impl;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.ResultStatistic;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.util.DateUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
@@ -9,6 +8,7 @@ import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.attribute.BasicFileAttributes;
import java.time.Instant;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@@ -53,21 +53,21 @@ public class FileFilter {
}
private boolean isNewer(File pathName, Date date) {
return isNewer(pathName, date.toInstant());
}
private boolean isNewer(File pathName, Instant date) {
if (date == null) return true;
try {
BasicFileAttributes attributes = Files.readAttributes(pathName.toPath(), BasicFileAttributes.class);
return isNewer(DateUtils.convert(attributes.creationTime().toMillis()), date)
|| isNewer(DateUtils.convert(attributes.lastModifiedTime().toMillis()), date);
return attributes.creationTime().toInstant().isAfter(date)
|| attributes.lastModifiedTime().toInstant().isAfter(date);
} catch (IOException e) {
log.warn("File attributes could not be read", e);
}
return true;
}
private boolean isNewer(Date creationDate, Date compareDate) {
return creationDate.toInstant().isAfter(compareDate.toInstant());
}
private boolean isExcluded(File pathName, Set<String> excludedDirs) {
if (excludedDirs.contains(pathName.getPath())) return true;
@@ -89,7 +89,7 @@ public class FileFilter {
}
private boolean isNewOrChanged(File pathname) {
Date lastExecutionDate = lastExecutionHandler.get(pathname.getAbsolutePath());
Instant lastExecutionDate = lastExecutionHandler.get(pathname.getAbsolutePath());
return lastExecutionDate == null || isNewer(pathname, lastExecutionDate);
}
}

View File

@@ -1,59 +1,53 @@
package at.pcgamingfreaks.mkvaudiosubtitlechanger.impl;
import at.pcgamingfreaks.yaml.YAML;
import at.pcgamingfreaks.yaml.YamlInvalidContentException;
import at.pcgamingfreaks.yaml.YamlKeyNotFoundException;
import lombok.extern.slf4j.Slf4j;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.time.Instant;
import java.util.Date;
import java.util.Properties;
@Slf4j
public class LastExecutionHandler {
private final File file;
private YAML lastFileExecution;
private final Properties lastFileExecution;
public LastExecutionHandler(String path) {
file = new File(path);
try {
lastFileExecution = loadLastFileExecution(file);
} catch (YamlInvalidContentException | IOException e) {
log.warn("Couldn't find or read {}", path, e);
}
lastFileExecution = loadLastFileExecution(file);
}
public YAML loadLastFileExecution(File file) throws YamlInvalidContentException, IOException {
if (file.exists() && file.isFile()) {
return new YAML(file);
public Properties loadLastFileExecution(File file) {
Properties properties = new Properties();
try (FileInputStream in = new FileInputStream(file)) {
properties.load(in);
} catch (IOException e) {
log.warn("Couldn't find or read {}", file.getPath(), e);
}
return new YAML("");
return properties;
}
public Date get(String path) {
if (!lastFileExecution.isSet(path)) return null;
try {
return Date.from(Instant.parse(lastFileExecution.getString(path)));
} catch (YamlKeyNotFoundException e) {
throw new RuntimeException(e);
}
public Instant get(String path) {
if (!lastFileExecution.containsKey(path)) return null;
return Instant.parse(lastFileExecution.getProperty(path));
}
public void update(String path) {
update(path, Date.from(Instant.now()));
update(path, Instant.now());
}
public void update(String path, Date execution) {
public void update(String path, Instant execution) {
if (lastFileExecution == null) return;
lastFileExecution.set(path, execution.toInstant());
lastFileExecution.put(path, execution.toString());
}
public void persist() {
try {
lastFileExecution.save(file);
try (FileOutputStream out = new FileOutputStream(file)) {
lastFileExecution.store(out, "MKVAudioSubtitleChanger - Last file execution");
} catch (IOException e) {
log.warn("", e);
log.warn("Persisting last file execution dates failed", e);
}
}
}

View File

@@ -5,7 +5,7 @@ import at.pcgamingfreaks.mkvaudiosubtitlechanger.impl.FileFilter;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.core.util.IOUtils;
import org.apache.commons.io.IOUtils;
import tools.jackson.databind.ObjectMapper;
import java.io.File;

View File

@@ -53,7 +53,7 @@ public class InputConfig implements CommandLine.IVersionProvider {
private boolean forceCoherent;
// TODO: implement usage
@Option(names = {"-n", "--only-new-file"}, description = "ignores all files unchanged and previously processed")
@Option(names = {"-n", "--only-new-files"}, description = "ignores all files unchanged and previously processed")
private boolean onlyNewFiles;
@Option(names = {"-d", "--filter-date"}, defaultValue = Option.NULL_VALUE, description = "only consider files created newer than entered date (format: \"dd.MM.yyyy-HH:mm:ss\")")
private Date filterDate;

View File

@@ -11,24 +11,25 @@ import java.time.Instant;
import static org.junit.jupiter.api.Assertions.*;
class LastExecutionHandlerTest {
private static final String LAST_EXECUTION_YML = "./last-execution.yml";
private static final String LAST_EXECUTION_YML = "./last-execution.properties";
private static final String TEST_MKV_FILE = "/arst/file.mkv";
@AfterEach
void destruct() {
File file = new File("./last-execution.yml");
File file = new File(LAST_EXECUTION_YML);
if (file.exists()) file.delete();
}
@Test
void missingFile() throws IOException {
LastExecutionHandler underTest = new LastExecutionHandler("./last-execution.yml");
assertNull(underTest.get("/arst"));
underTest.update("/arst");
assertNotNull(underTest.get("/arst"));
LastExecutionHandler underTest = new LastExecutionHandler(LAST_EXECUTION_YML);
assertNull(underTest.get(TEST_MKV_FILE));
underTest.update(TEST_MKV_FILE);
assertNotNull(underTest.get(TEST_MKV_FILE));
underTest.persist();
File file = new File("./last-execution.yml");
File file = new File(LAST_EXECUTION_YML);
assertTrue(file.exists());
assertTrue(Files.readString(file.toPath()).contains("/arst: "));
assertTrue(Files.readString(file.toPath()).contains(TEST_MKV_FILE + "="));
}
@Test
@@ -42,28 +43,28 @@ class LastExecutionHandlerTest {
void existingFileNoChanges() throws IOException {
File file = new File(LAST_EXECUTION_YML);
file.createNewFile();
Files.writeString(file.toPath(), "/arst: \"" + Instant.now() + "\"");
String expected = Files.readString(file.toPath());
Files.writeString(file.toPath(), TEST_MKV_FILE + "=" + Instant.now());
String expected = Files.readString(file.toPath()).replace(":", "\\:");
LastExecutionHandler underTest = new LastExecutionHandler(LAST_EXECUTION_YML);
assertNotNull(underTest.get("/arst"));
assertNotNull(underTest.get(TEST_MKV_FILE));
underTest.persist();
File file1 = new File(LAST_EXECUTION_YML);
assertTrue(file1.exists());
assertEquals(expected, Files.readString(file.toPath()));
assertTrue(Files.readString(file.toPath()).contains(expected), "File contains expected value");
}
@Test
void existingFileWithChanges() throws IOException {
File file = new File(LAST_EXECUTION_YML);
file.createNewFile();
Files.writeString(file.toPath(), "/arst: \"" + Instant.now() + "\"");
Files.writeString(file.toPath(), TEST_MKV_FILE + "=" + Instant.now());
String expected = Files.readString(file.toPath());
LastExecutionHandler underTest = new LastExecutionHandler(LAST_EXECUTION_YML);
assertNotNull(underTest.get("/arst"));
underTest.update("/arst");
assertNotNull(underTest.get("/arst"));
assertNotNull(underTest.get(TEST_MKV_FILE));
underTest.update(TEST_MKV_FILE);
assertNotNull(underTest.get(TEST_MKV_FILE));
underTest.persist();
File file1 = new File(LAST_EXECUTION_YML);
assertTrue(file1.exists());