git-tfs-id: [http://tfs.userrendszerhaz.hu:8080/tfs/DefaultCollection]$/MediaCube...
authorvasary.daniel <TFS\vasary.daniel>
Thu, 29 Jul 2021 19:00:22 +0000 (19:00 +0000)
committervasary.daniel <TFS\vasary.daniel>
Thu, 29 Jul 2021 19:00:22 +0000 (19:00 +0000)
server/user.jobengine.executors/src/user/jobengine/server/steps/shared/PBQuery.java

index a550521c450f0aaa7ac17890726be39b0915cdec..2b50e30d091e3c79e3d6048d17c1f4aba43c9d81 100644 (file)
@@ -5,16 +5,23 @@ import java.io.FileNotFoundException;
 import java.io.IOException;\r
 import java.io.StringReader;\r
 import java.net.URL;\r
+import java.nio.file.CopyOption;\r
+import java.nio.file.DirectoryStream;\r
 import java.nio.file.Files;\r
 import java.nio.file.Path;\r
 import java.nio.file.Paths;\r
+import java.nio.file.StandardCopyOption;\r
 import java.text.SimpleDateFormat;\r
+import java.util.Arrays;\r
 import java.util.Calendar;\r
 import java.util.Collection;\r
 import java.util.Date;\r
 import java.util.HashMap;\r
+import java.util.HashSet;\r
+import java.util.Iterator;\r
 import java.util.List;\r
 import java.util.Map;\r
+import java.util.Set;\r
 import java.util.TreeMap;\r
 \r
 import javax.xml.parsers.DocumentBuilder;\r
@@ -71,6 +78,10 @@ public class PBQuery {
        public static final String GETMEDIAUSAGEBYUTRANGE = "/getMediaUsageByUTRange";\r
        public static final String GETCUSTOMVIEW = "/getCustomView";\r
 \r
+       private static final Path subtitleRoot = Paths.get("X:\\PB_ARCH");\r
+       private static final Path missingSubsRoot = Paths.get("R:\\BeachPool");\r
+       private Set<Path> subsDirectoriesList;\r
+\r
        private static Document toDocument(String xml) {\r
                DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();\r
                DocumentBuilder builder = null;\r
@@ -137,6 +148,7 @@ public class PBQuery {
                this.secondaryUserName = secondaryUserName;\r
                this.secondaryPassword = secondaryPassword;\r
                this.rangeForwardHours = rangeForwardHours;\r
+               this.subsDirectoriesList = readSubtileDirectoriesList(subtitleRoot);\r
        }\r
 \r
        private void putEarliest(Map<String, MMMedia> result, String mediaName, String usageTime) {\r
@@ -306,8 +318,17 @@ public class PBQuery {
                        if (StringUtils.isBlank(mediaName))\r
                                continue;\r
 \r
-                       if (!StringUtils.equals("Subtitle", mediaType))\r
-                               logger.info(subtitleMarker, "querySortableMedias: {}", mediaName);\r
+                       if (StringUtils.equals("Subtitle", mediaType)) {\r
+                               logger.info(this.subtitleMarker, "Missing subtitle: {}", mediaName);\r
+\r
+                               try {\r
+                                       this.copyMissingSubtitle(mediaName + ".stl");\r
+                               } catch (Exception e) {\r
+                                       logger.error(this.subtitleMarker, e.getMessage());\r
+                               }\r
+\r
+                               continue;\r
+                       }\r
 \r
                        if (!StringUtils.equals("Video", mediaType))\r
                                continue;\r
@@ -339,4 +360,54 @@ public class PBQuery {
                return result;\r
        }\r
 \r
+       private Set<Path> readSubtileDirectoriesList(Path root) {\r
+               Set<Path> result = new HashSet<>();\r
+               DirectoryStream<Path> stream = null;\r
+               try {\r
+                       stream = Files.newDirectoryStream(Paths.get(root.toString()));\r
+                       Iterator<Path> dirIterator = stream.iterator();\r
+\r
+                       while (dirIterator.hasNext()) {\r
+                               Path path = dirIterator.next();\r
+                               if (Files.isDirectory(path)) {\r
+                                       result.add(path);\r
+                                       logger.info(this.subtitleMarker, "Detected subtitle root {}", path);\r
+                               }\r
+                       }\r
+               } catch (Exception e) {\r
+                       logger.error(this.subtitleMarker, e.getMessage());\r
+               } finally {\r
+                       try {\r
+                               if (stream != null)\r
+                                       stream.close();\r
+                       } catch (IOException e) {\r
+                       }\r
+\r
+               }\r
+               return result;\r
+       }\r
+\r
+       private void copyMissingSubtitle(String subtitleName) throws Exception {\r
+               boolean found = false;\r
+               for (Path subtitleDir : this.subsDirectoriesList) {\r
+                       Path source = Paths.get(subtitleDir.toString(), new String[] { subtitleName });\r
+                       if (source.toFile().exists()) {\r
+                               found = true;\r
+                               logger.info(this.subtitleMarker, "Found missing {}", source);\r
+                               Path target = Paths.get(missingSubsRoot.toString(), new String[] { subtitleName });\r
+                               if (target.toFile().exists())\r
+                                       logger.info(this.subtitleMarker, "Missing subtitle {} already exists, overriding", target);\r
+                               Files.copy(source, target, new CopyOption[] { StandardCopyOption.REPLACE_EXISTING });\r
+                               boolean isContentEquals = Arrays.equals(Files.readAllBytes(source), Files.readAllBytes(target));\r
+                               if (isContentEquals) {\r
+                                       logger.info(this.subtitleMarker, "Successfully copied {}", target);\r
+                                       break;\r
+                               }\r
+                               logger.error(this.subtitleMarker, "Source {} and target {} content are not equals", source, target);\r
+                               break;\r
+                       }\r
+               }\r
+               if (!found)\r
+                       logger.warn(this.subtitleMarker, "Can't find missing {}", subtitleName);\r
+       }\r
 }\r