From f206289dba62ceb2f4e1e3401c386bf928fb1ec5 Mon Sep 17 00:00:00 2001 From: xeviff Date: Tue, 2 May 2023 14:30:53 +0200 Subject: [PATCH] bugfix and responsability relocation --- pom.xml | 2 +- .../client/gateway/GoogleDriveApiGateway.java | 13 ++- .../tv/mangrana/jobs/JobsFileStorage.java | 99 +++++++++++++++++++ .../tv/mangrana/utils/yml/FakeYmlLoader.java | 5 + 4 files changed, 117 insertions(+), 2 deletions(-) create mode 100644 src/main/java/tv/mangrana/jobs/JobsFileStorage.java diff --git a/pom.xml b/pom.xml index aa893cd..601258a 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ tv.mangrana mangrana-commons - 3.1.3 + 3.1.5 8 diff --git a/src/main/java/tv/mangrana/google/api/client/gateway/GoogleDriveApiGateway.java b/src/main/java/tv/mangrana/google/api/client/gateway/GoogleDriveApiGateway.java index d821671..5c0584c 100644 --- a/src/main/java/tv/mangrana/google/api/client/gateway/GoogleDriveApiGateway.java +++ b/src/main/java/tv/mangrana/google/api/client/gateway/GoogleDriveApiGateway.java @@ -75,7 +75,7 @@ public class GoogleDriveApiGateway { "'"+parent.getId()+"' in parents"; List children = getChildrenCommonCall(query); if (children.isEmpty()) throw new NoElementFoundException("no elements in the list xO"); - if (children.size() > 1) log("WARNING: more than one element here not expected"); + if (children.size() > 1) log("WARNING: more than one element here not expected for <{0}> and parent <{1}>", name, parent.getName()); return children.get(0); } @@ -114,6 +114,17 @@ public class GoogleDriveApiGateway { .execute(); } + public void moveFile(File file, String destinationFolderId, String newName) throws IOException { + File newFileReference = new File(); + newFileReference.setName(newName); + service.files() + .update(file.getId(), newFileReference) + .setAddParents(destinationFolderId) + .setRemoveParents(file.getParents().get(0)) + .setSupportsTeamDrives(true) + .execute(); + } + public File createFolder(String name, String parentFolderId) throws IOException { File fileMetadata = new File(); fileMetadata.setName(name); diff --git a/src/main/java/tv/mangrana/jobs/JobsFileStorage.java b/src/main/java/tv/mangrana/jobs/JobsFileStorage.java new file mode 100644 index 0000000..e0d3d50 --- /dev/null +++ b/src/main/java/tv/mangrana/jobs/JobsFileStorage.java @@ -0,0 +1,99 @@ +package tv.mangrana.jobs; + +import org.apache.commons.lang.StringUtils; + +import java.io.FileWriter; +import java.io.IOException; +import java.io.PrintWriter; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.text.MessageFormat; +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; +import java.util.HashMap; +import java.util.Map; +import java.util.stream.Stream; + +import static tv.mangrana.utils.Output.DATE_TIME_FORMAT; + +public class JobsFileStorage { + + static final String COMPLETED_DATE = "done"; + static final String JOB_TYPE = "type"; + static final String HASH = "hash"; + static final String ARR_ID = "arrId"; + static final String INTERNET_DB_ID = "iId"; + static final String ELEMENT_NAME = "element"; + + public final String JOB_LINE_FORMAT; + + public JobsFileStorage() { + String preFormat = "{COMPLETED_DATE}: {5} | {JOB_TYPE}: {0} | {HASH}: {1} | {ARR_ID}: {2} | {INTERNET_DB_ID}: {3} | {ELEMENT_NAME}: {4}"; + JOB_LINE_FORMAT = preFormat + .replace("{COMPLETED_DATE}", COMPLETED_DATE) + .replace("{JOB_TYPE}", JOB_TYPE) + .replace("{HASH}", HASH) + .replace("{ARR_ID}", ARR_ID) + .replace("{INTERNET_DB_ID}", INTERNET_DB_ID) + .replace("{ELEMENT_NAME}", ELEMENT_NAME); + } + String resumeFile = JobFileManager.getResumeFile(); + + public void persistCompleted(String type, String downloadId, int arrId, int iId, String element, LocalDateTime time){ + String jobLine = MessageFormat.format(JOB_LINE_FORMAT, + type, + downloadId, + fixLength(String.valueOf(arrId)), + fixLength(String.valueOf(iId)), + element, + formatTime(time)); + addLine(jobLine); + } + + String fixLength(String text) { + return StringUtils.rightPad(text, 7); + } + + void addLine(String jobLine) { + try (FileWriter fw = new FileWriter(resumeFile, true); + PrintWriter pw = new PrintWriter(fw)) { + + pw.println(jobLine); + } catch (IOException e) { + e.printStackTrace(); + } + } + + public String getIIDByElement(String element) { + try (Stream linesStream = Files.lines(Paths.get(resumeFile))) { + return linesStream + .map(this::line2Map) + .filter(mappedLine -> mappedLine.get(ELEMENT_NAME).equals(element)) + .findAny() + .map(found -> found.get(INTERNET_DB_ID)) + .orElse(null); + } catch (IOException e) { + e.printStackTrace(); + } + return null; + } + + private Map line2Map(String line) { + Map mappedLine = new HashMap<>(); + String[] infoList = line.split("\\|"); + for (String elementInfo : infoList) { + if (!elementInfo.trim().startsWith(COMPLETED_DATE)) { + String[] fieldValue = elementInfo.split(":"); + String field = fieldValue[0].trim(); + String value = fieldValue[1].trim(); + mappedLine.put(field, value); + } + } + return mappedLine; + } + + String formatTime(LocalDateTime time) { + return time.format(DateTimeFormatter.ofPattern(DATE_TIME_FORMAT)); + } + +} diff --git a/src/main/java/tv/mangrana/utils/yml/FakeYmlLoader.java b/src/main/java/tv/mangrana/utils/yml/FakeYmlLoader.java index c0ea8d0..7379b6a 100644 --- a/src/main/java/tv/mangrana/utils/yml/FakeYmlLoader.java +++ b/src/main/java/tv/mangrana/utils/yml/FakeYmlLoader.java @@ -62,6 +62,11 @@ public class FakeYmlLoader { Map keyValueMap = new HashMap<>(); stringStream.forEach(line -> { try { + int firstPosition = line.indexOf(':'); + if (firstPosition != line.lastIndexOf(':')) { + line = line.replace(':', '-'); + line = line.replaceFirst("-", ":"); + } String[] keyValue = line.split(":"); keyValueMap.put(keyValue[0], keyValue[1].trim()); } catch (Exception e) {