コード例 #1
0
  /**
   * Copy a file into the given directory OR host
   *
   * @param file File to be copied
   * @param parent Destination Folder
   * @param host Destination host
   * @return true if copy success, false otherwise
   * @throws IOException
   * @throws DotHibernateException
   */
  private File copyFile(File file, Folder parent, Host host) throws DotDataException, IOException {

    File newFile = new File();

    newFile.copy(file);

    // gets filename before extension
    String fileName = com.dotmarketing.util.UtilMethods.getFileName(file.getFileName());
    // gets file extension
    String fileExtension = com.dotmarketing.util.UtilMethods.getFileExtension(file.getFileName());

    Boolean fileNameExists;
    if (parent != null) {
      fileNameExists = fileNameExists(parent, file.getFileName());
    } else {
      fileNameExists =
          fileNameExists(APILocator.getFolderAPI().findSystemFolder(), file.getFileName());
    }

    // Setting file name
    if (fileNameExists) {
      // adds "copy" word to the filename
      newFile.setFileName(fileName + "_copy." + fileExtension);
      newFile.setFriendlyName(file.getFriendlyName() + " (COPY) ");
    } else {
      newFile.setFileName(fileName + "." + fileExtension);
    }

    Identifier identifier;
    if (parent != null) {
      identifier = APILocator.getIdentifierAPI().createNew(newFile, parent);
    } else {
      identifier = APILocator.getIdentifierAPI().createNew(newFile, host);
    }
    newFile.setIdentifier(identifier.getInode());

    // persists the webasset
    HibernateUtil.saveOrUpdate(newFile);

    saveFileData(file, newFile, null);

    Logger.debug(FileFactory.class, "identifier=" + identifier.getURI());

    WorkingCache.removeAssetFromCache(newFile);
    WorkingCache.addToWorkingAssetToCache(newFile);
    PermissionAPI permissionAPI = APILocator.getPermissionAPI();

    try {
      APILocator.getVersionableAPI().setWorking(newFile);
      if (file.isLive()) APILocator.getVersionableAPI().setLive(newFile);
    } catch (DotStateException e) {
      Logger.error(this, e.getMessage());
    } catch (DotSecurityException e) {
      Logger.error(this, e.getMessage());
    }
    // Copy permissions
    permissionAPI.copyPermissions(file, newFile);

    return newFile;
  }
コード例 #2
0
  public java.io.File getAssetIOFile(File file) throws IOException {

    String fileName = file.getFileName();
    String suffix = UtilMethods.getFileExtension(fileName);

    String assetsPath = APILocator.getFileAPI().getRealAssetsRootPath();
    String fileInode = file.getInode();

    // creates the path where to save the working file based on the inode
    String fileFolderPath = String.valueOf(fileInode);
    if (fileFolderPath.length() == 1) {
      fileFolderPath = fileFolderPath + "0";
    }

    fileFolderPath =
        assetsPath
            + java.io.File.separator
            + fileFolderPath.substring(0, 1)
            + java.io.File.separator
            + fileFolderPath.substring(1, 2);

    new java.io.File(fileFolderPath).mkdirs();

    String filePath = fileFolderPath + java.io.File.separator + fileInode + "." + suffix;

    // creates the new file as
    // inode{1}/inode{2}/inode.file_extension
    java.io.File assetFile = new java.io.File(filePath);
    if (!assetFile.exists()) assetFile.createNewFile();

    return assetFile;
  }
