Example #1
0
  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;
  }
Example #2
0
  public static File getFileNameWithAddedExtension(File parent, File f, String ext) {
    File ff = new File(parent, f.getName() + ext);

    if (ff.exists()) {
      return ff;
    }

    return null;
  }
Example #3
0
  public static File isFileExists(File f, String ext) {
    int point = f.getName().lastIndexOf('.');

    if (point == -1) {
      point = f.getName().length();
    }

    File lowerCasedFile =
        new File(f.getParentFile(), f.getName().substring(0, point) + "." + ext.toLowerCase());
    if (lowerCasedFile.exists()) {
      return lowerCasedFile;
    }

    File upperCasedFile =
        new File(f.getParentFile(), f.getName().substring(0, point) + "." + ext.toUpperCase());
    if (upperCasedFile.exists()) {
      return upperCasedFile;
    }

    return null;
  }
Example #4
0
  public static boolean isFileRelevant(File f, PmsConfiguration configuration) {
    String fileName = f.getName().toLowerCase();
    if ((configuration.isArchiveBrowsing()
            && (fileName.endsWith(".zip")
                || fileName.endsWith(".cbz")
                || fileName.endsWith(".rar")
                || fileName.endsWith(".cbr")))
        || fileName.endsWith(".iso")
        || fileName.endsWith(".img")
        || fileName.endsWith(".m3u")
        || fileName.endsWith(".m3u8")
        || fileName.endsWith(".pls")
        || fileName.endsWith(".cue")) {
      return true;
    }

    return false;
  }
Example #5
0
  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;
  }
Example #6
0
 public static File getFileNameWithNewExtension(File parent, File file, String ext) {
   return isFileExists(new File(parent, file.getName()), ext);
 }