bugfixing, error handling and refinements
This commit is contained in:
parent
c5bf89fb4d
commit
083e33de9b
|
@ -21,6 +21,8 @@ import java.io.InputStreamReader;
|
|||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import static tv.mangrana.utils.Output.log;
|
||||
|
||||
public class GoogleDriveUtils {
|
||||
|
||||
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) //
|
||||
.setApplicationName(APPLICATION_NAME).build();
|
||||
log("Google Drive service initialized");
|
||||
return _driveService;
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,7 @@
|
|||
package tv.mangrana.exception;
|
||||
|
||||
public class JobFileNotMovedException extends Exception{
|
||||
public JobFileNotMovedException(String e) {
|
||||
super(e);
|
||||
}
|
||||
}
|
|
@ -1,6 +1,7 @@
|
|||
package tv.mangrana.jobs;
|
||||
|
||||
import tv.mangrana.config.LocalEnvironmentManager;
|
||||
import tv.mangrana.exception.JobFileNotMovedException;
|
||||
|
||||
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())) {
|
||||
jobFile = shiftFileFolder(jobFile, PATH_TODO, PATH_DOING);
|
||||
}
|
||||
}
|
||||
|
||||
public void markDone() {
|
||||
public void markDone() throws JobFileNotMovedException {
|
||||
jobFile = shiftFileFolder(jobFile, PATH_DOING, PATH_DONE);
|
||||
}
|
||||
|
||||
public void forceMarkDone() {
|
||||
public void forceMarkDone() throws JobFileNotMovedException {
|
||||
if (jobFile.getAbsolutePath().contains(PATH_DOING.getFolderName())) {
|
||||
jobFile = shiftFileFolder(jobFile, PATH_DOING, PATH_DONE);
|
||||
} 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())) {
|
||||
jobFile = shiftFileFolder(jobFile, PATH_DOING, PATH_TODO);
|
||||
}
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package tv.mangrana.jobs;
|
||||
|
||||
import tv.mangrana.config.LocalEnvironmentManager;
|
||||
import tv.mangrana.exception.JobFileNotMovedException;
|
||||
import tv.mangrana.utils.PathUtils;
|
||||
|
||||
import java.io.File;
|
||||
|
@ -24,7 +25,8 @@ public class JobFileManager {
|
|||
|
||||
public enum JobFileType {
|
||||
SONARR_JOBS("sonarr"),
|
||||
RADARR_JOBS("radarr");
|
||||
RADARR_JOBS("radarr"),
|
||||
TRANSMISSION_JOBS("transmission");
|
||||
private final String folderName;
|
||||
JobFileType(String folderName) {
|
||||
this.folderName=folderName;
|
||||
|
@ -40,7 +42,13 @@ public class JobFileManager {
|
|||
List<File> uncompleted = files!=null
|
||||
? Arrays.asList(files)
|
||||
: 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) {
|
||||
|
|
|
@ -18,8 +18,6 @@ import java.io.IOException;
|
|||
import java.io.InputStream;
|
||||
import java.net.URI;
|
||||
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.utils.Output.log;
|
||||
|
@ -42,8 +40,8 @@ public class PlexCommandLauncher {
|
|||
// new PlexCommandLauncher(new CommonConfigFileLoader()).scanByPath(toRefresh);
|
||||
// }
|
||||
|
||||
public void scanByPath(String fullDestinationPath) {
|
||||
String plexPathToRefresh = getPlexUrlPath2Refresh(fullDestinationPath);
|
||||
public void scanSerieByPath(String fullDestinationPath) {
|
||||
String plexPathToRefresh = getPlexSeriePath2Refresh(fullDestinationPath);
|
||||
String plexRefreshURL = getPlexRefreshURL(plexPathToRefresh);
|
||||
if (plexRefreshURL==null) return;
|
||||
try (CloseableHttpClient httpclient = HttpClients.createDefault()) {
|
||||
|
@ -62,14 +60,10 @@ public class PlexCommandLauncher {
|
|||
}
|
||||
}
|
||||
|
||||
public String getPlexUrlPath2Refresh(String fullDestinationPath) {
|
||||
Pattern p = Pattern.compile(config.getConfig(SONARR_PATHS_STARTER).concat("(.+/.+ \\(\\d{4}\\))"));
|
||||
Matcher m = p.matcher(fullDestinationPath);
|
||||
if (m.find()) {
|
||||
String pathInPlexDockerStart = config.getConfig(PLEX_SERIES_PATHS_STARTER);
|
||||
return pathInPlexDockerStart.concat(m.group(1));
|
||||
}
|
||||
return null;
|
||||
public String getPlexSeriePath2Refresh(String fullDestinationPath) {
|
||||
String sonarrPathStarter = config.getConfig(SONARR_PATHS_STARTER);
|
||||
String plexDockerPathStarter = config.getConfig(PLEX_SERIES_PATHS_STARTER);
|
||||
return fullDestinationPath.replaceFirst(sonarrPathStarter, plexDockerPathStarter);
|
||||
}
|
||||
|
||||
public Document retrieveSectionsInfo() {
|
||||
|
|
|
@ -2,10 +2,12 @@ package tv.mangrana.utils;
|
|||
|
||||
|
||||
|
||||
import tv.mangrana.exception.JobFileNotMovedException;
|
||||
import tv.mangrana.jobs.JobFile;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.FileAlreadyExistsException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
|
@ -36,7 +38,7 @@ public class PathUtils {
|
|||
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 {
|
||||
Path newPath = Files.move(
|
||||
jobFile.toPath()
|
||||
|
@ -44,10 +46,17 @@ public class PathUtils {
|
|||
.replaceFirst(folderOrigin.getFolderName(), folderDestination.getFolderName())));
|
||||
log(Output.msg("moved job file <{2}> from -{0}- to -{1}-", folderOrigin, folderDestination, jobFile.getAbsolutePath()));
|
||||
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) {
|
||||
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) {
|
||||
|
|
Loading…
Reference in New Issue