Ejemplo n.º 1
0
  // Loosely based on the workaround posted here:
  // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4993360
  // XXX why isn't this in Apache Commons?
  public static boolean isFileWritable(File file) {
    boolean isWritable = false;

    if (file != null) {
      boolean fileAlreadyExists = file.isFile(); // i.e. exists and is a File

      if (fileAlreadyExists || !file.exists()) {
        try {
          // true: open for append: make sure the open
          // doesn't clobber the file
          new FileOutputStream(file, true).close();
          isWritable = true;

          if (!fileAlreadyExists) { // a new file has been "touch"ed; try to remove it
            try {
              if (!file.delete()) {
                LOGGER.warn("Can't delete temporary test file: {}", file.getAbsolutePath());
              }
            } catch (SecurityException se) {
              LOGGER.error("Error deleting temporary test file: " + file.getAbsolutePath(), se);
            }
          }
        } catch (IOException | SecurityException ioe) {
        }
      }
    }

    return isWritable;
  }
Ejemplo n.º 2
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;
  }
Ejemplo n.º 3
0
  // based on the workaround posted here:
  // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4993360
  // XXX why isn't this in Apache Commons?
  public static boolean isFileReadable(File file) {
    boolean isReadable = false;

    if ((file != null) && file.isFile()) {
      try {
        new FileInputStream(file).close();
        isReadable = true;
      } catch (IOException ioe) {
      }
    }

    return isReadable;
  }
Ejemplo n.º 4
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;
  }