Improve input validation

This commit is contained in:
RatzzFatzz
2025-12-04 22:09:24 +01:00
parent 0f6bc271b1
commit 9ab417f71d
21 changed files with 239 additions and 177 deletions

View File

@@ -1,6 +1,6 @@
package at.pcgamingfreaks.mkvaudiosubtitlechanger.impl;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.config.Config;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.config.InputConfig;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.ResultStatistic;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.util.DateUtils;
import lombok.extern.slf4j.Slf4j;
@@ -31,11 +31,11 @@ public class FileFilter {
}
private static boolean hasMatchingPattern(File pathName) {
return Config.getInstance().getIncludePattern().matcher(pathName.getName()).matches();
return InputConfig.getInstance().getIncludePattern().matcher(pathName.getName()).matches();
}
private static boolean isNewer(File pathName) {
Config config = Config.getInstance();
InputConfig config = InputConfig.getInstance();
if (config.getFilterDate() == null) return true;
try {
BasicFileAttributes attributes = Files.readAttributes(pathName.toPath(), BasicFileAttributes.class);
@@ -47,6 +47,6 @@ public class FileFilter {
}
private static boolean isNewer(Date creationDate) {
return creationDate.toInstant().isAfter(Config.getInstance().getFilterDate().toInstant());
return creationDate.toInstant().isAfter(InputConfig.getInstance().getFilterDate().toInstant());
}
}

View File

@@ -1,6 +1,6 @@
package at.pcgamingfreaks.mkvaudiosubtitlechanger.impl;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.config.Config;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.config.InputConfig;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.exceptions.MkvToolNixException;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.*;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.util.SetUtils;
@@ -26,7 +26,7 @@ public class MkvFileProcessor implements FileProcessor {
private final ObjectMapper mapper = new ObjectMapper();
private static final SubtitleTrackComparator subtitleTrackComparator =
new SubtitleTrackComparator(Config.getInstance().getPreferredSubtitles().toArray(new String[0]));
new SubtitleTrackComparator(InputConfig.getInstance().getPreferredSubtitles().toArray(new String[0]));
private static final String DISABLE_DEFAULT_TRACK = "--edit track:%s --set flag-default=0";
private static final String ENABLE_DEFAULT_TRACK = "--edit track:%s --set flag-default=1";
@@ -40,7 +40,7 @@ public class MkvFileProcessor implements FileProcessor {
List<FileAttribute> fileAttributes = new ArrayList<>();
try {
String[] command = new String[]{
Config.getInstance().getPathFor(MkvToolNix.MKV_MERGE),
InputConfig.getInstance().getPathFor(MkvToolNix.MKV_MERGE),
"--identify",
"--identification-format",
"json",
@@ -132,7 +132,7 @@ public class MkvFileProcessor implements FileProcessor {
public List<FileAttribute> retrieveNonForcedTracks(List<FileAttribute> attributes) {
return attributes.stream()
.filter(elem -> !StringUtils.containsAnyIgnoreCase(elem.trackName(),
Config.getInstance().getForcedKeywords().toArray(new CharSequence[0])))
InputConfig.getInstance().getForcedKeywords().toArray(new CharSequence[0])))
.filter(elem -> !elem.forcedTrack())
.collect(Collectors.toList());
}
@@ -141,7 +141,7 @@ public class MkvFileProcessor implements FileProcessor {
public List<FileAttribute> retrieveNonCommentaryTracks(List<FileAttribute> attributes) {
return attributes.stream()
.filter(elem -> !StringUtils.containsAnyIgnoreCase(elem.trackName(),
Config.getInstance().getCommentaryKeywords().toArray(new CharSequence[0])))
InputConfig.getInstance().getCommentaryKeywords().toArray(new CharSequence[0])))
.collect(Collectors.toList());
}
@@ -151,7 +151,7 @@ public class MkvFileProcessor implements FileProcessor {
@Override
public void update(File file, FileInfo fileInfo) throws IOException, MkvToolNixException {
List<String> command = new ArrayList<>();
command.add(Config.getInstance().getPathFor(MkvToolNix.MKV_PROP_EDIT));
command.add(InputConfig.getInstance().getPathFor(MkvToolNix.MKV_PROP_EDIT));
command.add(String.format(file.getAbsolutePath()));
if (fileInfo.isAudioDifferent()) {

View File

@@ -1,6 +1,6 @@
package at.pcgamingfreaks.mkvaudiosubtitlechanger.impl.kernel;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.config.Config;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.config.InputConfig;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.exceptions.MkvToolNixException;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.impl.FileCollector;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.impl.FileProcessor;
@@ -38,7 +38,7 @@ public abstract class AttributeUpdaterKernel {
protected final FileCollector collector;
protected final FileProcessor processor;
protected final ResultStatistic statistic = ResultStatistic.getInstance();
private final ExecutorService executor = Executors.newFixedThreadPool(Config.getInstance().getThreads());
private final ExecutorService executor = Executors.newFixedThreadPool(InputConfig.getInstance().getThreads());
protected ProgressBarBuilder pbBuilder() {
return new ProgressBarBuilder()
@@ -52,7 +52,7 @@ public abstract class AttributeUpdaterKernel {
statistic.startTimer();
try (ProgressBar progressBar = pbBuilder().build()) {
List<File> files = loadFiles(Config.getInstance().getLibraryPath().getAbsolutePath());
List<File> files = loadFiles(InputConfig.getInstance().getLibraryPath().getAbsolutePath());
progressBar.maxHint(files.size());
files.forEach(file -> executor.submit(() -> {
@@ -71,7 +71,7 @@ public abstract class AttributeUpdaterKernel {
}
protected List<File> loadExcludedFiles() {
List<File> excludedFiles = Config.getInstance().getExcludedDirectories().stream()
List<File> excludedFiles = InputConfig.getInstance().getExcludedDirectories().stream()
.map(collector::loadFiles)
.flatMap(Collection::stream)
.collect(Collectors.toList());
@@ -111,7 +111,7 @@ public abstract class AttributeUpdaterKernel {
processor.detectDefaultTracks(fileInfo, attributes, nonForcedTracks);
processor.detectDesiredTracks(fileInfo, nonForcedTracks, nonCommentaryTracks,
Config.getInstance().getAttributeConfig().toArray(new AttributeConfig[]{}));
InputConfig.getInstance().getAttributeConfig().toArray(new AttributeConfig[]{}));
updateFile(fileInfo);
}
@@ -142,7 +142,7 @@ public abstract class AttributeUpdaterKernel {
}
private void commitChange(FileInfo fileInfo) {
if (Config.getInstance().isSafeMode()) {
if (InputConfig.getInstance().isSafeMode()) {
return;
}
@@ -157,7 +157,7 @@ public abstract class AttributeUpdaterKernel {
}
protected void endProcess() {
if (Config.getInstance().isSafeMode()) {
if (InputConfig.getInstance().isSafeMode()) {
return;
}
@@ -171,7 +171,7 @@ public abstract class AttributeUpdaterKernel {
if (!lastExecutionFile.exists()) lastExecutionFile.createNewFile();
YAML yaml = new YAML(lastExecutionFile);
yaml.set(Config.getInstance().getNormalizedLibraryPath(), DateUtils.convert(new Date()));
yaml.set(InputConfig.getInstance().getNormalizedLibraryPath(), DateUtils.convert(new Date()));
yaml.save(lastExecutionFile);
} catch (IOException | YamlInvalidContentException e) {
log.error("last-execution.yml could not be created or read.", e);

View File

@@ -1,6 +1,6 @@
package at.pcgamingfreaks.mkvaudiosubtitlechanger.impl.kernel;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.config.Config;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.config.InputConfig;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.impl.FileCollector;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.impl.FileProcessor;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.AttributeConfig;
@@ -32,7 +32,7 @@ public class CoherentAttributeUpdaterKernel extends AttributeUpdaterKernel {
*/
@Override
List<File> loadFiles(String path) {
return loadFiles(path, Config.getInstance().getCoherent());
return loadFiles(path, InputConfig.getInstance().getCoherent());
}
List<File> loadFiles(String path, int depth) {
@@ -53,7 +53,7 @@ public class CoherentAttributeUpdaterKernel extends AttributeUpdaterKernel {
/**
* Update files in directory, if possible, with the same {@link AttributeConfig}.
* If {@link Config#isForceCoherent()} then there will be no changes to the file if they don't match the same config.
* If {@link InputConfig#isForceCoherent()} then there will be no changes to the file if they don't match the same config.
* Otherwise, the default behaviour is executed.
* This method is called by the executor and is run in parallel.
*
@@ -61,7 +61,7 @@ public class CoherentAttributeUpdaterKernel extends AttributeUpdaterKernel {
*/
@Override
void process(File file) {
process(file, Config.getInstance().getCoherent());
process(file, InputConfig.getInstance().getCoherent());
}
void process(File file, int depth) {
@@ -71,7 +71,7 @@ public class CoherentAttributeUpdaterKernel extends AttributeUpdaterKernel {
.map(FileInfo::new)
.collect(Collectors.toList());
for (AttributeConfig config : Config.getInstance().getAttributeConfig()) {
for (AttributeConfig config : InputConfig.getInstance().getAttributeConfig()) {
for (FileInfo fileInfo : fileInfos) {
List<FileAttribute> attributes = processor.loadAttributes(fileInfo.getFile());
@@ -99,7 +99,7 @@ public class CoherentAttributeUpdaterKernel extends AttributeUpdaterKernel {
log.info("No coherent match found for {}", file.getAbsoluteFile());
for (FileInfo fileInfo : fileInfos) {
if (!Config.getInstance().isForceCoherent()) {
if (!InputConfig.getInstance().isForceCoherent()) {
super.process(fileInfo.getFile());
} else {
statistic.excluded();

View File

@@ -1,6 +1,6 @@
package at.pcgamingfreaks.mkvaudiosubtitlechanger.impl.kernel;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.config.Config;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.config.InputConfig;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.impl.FileCollector;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.impl.FileProcessor;
import lombok.extern.slf4j.Slf4j;
@@ -29,7 +29,7 @@ public class DefaultAttributeUpdaterKernel extends AttributeUpdaterKernel {
@Override
List<File> loadFiles(String path) {
List<File> excludedFiles = loadExcludedFiles();
return collector.loadFiles(Config.getInstance().getLibraryPath().getAbsolutePath()).stream()
return collector.loadFiles(InputConfig.getInstance().getLibraryPath().getAbsolutePath()).stream()
.filter(file -> !excludedFiles.contains(file))
.collect(Collectors.toList());
}