1
0
Fork 0

introduce MissingFilesDetector

This commit is contained in:
Xavier Fontanet 2024-05-19 17:26:52 +02:00
parent d69712e905
commit 978a80b9dc
2 changed files with 54 additions and 13 deletions

View File

@ -7,13 +7,13 @@ import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class FailedImportFixer {
public static final int MINIMUM_FILE_SIZE_TO_BE_CONSIDERED_A_VIDEO = 300000;
private final Record queueRecord;
private final SonarrSerie serie;
private final MissingFilesDetector missingFilesDetector = new MissingFilesDetector();
private FailedImportFixer(Record queueRecord, SonarrSerie serie) {
this.queueRecord = queueRecord;
@ -29,11 +29,11 @@ public class FailedImportFixer {
System.out.printf(">> located in: %s%n", queueRecord.getOutputPath());
var torrentPath = Path.of(queueRecord.getOutputPath());
List<Path> torrentFiles = getVideoFilesFrom(torrentPath);
var sonarPath = Path.of(serie.getPath());
List<Path> torrentFiles = getVideoFilesFrom(torrentPath);
List<Path> sonarFiles = getVideoFilesFrom(sonarPath);
printDifferencesBetween(torrentFiles, sonarFiles);
missingFilesDetector.printDifferencesBetween(torrentFiles, sonarFiles);
}
private List<Path> getVideoFilesFrom(Path torrentPath) throws IOException {
@ -45,14 +45,4 @@ public class FailedImportFixer {
}
}
private void printDifferencesBetween(List<Path> torrentFiles, List<Path> sonarrFiles) {
Map<Long, List<Path>> torrentFileLengths = getFileLengthsMapFrom(torrentFiles);
Map<Long, List<Path>> sonarrFileLengths = getFileLengthsMapFrom(sonarrFiles);
//TODO checks & prints
}
private Map<Long, List<Path>> getFileLengthsMapFrom(List<Path> files) {
return files.stream()
.collect(Collectors.groupingBy(p -> p.toFile().length()));
}
}

View File

@ -0,0 +1,51 @@
package tv.mangrana.worker;
import java.nio.file.Path;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class MissingFilesDetector {
void printDifferencesBetween(List<Path> torrentFiles, List<Path> sonarrFiles) {
Map<Long, List<Path>> torrentFileLengths = getFileLengthsMapFrom(torrentFiles);
if (hasFilesWithSameSize(torrentFileLengths))
return;
Map<Long, List<Path>> sonarrFileLengths = getFileLengthsMapFrom(sonarrFiles);
if (hasFilesWithSameSize(sonarrFileLengths))
return;
torrentFileLengths.keySet().forEach(fileSize ->
takeDecisionForComparing(fileSize, torrentFileLengths));
}
Map<Long, List<Path>> getFileLengthsMapFrom(List<Path> files) {
return files.stream()
.collect(Collectors.groupingBy(p -> p.toFile().length()));
}
boolean hasFilesWithSameSize(Map<Long, List<Path>> torrentFileLengths) {
return torrentFileLengths
.values()
.stream()
.anyMatch(this::hasMultipleElements);
}
boolean hasMultipleElements(List<Path> paths) {
if (paths.size() > 1) {
var sampleFile = paths.get(0).toFile();
System.out.printf("There is more than one file with the same size of %s. Name: %s %n",
sampleFile.length(), sampleFile.getName());
return true;
}
return false;
}
void takeDecisionForComparing(Long fileSize, Map<Long, List<Path>> torrentFileLengths) {
var alreadyExists = torrentFileLengths.containsKey(fileSize);
if (!alreadyExists) {
System.out.printf("- needs to copy %s%n", torrentFileLengths.get(fileSize));
}
}
}