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
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
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
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
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