public static boolean isFolderRelevant( File f, PmsConfiguration configuration, Set<String> ignoreFiles) { if (f.isDirectory() && configuration.isHideEmptyFolders()) { File[] children = f.listFiles(); /** * listFiles() returns null if "this abstract pathname does not denote a directory, or if an * I/O error occurs". in this case (since we've already confirmed that it's a directory), this * seems to mean the directory is non-readable * http://www.ps3mediaserver.org/forum/viewtopic.php?f=6&t=15135 * http://stackoverflow.com/questions/3228147/retrieving-the-underlying-error-when-file-listfiles-return-null */ if (children == null) { LOGGER.warn("Can't list files in non-readable directory: {}", f.getAbsolutePath()); } else { for (File child : children) { if (ignoreFiles.contains(child.getAbsolutePath())) { continue; } if (child.isFile()) { if (FormatFactory.getAssociatedFormat(child.getName()) != null || isFileRelevant(child, configuration)) { return true; } } else { if (isFolderRelevant(child, configuration, ignoreFiles)) { return true; } } } } } return false; }
// XXX dir.canRead() has issues on Windows, so verify it directly: // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6203387 public static boolean isDirectoryReadable(File dir) { boolean isReadable = false; if (dir != null) { // new File("").isDirectory() is false, even though getAbsolutePath() returns the right path. // this resolves it dir = dir.getAbsoluteFile(); if (dir.isDirectory()) { try { File[] files = dir.listFiles(); // null if an I/O error occurs isReadable = files != null; } catch (SecurityException se) { } } } return isReadable; }
private static synchronized boolean browseFolderForSubtitles( File subFolder, File file, DLNAMediaInfo media, boolean usecache) { boolean found = false; if (!usecache) { cache = null; } if (cache == null) { cache = new HashMap<>(); } final Set<String> supported = SubtitleType.getSupportedFileExtensions(); File[] allSubs = cache.get(subFolder); if (allSubs == null) { allSubs = subFolder.listFiles( new FilenameFilter() { @Override public boolean accept(File dir, String name) { String ext = FilenameUtils.getExtension(name).toLowerCase(); if ("sub".equals(ext)) { // Avoid microdvd/vobsub confusion by ignoring sub+idx pairs here since // they'll come in unambiguously as vobsub via the idx file anyway return isFileExists(new File(dir, name), "idx") == null; } return supported.contains(ext); } }); if (allSubs != null) { cache.put(subFolder, allSubs); } } String fileName = getFileNameWithoutExtension(file.getName()).toLowerCase(); if (allSubs != null) { for (File f : allSubs) { if (f.isFile() && !f.isHidden()) { String fName = f.getName().toLowerCase(); for (String ext : supported) { if (fName.length() > ext.length() && fName.startsWith(fileName) && endsWithIgnoreCase(fName, "." + ext)) { int a = fileName.length(); int b = fName.length() - ext.length() - 1; String code = ""; if (a <= b) { // handling case with several dots: <video>..<extension> code = fName.substring(a, b); } if (code.startsWith(".")) { code = code.substring(1); } boolean exists = false; if (media != null) { for (DLNAMediaSubtitle sub : media.getSubtitleTracksList()) { if (f.equals(sub.getExternalFile())) { exists = true; } else if (equalsIgnoreCase(ext, "idx") && sub.getType() == SubtitleType.MICRODVD) { // sub+idx => VOBSUB sub.setType(SubtitleType.VOBSUB); exists = true; } else if (equalsIgnoreCase(ext, "sub") && sub.getType() == SubtitleType.VOBSUB) { // VOBSUB try { sub.setExternalFile(f); } catch (FileNotFoundException ex) { LOGGER.warn("Exception during external subtitles scan.", ex); } exists = true; } } } if (!exists) { DLNAMediaSubtitle sub = new DLNAMediaSubtitle(); sub.setId( 100 + (media == null ? 0 : media.getSubtitleTracksList().size())); // fake id, not used if (code.length() == 0 || !Iso639.getCodeList().contains(code)) { sub.setLang(DLNAMediaSubtitle.UND); sub.setType(SubtitleType.valueOfFileExtension(ext)); if (code.length() > 0) { sub.setFlavor(code); if (sub.getFlavor().contains("-")) { String flavorLang = sub.getFlavor().substring(0, sub.getFlavor().indexOf('-')); String flavorTitle = sub.getFlavor().substring(sub.getFlavor().indexOf('-') + 1); if (Iso639.getCodeList().contains(flavorLang)) { sub.setLang(flavorLang); sub.setFlavor(flavorTitle); } } } } else { sub.setLang(code); sub.setType(SubtitleType.valueOfFileExtension(ext)); } try { sub.setExternalFile(f); } catch (FileNotFoundException ex) { LOGGER.warn("Exception during external subtitles scan.", ex); } found = true; if (media != null) { media.getSubtitleTracksList().add(sub); } } } } } } } return found; }