diff --git a/push.sh b/push.sh new file mode 100644 index 0000000..3cc8888 --- /dev/null +++ b/push.sh @@ -0,0 +1,2 @@ +docker build -t xeviff/sonarr_queue_fix:latest . +docker push xeviff/sonarr_queue_fix:latest diff --git a/src/main/java/tv/mangrana/worker/FailedImportFixer.java b/src/main/java/tv/mangrana/worker/FailedImportFixer.java index e0fd9ee..5b8f2a2 100644 --- a/src/main/java/tv/mangrana/worker/FailedImportFixer.java +++ b/src/main/java/tv/mangrana/worker/FailedImportFixer.java @@ -14,33 +14,46 @@ 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 final Path torrentPath; + private final Path seriePath; + private final FileCopier fileCopier; + private Path seasonPath; + + static Factory factory() { + return new Factory(); + } + public static class Factory { + FailedImportFixer newFixerFor(Record queueRecord, SonarrSerie serie){ + return new FailedImportFixer(queueRecord, serie); + } + } private FailedImportFixer(Record queueRecord, SonarrSerie serie) { this.queueRecord = queueRecord; - this.serie = serie; - } - - static FailedImportFixer of(Record queueRecord, SonarrSerie serie) { - return new FailedImportFixer(queueRecord, serie); + this.torrentPath = Path.of(queueRecord.getOutputPath()); + this.seriePath = Path.of(serie.getPath()); + fileCopier = new FileCopier(); } void fix() throws IOException { System.out.printf("%nfixing: %s%n" ,queueRecord.getTitle()); - System.out.printf(">> located in: %s%n", queueRecord.getOutputPath()); - - var torrentPath = Path.of(queueRecord.getOutputPath()); - List torrentFiles = getVideoFilesFrom(torrentPath); - - var seasonFolderName = calculateSeasonPathFrom(torrentPath); - var sonarPath = Path.of(serie.getPath()); - List sonarFiles = getVideoFilesFrom(sonarPath.resolve(seasonFolderName)); - - missingFilesDetector.printDifferencesBetween(torrentFiles, sonarFiles); + List torrentFiles = resolveTorrentFiles(); + List sonarFiles = resolveSonarrFiles(); + List filesToCopy = missingFilesDetector.getMissingFilesAtDestination(torrentFiles, sonarFiles); + filesToCopy.forEach(this::copy); } - private String calculateSeasonPathFrom(Path torrentPath) { + private List resolveTorrentFiles() throws IOException { + return getVideoFilesFrom(Path.of(queueRecord.getOutputPath())); + } + + private List resolveSonarrFiles() throws IOException { + seasonPath = seriePath.resolve(getSeasonFolder()); + return getVideoFilesFrom(seasonPath); + } + + private String getSeasonFolder() { try { return StringCaptor.getSeasonFolderNameFromSeason(torrentPath.getFileName().toString()); } catch (IncorrectWorkingReferencesException e) { @@ -58,4 +71,9 @@ public class FailedImportFixer { } } + private void copy(Path fileToCopy) { + Path target = seasonPath.resolve(fileToCopy.getFileName()); + System.out.println("** going to hardlink file to "+target); + fileCopier.hardLink(fileToCopy, target); + } } diff --git a/src/main/java/tv/mangrana/worker/FileCopier.java b/src/main/java/tv/mangrana/worker/FileCopier.java index 6436901..d1e0d95 100644 --- a/src/main/java/tv/mangrana/worker/FileCopier.java +++ b/src/main/java/tv/mangrana/worker/FileCopier.java @@ -5,8 +5,7 @@ import java.nio.file.Files; import java.nio.file.Path; public class FileCopier { - - private void hardLink(Path source, Path destination) { + void hardLink(Path source, Path destination) { try { Files.createLink(destination, source); } catch (IOException e) { @@ -16,9 +15,10 @@ public class FileCopier { } } } + class ProjectPath { static Path of(String path) { String projectPath = System.getProperty("user.dir"); - return Path.of(projectPath+path); + return Path.of(projectPath + path); } } diff --git a/src/main/java/tv/mangrana/worker/MissingFilesDetector.java b/src/main/java/tv/mangrana/worker/MissingFilesDetector.java index 8848d22..92ec98f 100644 --- a/src/main/java/tv/mangrana/worker/MissingFilesDetector.java +++ b/src/main/java/tv/mangrana/worker/MissingFilesDetector.java @@ -7,20 +7,19 @@ import java.util.stream.Collectors; public class MissingFilesDetector { - void printDifferencesBetween(List torrentFiles, List sonarrFiles) { + private Map> sonarrFilesByLength; + + List getMissingFilesAtDestination(List torrentFiles, List sonarrFiles) { System.out.printf("going to compare %d torrent files with %d sonar files%n", torrentFiles.size(), sonarrFiles.size()); Map> torrentFileLengths = getFileLengthsMapFrom(torrentFiles); if (hasFilesWithSameSize(torrentFileLengths)) - return; - Map> sonarrFileLengths = getFileLengthsMapFrom(sonarrFiles); - if (hasFilesWithSameSize(sonarrFileLengths)) - return; - - torrentFileLengths.keySet().stream() - .filter(fileSize -> missingAtDestination(fileSize, sonarrFileLengths)) - .collect(Collectors.toSet()); + return torrentFiles; + sonarrFilesByLength = getFileLengthsMapFrom(sonarrFiles); + if (hasFilesWithSameSize(sonarrFilesByLength)) + return torrentFiles; + return resolveMissingFiles(torrentFileLengths); } Map> getFileLengthsMapFrom(List files) { @@ -38,14 +37,24 @@ public class MissingFilesDetector { boolean hasMultipleElements(List 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 %d. Name: %s %n", + System.out.printf("!!! There is more than one file with the same size of %d. Name: %s %n", sampleFile.length(), sampleFile.getName()); return true; } return false; } - boolean missingAtDestination(Long fileSize, Map> torrentFileLengths) { - return !torrentFileLengths.containsKey(fileSize); + private List resolveMissingFiles(Map> torrentFilesByLength) { + return torrentFilesByLength.entrySet() + .stream() + .filter(this::missingAtDestination) + .map(Map.Entry::getValue) + .flatMap(List::stream) + .toList(); } + + private boolean missingAtDestination(Map.Entry> torrentFileEntry) { + return !sonarrFilesByLength.containsKey(torrentFileEntry.getKey()); + } + } \ No newline at end of file diff --git a/src/main/java/tv/mangrana/worker/QueueFixer.java b/src/main/java/tv/mangrana/worker/QueueFixer.java index 6eef005..d38ac81 100644 --- a/src/main/java/tv/mangrana/worker/QueueFixer.java +++ b/src/main/java/tv/mangrana/worker/QueueFixer.java @@ -15,10 +15,11 @@ import java.util.stream.Collectors; public class QueueFixer { final static String IMPORT_FAILURE_BECAUSE_MATCHED_BY_ID = "Found matching series via grab history, but release was matched to series by ID. Automatic import is not possible. See the FAQ for details."; private final SonarrApiGateway sonarrApiGateway; - private FailedImportFixer failedImportFixer; + private final FailedImportFixer.Factory failedImportFixerFactory; QueueFixer() { sonarrApiGateway = Sonarr.api(); + failedImportFixerFactory = FailedImportFixer.factory(); } void fix() { @@ -57,8 +58,9 @@ public class QueueFixer { SonarrSerie serie = getSerieFromSonarr(seriesId); if (serie == null) return; - failedImportFixer = FailedImportFixer.of(record, serie); - failedImportFixer.fix(); + failedImportFixerFactory + .newFixerFor(record, serie) + .fix(); } catch (IOException e) { System.out.printf("!! could not fix the import %s%n", record.getTitle()); e.printStackTrace();