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,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);
}
}
}