Add basic date filter

This commit is contained in:
2023-04-02 20:11:42 +02:00
parent 943308dd59
commit 7ee51421e0
6 changed files with 93 additions and 2 deletions

View File

@@ -2,18 +2,56 @@ package at.pcgamingfreaks.mkvaudiosubtitlechanger.impl;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.config.Config;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.ResultStatistic;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.util.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.Date;
@Slf4j
public class FileFilter {
static boolean accept(File pathName, String[] fileExtensions) {
if (StringUtils.endsWithAny(pathName.getAbsolutePath().toLowerCase(), fileExtensions)
&& Config.getInstance().getIncludePattern().matcher(pathName.getName()).matches()) {
if (hasProperFileExtension(pathName, fileExtensions) && isIncluded(pathName) && isNewer(pathName)) {
return true;
}
ResultStatistic.getInstance().excluded();
return false;
}
private static boolean hasProperFileExtension(File pathName, String[] fileExtensions) {
return StringUtils.endsWithAny(pathName.getAbsolutePath().toLowerCase(), fileExtensions);
}
private static boolean isIncluded(File pathName) {
return Config.getInstance().getIncludePattern().matcher(pathName.getName()).matches();
}
private static boolean isNewer(File pathName) {
Config config = Config.getInstance();
if (config.getFilterDate() == null) return true;
try {
BasicFileAttributes attributes = Files.readAttributes(pathName.toPath(), BasicFileAttributes.class);
return isNewer(DateUtils.convert(attributes.creationTime().toMillis()));
} catch (IOException e) {
log.warn("File attributes could not be read. This could have XX reason"); // TODO
}
return true;
}
private static boolean isNewer(Date creationDate) {
return creationDate.toInstant().isAfter(Config.getInstance().getFilterDate().toInstant());
}
private static boolean isNewerThanLastExecution(File pathName) {
if (Config.getInstance().isOnlyNewFiles()) {
return isNewer(pathName);
}
return true;
// return Config.getInstance().isOnlyNewFiles() && isNewer(pathName);
}
}