Example #1
0
  /*     pictures/
   *       some/
   *         folder/
   *           file.jpg
   *       some\\folder/
   * ->       file.jpg
   *
   *  relativeNormalizedPath = pictures/some\\folder/file.jpg
   *
   *  -> createable: pictures/somefolder (filename conflict)/file.jpg
   *
   *  http://msdn.microsoft.com/en-us/library/system.io.path.getinvalidfilenamechars.aspx
   */
  public NormalizedPath toCreatable(String filenameSuffix, boolean canExist) throws Exception {
    if (canExist) {
      return toCreatable(filenameSuffix, 0);
    } else {
      NormalizedPath creatableNormalizedPath = null;
      int attempt = 0;

      do {
        creatableNormalizedPath = toCreatable(filenameSuffix, attempt);
        boolean exists = FileUtil.exists(creatableNormalizedPath.toFile());

        // TODO [medium] The exists-check should be in the pathPart-loop, b/c what if fileB is a
        // FILE in this path: folderA/fileB/folderC/file1.jpg

        if (!exists) {
          return creatableNormalizedPath;
        }

        logger.log(
            Level.WARNING, " - File exists, trying new file: " + creatableNormalizedPath.toFile());
      } while (attempt++ < 10);

      throw new Exception(
          "Cannot create creatable path; " + creatableNormalizedPath + " attempts: " + attempt);
    }
  }
Example #2
0
  public NormalizedPath withSuffix(String filenameSuffix, boolean canExist) throws Exception {
    if (canExist) {
      return toCreatable(filenameSuffix, 0);
    } else {
      NormalizedPath creatableNormalizedPath = null;
      int attempt = 0;

      do {
        String aFilenameSuffix = (attempt > 0) ? filenameSuffix + " " + attempt : filenameSuffix;
        creatableNormalizedPath =
            new NormalizedPath(
                root, addFilenameConflictSuffix(normalizedPath.toString(), aFilenameSuffix));
        boolean exists = FileUtil.exists(creatableNormalizedPath.toFile());

        if (!exists) {
          return creatableNormalizedPath;
        }
      } while (attempt++ < 200);

      throw new Exception(
          "Cannot create path with suffix; " + attempt + " attempts: " + creatableNormalizedPath);
    }
  }