mirror of
https://github.com/RatzzFatzz/MKVAudioSubtitleChanger.git
synced 2026-02-11 02:05:56 +01:00
Migrate last execution handler to properties
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user