mirror of
https://github.com/RatzzFatzz/MKVAudioSubtitleChanger.git
synced 2026-02-10 17:55:57 +01:00
Migrate last execution handler to properties
This commit is contained in:
@@ -1,7 +1,6 @@
|
|||||||
package at.pcgamingfreaks.mkvaudiosubtitlechanger.impl;
|
package at.pcgamingfreaks.mkvaudiosubtitlechanger.impl;
|
||||||
|
|
||||||
import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.ResultStatistic;
|
import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.ResultStatistic;
|
||||||
import at.pcgamingfreaks.mkvaudiosubtitlechanger.util.DateUtils;
|
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
@@ -9,6 +8,7 @@ import java.io.File;
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.attribute.BasicFileAttributes;
|
import java.nio.file.attribute.BasicFileAttributes;
|
||||||
|
import java.time.Instant;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.regex.Matcher;
|
import java.util.regex.Matcher;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
@@ -53,21 +53,21 @@ public class FileFilter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private boolean isNewer(File pathName, Date date) {
|
private boolean isNewer(File pathName, Date date) {
|
||||||
|
return isNewer(pathName, date.toInstant());
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean isNewer(File pathName, Instant date) {
|
||||||
if (date == null) return true;
|
if (date == null) return true;
|
||||||
try {
|
try {
|
||||||
BasicFileAttributes attributes = Files.readAttributes(pathName.toPath(), BasicFileAttributes.class);
|
BasicFileAttributes attributes = Files.readAttributes(pathName.toPath(), BasicFileAttributes.class);
|
||||||
return isNewer(DateUtils.convert(attributes.creationTime().toMillis()), date)
|
return attributes.creationTime().toInstant().isAfter(date)
|
||||||
|| isNewer(DateUtils.convert(attributes.lastModifiedTime().toMillis()), date);
|
|| attributes.lastModifiedTime().toInstant().isAfter(date);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
log.warn("File attributes could not be read", e);
|
log.warn("File attributes could not be read", e);
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean isNewer(Date creationDate, Date compareDate) {
|
|
||||||
return creationDate.toInstant().isAfter(compareDate.toInstant());
|
|
||||||
}
|
|
||||||
|
|
||||||
private boolean isExcluded(File pathName, Set<String> excludedDirs) {
|
private boolean isExcluded(File pathName, Set<String> excludedDirs) {
|
||||||
if (excludedDirs.contains(pathName.getPath())) return true;
|
if (excludedDirs.contains(pathName.getPath())) return true;
|
||||||
|
|
||||||
@@ -89,7 +89,7 @@ public class FileFilter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private boolean isNewOrChanged(File pathname) {
|
private boolean isNewOrChanged(File pathname) {
|
||||||
Date lastExecutionDate = lastExecutionHandler.get(pathname.getAbsolutePath());
|
Instant lastExecutionDate = lastExecutionHandler.get(pathname.getAbsolutePath());
|
||||||
return lastExecutionDate == null || isNewer(pathname, lastExecutionDate);
|
return lastExecutionDate == null || isNewer(pathname, lastExecutionDate);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,59 +1,53 @@
|
|||||||
package at.pcgamingfreaks.mkvaudiosubtitlechanger.impl;
|
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 lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
import java.io.FileInputStream;
|
||||||
|
import java.io.FileOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.time.Instant;
|
import java.time.Instant;
|
||||||
import java.util.Date;
|
import java.util.Properties;
|
||||||
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public class LastExecutionHandler {
|
public class LastExecutionHandler {
|
||||||
private final File file;
|
private final File file;
|
||||||
private YAML lastFileExecution;
|
private final Properties lastFileExecution;
|
||||||
|
|
||||||
public LastExecutionHandler(String path) {
|
public LastExecutionHandler(String path) {
|
||||||
file = new File(path);
|
file = new File(path);
|
||||||
try {
|
lastFileExecution = loadLastFileExecution(file);
|
||||||
lastFileExecution = loadLastFileExecution(file);
|
|
||||||
} catch (YamlInvalidContentException | IOException e) {
|
|
||||||
log.warn("Couldn't find or read {}", path, e);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public YAML loadLastFileExecution(File file) throws YamlInvalidContentException, IOException {
|
public Properties loadLastFileExecution(File file) {
|
||||||
if (file.exists() && file.isFile()) {
|
Properties properties = new Properties();
|
||||||
return new YAML(file);
|
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) {
|
public Instant get(String path) {
|
||||||
if (!lastFileExecution.isSet(path)) return null;
|
if (!lastFileExecution.containsKey(path)) return null;
|
||||||
try {
|
return Instant.parse(lastFileExecution.getProperty(path));
|
||||||
return Date.from(Instant.parse(lastFileExecution.getString(path)));
|
|
||||||
} catch (YamlKeyNotFoundException e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void update(String 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;
|
if (lastFileExecution == null) return;
|
||||||
lastFileExecution.set(path, execution.toInstant());
|
lastFileExecution.put(path, execution.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
public void persist() {
|
public void persist() {
|
||||||
try {
|
try (FileOutputStream out = new FileOutputStream(file)) {
|
||||||
lastFileExecution.save(file);
|
lastFileExecution.store(out, "MKVAudioSubtitleChanger - Last file execution");
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
log.warn("", e);
|
log.warn("Persisting last file execution dates failed", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ import at.pcgamingfreaks.mkvaudiosubtitlechanger.impl.FileFilter;
|
|||||||
import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.*;
|
import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.*;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
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 tools.jackson.databind.ObjectMapper;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
|||||||
@@ -53,7 +53,7 @@ public class InputConfig implements CommandLine.IVersionProvider {
|
|||||||
private boolean forceCoherent;
|
private boolean forceCoherent;
|
||||||
|
|
||||||
// TODO: implement usage
|
// 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;
|
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\")")
|
@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;
|
private Date filterDate;
|
||||||
|
|||||||
@@ -11,24 +11,25 @@ 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.yml";
|
private static final String LAST_EXECUTION_YML = "./last-execution.properties";
|
||||||
|
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_YML);
|
||||||
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_YML);
|
||||||
assertNull(underTest.get("/arst"));
|
assertNull(underTest.get(TEST_MKV_FILE));
|
||||||
underTest.update("/arst");
|
underTest.update(TEST_MKV_FILE);
|
||||||
assertNotNull(underTest.get("/arst"));
|
assertNotNull(underTest.get(TEST_MKV_FILE));
|
||||||
underTest.persist();
|
underTest.persist();
|
||||||
File file = new File("./last-execution.yml");
|
File file = new File(LAST_EXECUTION_YML);
|
||||||
assertTrue(file.exists());
|
assertTrue(file.exists());
|
||||||
assertTrue(Files.readString(file.toPath()).contains("/arst: "));
|
assertTrue(Files.readString(file.toPath()).contains(TEST_MKV_FILE + "="));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -42,28 +43,28 @@ class LastExecutionHandlerTest {
|
|||||||
void existingFileNoChanges() throws IOException {
|
void existingFileNoChanges() throws IOException {
|
||||||
File file = new File(LAST_EXECUTION_YML);
|
File file = new File(LAST_EXECUTION_YML);
|
||||||
file.createNewFile();
|
file.createNewFile();
|
||||||
Files.writeString(file.toPath(), "/arst: \"" + Instant.now() + "\"");
|
Files.writeString(file.toPath(), TEST_MKV_FILE + "=" + Instant.now());
|
||||||
String expected = Files.readString(file.toPath());
|
String expected = Files.readString(file.toPath()).replace(":", "\\:");
|
||||||
|
|
||||||
LastExecutionHandler underTest = new LastExecutionHandler(LAST_EXECUTION_YML);
|
LastExecutionHandler underTest = new LastExecutionHandler(LAST_EXECUTION_YML);
|
||||||
assertNotNull(underTest.get("/arst"));
|
assertNotNull(underTest.get(TEST_MKV_FILE));
|
||||||
underTest.persist();
|
underTest.persist();
|
||||||
File file1 = new File(LAST_EXECUTION_YML);
|
File file1 = new File(LAST_EXECUTION_YML);
|
||||||
assertTrue(file1.exists());
|
assertTrue(file1.exists());
|
||||||
assertEquals(expected, Files.readString(file.toPath()));
|
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_YML);
|
||||||
file.createNewFile();
|
file.createNewFile();
|
||||||
Files.writeString(file.toPath(), "/arst: \"" + 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_YML);
|
||||||
assertNotNull(underTest.get("/arst"));
|
assertNotNull(underTest.get(TEST_MKV_FILE));
|
||||||
underTest.update("/arst");
|
underTest.update(TEST_MKV_FILE);
|
||||||
assertNotNull(underTest.get("/arst"));
|
assertNotNull(underTest.get(TEST_MKV_FILE));
|
||||||
underTest.persist();
|
underTest.persist();
|
||||||
File file1 = new File(LAST_EXECUTION_YML);
|
File file1 = new File(LAST_EXECUTION_YML);
|
||||||
assertTrue(file1.exists());
|
assertTrue(file1.exists());
|
||||||
|
|||||||
Reference in New Issue
Block a user