move file management to commons library
This commit is contained in:
parent
bd2a013283
commit
f9a3399303
|
@ -0,0 +1,72 @@
|
|||
package tv.mangrana.jobs;
|
||||
|
||||
import tv.mangrana.config.LocalEnvironmentManager;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import static tv.mangrana.jobs.JobFile.JobLocation.*;
|
||||
import static tv.mangrana.utils.PathUtils.shiftFileFolder;
|
||||
|
||||
public abstract class JobFile<E> {
|
||||
|
||||
public enum JobLocation {
|
||||
PATH_TODO("to_do"),
|
||||
PATH_DOING("doing"),
|
||||
PATH_DONE("done");
|
||||
private final String folderName;
|
||||
private static final String LOCAL_WORKING_PATH = "local_test";
|
||||
JobLocation(String folderName) {
|
||||
this.folderName=folderName;
|
||||
}
|
||||
public String getFolderName(){
|
||||
return getLocalNameIfNecessary(this);
|
||||
}
|
||||
private String getLocalNameIfNecessary(JobLocation location) {
|
||||
if (LocalEnvironmentManager.isLocal()
|
||||
&& (location.equals(JobLocation.PATH_TODO) || location.equals(JobLocation.PATH_DOING))) {
|
||||
return LOCAL_WORKING_PATH;
|
||||
}
|
||||
return location.folderName;
|
||||
}
|
||||
}
|
||||
|
||||
protected JobFile(File jobFile) {
|
||||
this.jobFile = jobFile;
|
||||
}
|
||||
|
||||
public abstract String getInfo(E key);
|
||||
|
||||
public abstract boolean hasNoInfo();
|
||||
|
||||
protected File jobFile;
|
||||
|
||||
public File getFile () {
|
||||
return jobFile;
|
||||
}
|
||||
|
||||
|
||||
public void markDoing() {
|
||||
if (jobFile.getAbsolutePath().contains(PATH_TODO.getFolderName())) {
|
||||
jobFile = shiftFileFolder(jobFile, PATH_TODO, PATH_DOING);
|
||||
}
|
||||
}
|
||||
|
||||
public void markDone() {
|
||||
jobFile = shiftFileFolder(jobFile, PATH_DOING, PATH_DONE);
|
||||
}
|
||||
|
||||
public void forceMarkDone() {
|
||||
if (jobFile.getAbsolutePath().contains(PATH_DOING.getFolderName())) {
|
||||
jobFile = shiftFileFolder(jobFile, PATH_DOING, PATH_DONE);
|
||||
} else if (jobFile.getAbsolutePath().contains(PATH_TODO.getFolderName())) {
|
||||
jobFile = shiftFileFolder(jobFile, PATH_TODO, PATH_DONE);
|
||||
}
|
||||
}
|
||||
|
||||
public void driveBack() {
|
||||
if (jobFile.getAbsolutePath().contains(PATH_DOING.getFolderName())) {
|
||||
jobFile = shiftFileFolder(jobFile, PATH_DOING, PATH_TODO);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,65 @@
|
|||
package tv.mangrana.jobs;
|
||||
|
||||
import tv.mangrana.config.LocalEnvironmentManager;
|
||||
import tv.mangrana.utils.PathUtils;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static tv.mangrana.config.LocalEnvironmentManager.NAS_ACCESS_FOLDER_FROM_MAC;
|
||||
import static tv.mangrana.jobs.JobFile.JobLocation;
|
||||
import static tv.mangrana.jobs.JobFile.JobLocation.PATH_DOING;
|
||||
import static tv.mangrana.jobs.JobFile.JobLocation.PATH_TODO;
|
||||
import static tv.mangrana.utils.PathUtils.addSubFolder;
|
||||
import static tv.mangrana.utils.PathUtils.rootFolder;
|
||||
|
||||
public class JobFileManager {
|
||||
|
||||
private JobFileManager(){}
|
||||
|
||||
public static final String JOBS_FOLDER = "jobs";
|
||||
|
||||
public enum JobFileType {
|
||||
SONARR_JOBS("sonarr"),
|
||||
RADARR_JOBS("radarr");
|
||||
private final String folderName;
|
||||
JobFileType(String folderName) {
|
||||
this.folderName=folderName;
|
||||
}
|
||||
public String getFolderName(){
|
||||
return folderName;
|
||||
}
|
||||
}
|
||||
|
||||
public static void moveUncompletedJobsToRetry(JobFileType appType) {
|
||||
File jobsDir = new File(getAbsolutePath(PATH_DOING, appType));
|
||||
File[] files = jobsDir.listFiles();
|
||||
List<File> uncompleted = files!=null
|
||||
? Arrays.asList(files)
|
||||
: Collections.emptyList();
|
||||
uncompleted.forEach(file -> PathUtils.shiftFileFolder(file, PATH_DOING, PATH_TODO));
|
||||
}
|
||||
|
||||
public static List<File> retrieveJobFiles(String fileIdentifierRegex, JobFileType appType) {
|
||||
File jobsDir = new File(getAbsolutePath(PATH_TODO, appType));
|
||||
File[] files = jobsDir.listFiles();
|
||||
return files==null
|
||||
? Collections.emptyList()
|
||||
: Arrays.stream(files)
|
||||
.filter(file -> file.getName().matches(fileIdentifierRegex))
|
||||
.sorted(PathUtils::compareFileCreationDate)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
public static String getAbsolutePath(JobLocation location, JobFileType appType) {
|
||||
String jobsFolder = LocalEnvironmentManager.isLocal()
|
||||
? addSubFolder(rootFolder(NAS_ACCESS_FOLDER_FROM_MAC), JOBS_FOLDER)
|
||||
: rootFolder(JOBS_FOLDER);
|
||||
String appFolderPath = addSubFolder(jobsFolder, appType.getFolderName());
|
||||
return addSubFolder(appFolderPath, location.getFolderName());
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,74 @@
|
|||
package tv.mangrana.utils;
|
||||
|
||||
|
||||
|
||||
import tv.mangrana.jobs.JobFile;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.nio.file.attribute.FileTime;
|
||||
|
||||
public class PathUtils {
|
||||
|
||||
public static final char SEPARATOR = '/';
|
||||
|
||||
private PathUtils(){}
|
||||
|
||||
public static String addSubFolder(String parentFolder, String childFolder) {
|
||||
return parentFolder+ SEPARATOR +childFolder;
|
||||
}
|
||||
public static String rootFolder(String rootFolderName){
|
||||
return addSubFolder("",rootFolderName);
|
||||
}
|
||||
|
||||
public static String getParentFromFullPath(String absolutePath){
|
||||
return Paths
|
||||
.get(absolutePath)
|
||||
.getParent()
|
||||
.getFileName()
|
||||
.toString();
|
||||
}
|
||||
|
||||
public static String getCurrentFromFullPath(String absolutePath) {
|
||||
return absolutePath.substring(absolutePath.lastIndexOf(SEPARATOR)+1);
|
||||
}
|
||||
|
||||
public static File shiftFileFolder(File jobFile, JobFile.JobLocation folderOrigin, JobFile.JobLocation folderDestination) {
|
||||
try {
|
||||
Path newPath = Files.move(
|
||||
jobFile.toPath()
|
||||
, Paths.get(jobFile.getAbsolutePath()
|
||||
.replaceFirst(folderOrigin.getFolderName(), folderDestination.getFolderName())));
|
||||
log(Output.msg("moved job file <{2}> from -{0}- to -{1}-", folderOrigin, folderDestination, jobFile.getAbsolutePath()));
|
||||
return newPath.toFile();
|
||||
} catch (IOException e) {
|
||||
log(Output.msg("COULD NOT MOVE file {2} from -{0}- to -{1}-", folderOrigin, folderDestination, jobFile.getAbsolutePath()));
|
||||
return jobFile;
|
||||
}
|
||||
}
|
||||
|
||||
public static int compareFileCreationDate (File o1, File o2) {
|
||||
final String creationTimeAttr = "creationTime";
|
||||
int res = 0;
|
||||
try {
|
||||
FileTime o1Birthday = (FileTime) Files.getAttribute(o1.toPath(), creationTimeAttr);
|
||||
FileTime o2Birthday = (FileTime) Files.getAttribute(o2.toPath(), creationTimeAttr);
|
||||
res = o1Birthday.compareTo(o2Birthday);
|
||||
} catch (IOException e) {
|
||||
log("there was a problem trying to compare creation date between "
|
||||
+ o1.getName() + " and " + o2.getName());
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
private static void log(String msg){
|
||||
Output.log("PathUtils: "+msg);
|
||||
}
|
||||
|
||||
public static String getRootProjectPath() {
|
||||
return System.getProperty("user.dir");
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue