Exemplo n.º 1
0
  /**
   * This method will copy the file data from file to version if version is not null and version
   * inode > 0 and will replace current file data if newData passed is not null
   *
   * @param file
   * @param version
   * @param newData
   * @throws IOException
   * @throws Exception
   */
  public void saveFileData(File file, File destination, java.io.File newDataFile)
      throws IOException {

    String fileName = file.getFileName();

    // This was added for http://jira.dotmarketing.net/browse/DOTCMS-5390
    // but this breaks the original intent of the
    // method. See the doc for the method above. Caused
    // http://jira.dotmarketing.net/browse/DOTCMS-5539 so commented out.
    // if(newDataFile ==null || newDataFile.length() ==0){
    // throw new
    // DotStateException("Null or 0 lenght java.io.file passed in for file:"
    // + file.getInode());
    // }

    String assetsPath = APILocator.getFileAPI().getRealAssetsRootPath();
    new java.io.File(assetsPath).mkdir();

    // creates the new file as
    // inode{1}/inode{2}/inode.file_extension
    java.io.File workingFile = getAssetIOFile(file);

    // http://jira.dotmarketing.net/browse/DOTCMS-1873
    // To clear velocity cache
    DotResourceCache vc = CacheLocator.getVeloctyResourceCache();
    vc.remove(ResourceManager.RESOURCE_TEMPLATE + workingFile.getPath());

    // If a new version was created, we move the current data to the new
    // version
    if (destination != null && InodeUtils.isSet(destination.getInode())) {
      java.io.File newVersionFile = getAssetIOFile(destination);
      // FileUtil.copyFile(workingFile, newVersionFile);
      FileUtils.copyFile(workingFile, newVersionFile);
      // FileInputStream is = new FileInputStream(workingFile);
      // FileChannel channelFrom = is.getChannel();
      // java.io.File newVersionFile = getAssetIOFile(destination);
      // FileChannel channelTo = new
      // FileOutputStream(newVersionFile).getChannel();
      // channelFrom.transferTo(0, channelFrom.size(), channelTo);
      // channelTo.force(false);
      // channelTo.close();
      // channelFrom.close();
    }

    if (newDataFile != null) {
      // Saving the new working data
      FileUtils.copyFile(newDataFile, workingFile);

      file.setSize((int) newDataFile.length());

      // checks if it's an image
      if (UtilMethods.isImage(fileName)) {
        InputStream in = null;
        try {
          // gets image height
          in = new BufferedInputStream(new FileInputStream(workingFile));
          byte[] imageData = new byte[in.available()];
          in.read(imageData);
          Image image = Toolkit.getDefaultToolkit().createImage(imageData);
          MediaTracker mediaTracker = new MediaTracker(new Container());
          mediaTracker.addImage(image, 0);
          mediaTracker.waitForID(0);
          int imageWidth = image.getWidth(null);
          int imageHeight = image.getHeight(null);

          in.close();
          in = null;
          // gets image width
          file.setHeight(imageHeight);
          file.setWidth(imageWidth);
        } catch (Exception e) {
          Logger.error(
              FileFactory.class, "Unable to read image " + workingFile + " : " + e.getMessage());
          throw new IOException(e);
        } finally {
          if (in != null) {
            try {
              in.close();
            } catch (Exception e) {

              Logger.error(FileFactory.class, "Unable to close image " + e.getMessage());
            }
          }
        }
      }
      // Wiping out the thumbnails and resized versions
      // http://jira.dotmarketing.net/browse/DOTCMS-5911
      String inode = file.getInode();
      if (UtilMethods.isSet(inode)) {
        String realAssetPath = APILocator.getFileAPI().getRealAssetPath();
        java.io.File tumbnailDir =
            new java.io.File(
                realAssetPath
                    + java.io.File.separator
                    + "dotGenerated"
                    + java.io.File.separator
                    + inode.charAt(0)
                    + java.io.File.separator
                    + inode.charAt(1));
        if (tumbnailDir != null) {
          java.io.File[] files = tumbnailDir.listFiles();
          if (files != null) {
            for (java.io.File iofile : files) {
              try {
                if (iofile.getName().startsWith("dotGenerated_")) {
                  iofile.delete();
                }
              } catch (SecurityException e) {
                Logger.error(
                    FileFactory.class,
                    "EditFileAction._saveWorkingFileData(): "
                        + iofile.getName()
                        + " cannot be erased. Please check the file permissions.");
              } catch (Exception e) {
                Logger.error(
                    FileFactory.class, "EditFileAction._saveWorkingFileData(): " + e.getMessage());
              }
            }
          }
        }
      }
    }
  }