1
0
Fork 0

bugfixing, error handling and refinements

This commit is contained in:
xeviff 2023-01-10 14:48:06 +01:00
parent c5bf89fb4d
commit 083e33de9b
6 changed files with 42 additions and 20 deletions

View File

@ -21,6 +21,8 @@ import java.io.InputStreamReader;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import static tv.mangrana.utils.Output.log;
public class GoogleDriveUtils { public class GoogleDriveUtils {
private static final String APPLICATION_NAME = "Google Drive API Java Quickstart"; private static final String APPLICATION_NAME = "Google Drive API Java Quickstart";
@ -76,6 +78,7 @@ public class GoogleDriveUtils {
// //
_driveService = new Drive.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential) // _driveService = new Drive.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential) //
.setApplicationName(APPLICATION_NAME).build(); .setApplicationName(APPLICATION_NAME).build();
log("Google Drive service initialized");
return _driveService; return _driveService;
} }

View File

@ -0,0 +1,7 @@
package tv.mangrana.exception;
public class JobFileNotMovedException extends Exception{
public JobFileNotMovedException(String e) {
super(e);
}
}

View File

@ -1,6 +1,7 @@
package tv.mangrana.jobs; package tv.mangrana.jobs;
import tv.mangrana.config.LocalEnvironmentManager; import tv.mangrana.config.LocalEnvironmentManager;
import tv.mangrana.exception.JobFileNotMovedException;
import java.io.File; import java.io.File;
@ -45,17 +46,17 @@ public abstract class JobFile<E> {
} }
public void markDoing() { public void markDoing() throws JobFileNotMovedException {
if (jobFile.getAbsolutePath().contains(PATH_TODO.getFolderName())) { if (jobFile.getAbsolutePath().contains(PATH_TODO.getFolderName())) {
jobFile = shiftFileFolder(jobFile, PATH_TODO, PATH_DOING); jobFile = shiftFileFolder(jobFile, PATH_TODO, PATH_DOING);
} }
} }
public void markDone() { public void markDone() throws JobFileNotMovedException {
jobFile = shiftFileFolder(jobFile, PATH_DOING, PATH_DONE); jobFile = shiftFileFolder(jobFile, PATH_DOING, PATH_DONE);
} }
public void forceMarkDone() { public void forceMarkDone() throws JobFileNotMovedException {
if (jobFile.getAbsolutePath().contains(PATH_DOING.getFolderName())) { if (jobFile.getAbsolutePath().contains(PATH_DOING.getFolderName())) {
jobFile = shiftFileFolder(jobFile, PATH_DOING, PATH_DONE); jobFile = shiftFileFolder(jobFile, PATH_DOING, PATH_DONE);
} else if (jobFile.getAbsolutePath().contains(PATH_TODO.getFolderName())) { } else if (jobFile.getAbsolutePath().contains(PATH_TODO.getFolderName())) {
@ -63,7 +64,7 @@ public abstract class JobFile<E> {
} }
} }
public void driveBack() { public void driveBack() throws JobFileNotMovedException {
if (jobFile.getAbsolutePath().contains(PATH_DOING.getFolderName())) { if (jobFile.getAbsolutePath().contains(PATH_DOING.getFolderName())) {
jobFile = shiftFileFolder(jobFile, PATH_DOING, PATH_TODO); jobFile = shiftFileFolder(jobFile, PATH_DOING, PATH_TODO);
} }

View File

@ -1,6 +1,7 @@
package tv.mangrana.jobs; package tv.mangrana.jobs;
import tv.mangrana.config.LocalEnvironmentManager; import tv.mangrana.config.LocalEnvironmentManager;
import tv.mangrana.exception.JobFileNotMovedException;
import tv.mangrana.utils.PathUtils; import tv.mangrana.utils.PathUtils;
import java.io.File; import java.io.File;
@ -24,7 +25,8 @@ public class JobFileManager {
public enum JobFileType { public enum JobFileType {
SONARR_JOBS("sonarr"), SONARR_JOBS("sonarr"),
RADARR_JOBS("radarr"); RADARR_JOBS("radarr"),
TRANSMISSION_JOBS("transmission");
private final String folderName; private final String folderName;
JobFileType(String folderName) { JobFileType(String folderName) {
this.folderName=folderName; this.folderName=folderName;
@ -40,7 +42,13 @@ public class JobFileManager {
List<File> uncompleted = files!=null List<File> uncompleted = files!=null
? Arrays.asList(files) ? Arrays.asList(files)
: Collections.emptyList(); : Collections.emptyList();
uncompleted.forEach(file -> PathUtils.shiftFileFolder(file, PATH_DOING, PATH_TODO)); uncompleted.forEach(file -> {
try {
PathUtils.shiftFileFolder(file, PATH_DOING, PATH_TODO);
} catch (JobFileNotMovedException e) {
e.printStackTrace();
}
});
} }
public static List<File> retrieveJobFiles(String fileIdentifierRegex, JobFileType appType) { public static List<File> retrieveJobFiles(String fileIdentifierRegex, JobFileType appType) {

View File

@ -18,8 +18,6 @@ import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.net.URI; import java.net.URI;
import java.net.URISyntaxException; import java.net.URISyntaxException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static tv.mangrana.config.CommonConfigFileLoader.CommonProjectConfiguration.*; import static tv.mangrana.config.CommonConfigFileLoader.CommonProjectConfiguration.*;
import static tv.mangrana.utils.Output.log; import static tv.mangrana.utils.Output.log;
@ -42,8 +40,8 @@ public class PlexCommandLauncher {
// new PlexCommandLauncher(new CommonConfigFileLoader()).scanByPath(toRefresh); // new PlexCommandLauncher(new CommonConfigFileLoader()).scanByPath(toRefresh);
// } // }
public void scanByPath(String fullDestinationPath) { public void scanSerieByPath(String fullDestinationPath) {
String plexPathToRefresh = getPlexUrlPath2Refresh(fullDestinationPath); String plexPathToRefresh = getPlexSeriePath2Refresh(fullDestinationPath);
String plexRefreshURL = getPlexRefreshURL(plexPathToRefresh); String plexRefreshURL = getPlexRefreshURL(plexPathToRefresh);
if (plexRefreshURL==null) return; if (plexRefreshURL==null) return;
try (CloseableHttpClient httpclient = HttpClients.createDefault()) { try (CloseableHttpClient httpclient = HttpClients.createDefault()) {
@ -62,14 +60,10 @@ public class PlexCommandLauncher {
} }
} }
public String getPlexUrlPath2Refresh(String fullDestinationPath) { public String getPlexSeriePath2Refresh(String fullDestinationPath) {
Pattern p = Pattern.compile(config.getConfig(SONARR_PATHS_STARTER).concat("(.+/.+ \\(\\d{4}\\))")); String sonarrPathStarter = config.getConfig(SONARR_PATHS_STARTER);
Matcher m = p.matcher(fullDestinationPath); String plexDockerPathStarter = config.getConfig(PLEX_SERIES_PATHS_STARTER);
if (m.find()) { return fullDestinationPath.replaceFirst(sonarrPathStarter, plexDockerPathStarter);
String pathInPlexDockerStart = config.getConfig(PLEX_SERIES_PATHS_STARTER);
return pathInPlexDockerStart.concat(m.group(1));
}
return null;
} }
public Document retrieveSectionsInfo() { public Document retrieveSectionsInfo() {

View File

@ -2,10 +2,12 @@ package tv.mangrana.utils;
import tv.mangrana.exception.JobFileNotMovedException;
import tv.mangrana.jobs.JobFile; import tv.mangrana.jobs.JobFile;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.nio.file.FileAlreadyExistsException;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.Paths; import java.nio.file.Paths;
@ -36,7 +38,7 @@ public class PathUtils {
return absolutePath.substring(absolutePath.lastIndexOf(SEPARATOR)+1); return absolutePath.substring(absolutePath.lastIndexOf(SEPARATOR)+1);
} }
public static File shiftFileFolder(File jobFile, JobFile.JobLocation folderOrigin, JobFile.JobLocation folderDestination) { public static File shiftFileFolder(File jobFile, JobFile.JobLocation folderOrigin, JobFile.JobLocation folderDestination) throws JobFileNotMovedException {
try { try {
Path newPath = Files.move( Path newPath = Files.move(
jobFile.toPath() jobFile.toPath()
@ -44,10 +46,17 @@ public class PathUtils {
.replaceFirst(folderOrigin.getFolderName(), folderDestination.getFolderName()))); .replaceFirst(folderOrigin.getFolderName(), folderDestination.getFolderName())));
log(Output.msg("moved job file <{2}> from -{0}- to -{1}-", folderOrigin, folderDestination, jobFile.getAbsolutePath())); log(Output.msg("moved job file <{2}> from -{0}- to -{1}-", folderOrigin, folderDestination, jobFile.getAbsolutePath()));
return newPath.toFile(); return newPath.toFile();
} catch (FileAlreadyExistsException e) {
log("File already exists on destination and will be deleted from source");
boolean deleted = jobFile.delete();
if (!deleted) throw new JobFileNotMovedException("Could not move the file because exists on destination and either delete it from source");
} catch (IOException e) { } catch (IOException e) {
log(Output.msg("COULD NOT MOVE file {2} from -{0}- to -{1}-", folderOrigin, folderDestination, jobFile.getAbsolutePath())); log(Output.msg("COULD NOT MOVE file {2} from -{0}- to -{1}-", folderOrigin, folderDestination, jobFile.getAbsolutePath()));
return jobFile; e.printStackTrace();
boolean renamed = jobFile.renameTo(new File(jobFile.getName() + "_handled"));
if (!renamed) throw new JobFileNotMovedException("Could not move the file and either rename it");
} }
return jobFile;
} }
public static int compareFileCreationDate (File o1, File o2) { public static int compareFileCreationDate (File o1, File o2) {