コード例 #3
0
  @SuppressWarnings({"unchecked", "deprecation"})
  public boolean renameFile(File file, String newName)
      throws DotStateException, DotDataException, DotSecurityException {

    // getting old file properties
    String oldFileName = file.getFileName();
    String ext = UtilMethods.getFileExtension(oldFileName);
    Folder folder =
        APILocator.getFolderAPI()
            .findParentFolder(file, APILocator.getUserAPI().getSystemUser(), false);

    Identifier ident = APILocator.getIdentifierAPI().find(file);

    String newFileName = newName;
    if (UtilMethods.isSet(ext)) {
      newFileName = newFileName + "." + ext;
    }

    if (fileNameExists(folder, newFileName) || file.isLocked()) return false;

    List<Versionable> versions = APILocator.getVersionableAPI().findAllVersions(ident);

    boolean islive = false;

    for (Versionable version : versions) {
      File f = (File) version;

      // sets filename for this new file
      f.setFileName(newFileName);

      HibernateUtil.saveOrUpdate(f);

      if (f.isLive()) islive = true;
    }

    LiveCache.removeAssetFromCache(file);
    WorkingCache.removeAssetFromCache(file);
    CacheLocator.getIdentifierCache().removeFromCacheByVersionable(file);

    ident.setURI(APILocator.getIdentifierAPI().find(folder).getPath() + newFileName);
    // HibernateUtil.saveOrUpdate(ident);
    APILocator.getIdentifierAPI().save(ident);

    if (islive) {
      LiveCache.removeAssetFromCache(file);
      LiveCache.addToLiveAssetToCache(file);
    }
    WorkingCache.removeAssetFromCache(file);
    WorkingCache.addToWorkingAssetToCache(file);
    CacheLocator.getIdentifierCache().removeFromCacheByVersionable(file);

    // RefreshMenus.deleteMenus();
    RefreshMenus.deleteMenu(file);
    CacheLocator.getNavToolCache().removeNav(folder.getHostId(), folder.getInode());

    return true;
  }
コード例 #4
0
ファイル: TemplateAjax.java プロジェクト: 4DvAnCeBoY/dotCMS
 public Map<String, Object> fetchTemplateImage(String id)
     throws DotDataException, DotSecurityException {
   Map<String, Object> toReturn = new HashMap<String, Object>();
   Template template = null;
   try {
     template =
         templateAPI.findWorkingTemplate(id, APILocator.getUserAPI().getSystemUser(), false);
   } catch (DotSecurityException e) {
     Logger.error(this, e.getMessage());
   }
   if (template != null) {
     Identifier imageIdentifier = APILocator.getIdentifierAPI().find(template.getImage());
     if (UtilMethods.isSet(imageIdentifier.getAssetType())
         && imageIdentifier.getAssetType().equals("contentlet")) {
       Contentlet imageContentlet = TemplateFactory.getImageContentlet(template);
       if (imageContentlet != null) {
         toReturn.put("inode", imageContentlet.getInode());
         toReturn.put("name", imageContentlet.getTitle());
         toReturn.put("identifier", imageContentlet.getIdentifier());
         toReturn.put(
             "extension",
             com.dotmarketing.util.UtilMethods.getFileExtension(imageContentlet.getTitle()));
       }
     } else {
       File imgFile = TemplateFactory.getImageFile(template);
       if (imgFile != null) {
         toReturn.put("inode", imgFile.getInode());
         toReturn.put("name", imgFile.getFileName());
         toReturn.put("identifier", imgFile.getIdentifier());
         toReturn.put(
             "extension",
             com.dotmarketing.util.UtilMethods.getFileExtension(imgFile.getFileName()));
       }
     }
   }
   return toReturn;
 }
コード例 #5
0
  /**
   * This method get the File date from a dotmarketing file object
   *
   * @param file dotmarketing File object
   * @return Java io.File
   * @throws IOException
   */
  private static java.io.File getAssetIOFile(com.dotmarketing.portlets.files.model.File file)
      throws IOException {

    String fileName = file.getFileName();
    String suffix = UtilMethods.getFileExtension(fileName);

    String assetsPath = APILocator.getFileAPI().getRealAssetsRootPath();
    String fileInode = file.getInode();

    // creates the path where to save the working file based on the inode
    Logger.debug(
        CMSMaintenanceFactory.class,
        "Creating path to save the working file with inode: " + fileInode);
    String fileFolderPath = String.valueOf(fileInode);
    if (fileFolderPath.length() == 1) {
      fileFolderPath = fileFolderPath + "0";
    }

    fileFolderPath =
        assetsPath
            + java.io.File.separator
            + fileFolderPath.substring(0, 1)
            + java.io.File.separator
            + fileFolderPath.substring(1, 2);

    new java.io.File(fileFolderPath).mkdirs();

    String filePath = fileFolderPath + java.io.File.separator + fileInode + "." + suffix;

    // creates the new file as
    // inode{1}/inode{2}/inode.file_extension
    java.io.File assetFile = new java.io.File(filePath);
    if (!assetFile.exists())
      Logger.debug(CMSMaintenanceFactory.class, "Creating new file in:  " + filePath);
    assetFile.createNewFile();

    return assetFile;
  }
コード例 #6
0
  /**
   * Moves a file into the given directory OR host
   *
   * @param file File to be moved
   * @param parent Destination Folder
   * @param host Destination Host
   * @return true if move success, false otherwise
   * @throws DotDataException
   * @throws DotStateException
   * @throws DotSecurityException
   */
  private Boolean moveFile(File file, Folder parent, Host host)
      throws DotStateException, DotDataException, DotSecurityException {

    HostAPI hostAPI = APILocator.getHostAPI();

    // Find file identifier
    Identifier identifier = com.dotmarketing.business.APILocator.getIdentifierAPI().find(file);

    // gets working container
    File workingWebAsset =
        (File)
            APILocator.getVersionableAPI()
                .findWorkingVersion(identifier, APILocator.getUserAPI().getSystemUser(), false);
    // gets live container
    File liveWebAsset =
        (File)
            APILocator.getVersionableAPI()
                .findLiveVersion(identifier, APILocator.getUserAPI().getSystemUser(), false);

    // checks if another identifer with the same name exists in the same
    Boolean fileNameExists;
    if (parent != null) {
      fileNameExists = fileNameExists(parent, file.getFileName());
    } else {
      fileNameExists =
          fileNameExists(APILocator.getFolderAPI().findSystemFolder(), file.getFileName());
    }
    if (fileNameExists) {
      return false;
    }

    // assets cache
    if ((liveWebAsset != null) && (InodeUtils.isSet(liveWebAsset.getInode()))) {
      LiveCache.removeAssetFromCache(liveWebAsset);
    }
    WorkingCache.removeAssetFromCache(workingWebAsset);

    // gets old parent
    Folder oldParent =
        APILocator.getFolderAPI()
            .findParentFolder(workingWebAsset, APILocator.getUserAPI().getSystemUser(), false);

    /*oldParent.deleteChild(workingWebAsset);
    if ((liveWebAsset != null) && (InodeUtils.isSet(liveWebAsset.getInode()))) {
        oldParent.deleteChild(liveWebAsset);
    }
    //add new Parent
    parent.addChild(workingWebAsset);
    if ((liveWebAsset != null) && (InodeUtils.isSet(liveWebAsset.getInode()))) {
        parent.addChild(liveWebAsset);
    }*/

    // gets identifier for this webasset and changes the uri and persists it
    User systemUser;
    try {
      systemUser = APILocator.getUserAPI().getSystemUser();
      if (host == null) {
        host = hostAPI.findParentHost(parent, systemUser, false);
      }
    } catch (DotDataException e) {
      Logger.error(FileFactory.class, e.getMessage(), e);
      throw new DotRuntimeException(e.getMessage(), e);

    } catch (DotSecurityException e) {
      Logger.error(FileFactory.class, e.getMessage(), e);
      throw new DotRuntimeException(e.getMessage(), e);
    }
    identifier.setHostId(host.getIdentifier());
    identifier.setURI(parent != null ? workingWebAsset.getURI(parent) : workingWebAsset.getURI());
    // HibernateUtil.saveOrUpdate(identifier);
    APILocator.getIdentifierAPI().save(identifier);

    if (UtilMethods.isSet(liveWebAsset))
      CacheLocator.getIdentifierCache().removeFromCacheByVersionable(liveWebAsset);
    //		IdentifierCache.addAssetToIdentifierCache(liveWebAsset);

    // Add to Preview and Live Cache
    if ((liveWebAsset != null) && (InodeUtils.isSet(liveWebAsset.getInode()))) {
      LiveCache.removeAssetFromCache(liveWebAsset);
      LiveCache.addToLiveAssetToCache(liveWebAsset);
    }
    WorkingCache.removeAssetFromCache(workingWebAsset);
    WorkingCache.addToWorkingAssetToCache(workingWebAsset);

    if (file.isShowOnMenu()) {
      // existing folder with different show on menu ... need to regenerate menu
      if (parent != null) {
        RefreshMenus.deleteMenu(oldParent, parent);
        CacheLocator.getNavToolCache().removeNav(parent.getHostId(), parent.getInode());
      } else {
        RefreshMenus.deleteMenu(oldParent);
      }
      CacheLocator.getNavToolCache().removeNav(oldParent.getHostId(), oldParent.getInode());
    }

    return true;
  }
コード例 #7
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());
              }
            }
          }
        }
      }
    }
  }