Package com.dotmarketing.portlets.folders.model

Examples of com.dotmarketing.portlets.folders.model.Folder


  }

  public static FileAsset saveTempFile(User user, Host host, java.io.File uploadedFile, String folderPath, String title) throws Exception {

    Folder folder = APILocator.getFolderAPI().findFolderByPath(folderPath, host,user,false);

    byte[] bytes = FileUtil.getBytes(uploadedFile);

    if (bytes!=null) {

      String name = UtilMethods.getFileName(title);
      int counter = 1;
      while(APILocator.getFileAPI().fileNameExists(folder, name)) {
        name = name + counter;
        counter++;
      }

      Contentlet cont = new Contentlet();
      cont.setStructureInode(folder.getDefaultFileType());
      cont.setStringProperty(FileAssetAPI.TITLE_FIELD, UtilMethods.getFileName(name));
      cont.setFolder(folder.getInode());
      cont.setHost(host.getIdentifier());
      cont.setBinary(FileAssetAPI.BINARY_FIELD, uploadedFile);
      APILocator.getContentletAPI().checkin(cont, user,false);
      APILocator.getVersionableAPI().setLive(cont);
      return APILocator.getFileAssetAPI().fromContentlet(cont);
View Full Code Here


          Host host = APILocator.getHostAPI().find(value, APILocator.getUserAPI().getSystemUser(), false);
          if(host!=null && InodeUtils.isSet(host.getIdentifier())){
            contentlet.setHost(host.getIdentifier());
            contentlet.setFolder(FolderAPI.SYSTEM_FOLDER);
          }else{
            Folder folder = APILocator.getFolderAPI().find(value, APILocator.getUserAPI().getSystemUser(), false);
            if(folder!=null && InodeUtils.isSet(folder.getInode())){
              contentlet.setHost(folder.getHostId());
              contentlet.setFolder(folder.getInode());
            }
          }
        }else if(field.getFieldType().equals(Field.FieldType.MULTI_SELECT.toString()) || field.getFieldType().equals(Field.FieldType.CHECKBOX.toString())){
          if (field.getFieldContentlet().startsWith("float") || field.getFieldContentlet().startsWith("integer")) {
            value = values[0];
View Full Code Here

   * @throws Exception
   */
  @SuppressWarnings("unchecked")
  private static FileAsset saveFile(User user, Host host, java.io.File uploadedFile, String folderPath, String title) throws Exception {

    Folder folder = APILocator.getFolderAPI().findFolderByPath(folderPath, host,APILocator.getUserAPI().getSystemUser(),false);
    if(!InodeUtils.isSet(folder.getInode() )){
      folder = APILocator.getFolderAPI().createFolders(folderPath, host,user,false);
      Permission newPerm = new Permission();
      newPerm.setPermission(perAPI.PERMISSION_PUBLISH);
      newPerm.setRoleId(APILocator.getRoleAPI().loadCMSAnonymousRole().getId());
      newPerm.setInode(folder.getInode());
      perAPI.save(newPerm, folder, APILocator.getUserAPI().getSystemUser(), false);
    }

    byte[] bytes = FileUtil.getBytes(uploadedFile);

    if (bytes!=null) {

      String name = UtilMethods.getFileName(title);
      int counter = 1;
      while(fileAPI.fileNameExists(folder, name)) {
        name = name + counter;
        counter++;
      }
      while(APILocator.getFileAssetAPI().fileNameExists(host,folder, name, "")) {
        name = name + counter;
        counter++;
      }
     
      Contentlet cont = new Contentlet();
      cont.setStructureInode(folder.getDefaultFileType());
      cont.setStringProperty(FileAssetAPI.TITLE_FIELD, UtilMethods.getFileName(name));
      cont.setFolder(folder.getInode());
      cont.setHost(host.getIdentifier());
      cont.setBinary(FileAssetAPI.BINARY_FIELD, uploadedFile);
      APILocator.getContentletAPI().checkin(cont, user,false);
      APILocator.getVersionableAPI().setLive(cont);
      return APILocator.getFileAssetAPI().fromContentlet(cont);
View Full Code Here

          if(!path.endsWith("/"))
            path += "/";
          if(path.contains("mvdest2")){
            Logger.debug(this, "is mvdest2 a folder");
          }
          Folder folder = new Folder();
          try {
            folder = folderAPI.findFolderByPath(path, host,user,false);
          } catch (Exception e) {
            Logger.debug(this, "unable to find folder " + path );
            //throw new IOException(e.getMessage());
          }
          if (InodeUtils.isSet(folder.getInode())) {
            returnValue = true;
          }
        }
      }
    }
View Full Code Here

      Logger.debug(this, "isResource Method: host id is " + host.getIdentifier() + " and the host name is " + host.getHostname());
    }
    // Folder
    String path = getPath(uri);
    String folderName = getFolderName(path);
    Folder folder;
    try {
      folder = folderAPI.findFolderByPath(folderName, host, user,false);
    } catch (Exception e) {
      Logger.error(DotWebdavHelper.class, e.getMessage(), e);
      throw new IOException(e.getMessage());
    }
    if(folder!=null && InodeUtils.isSet(folder.getInode())) {
        // FileName
        String fileName = getFileName(path);
        fileName = deleteSpecialCharacter(fileName);

        if (InodeUtils.isSet(host.getInode())) {
View Full Code Here

  public Folder loadFolder(String url,User user) throws IOException{
    url = stripMapping(url);
    String hostName = getHostname(url);
    url = getPath(url);
    Host host;
    Folder folder;
    try {
      host = hostAPI.findByName(hostName, user, false);
      folder = folderAPI.findFolderByPath(url, host,user, false);
    } catch (DotDataException e) {
      Logger.error(DotWebdavHelper.class, e.getMessage(), e);
View Full Code Here

      sourceHost = hostAPI.findByName(sourceHostName, user, false);



      Folder sourceFolder = folderAPI.findFolderByPath(sourceFolderName + "/", sourceHost,user,false);
      // Destination
      String destinationHostName = getHostname(destinationPath);
      String destinationFolderName = getPath(destinationPath);
      // String destinationFolderName =
      // DotCMSStorage.getFolderName(destinationPath);
      Host destinationHost;

      destinationHost = hostAPI.findByName(destinationHostName, user, false);


      Folder destinationFolder = folderAPI.findFolderByPath(destinationFolderName + "/", destinationHost,user,false);

      // Delete the new permissions
      perAPI.removePermissions(destinationFolder);

      // Copy the new permissions
View Full Code Here

      Host host;

      host = hostAPI.findByName(hostName, user, false);

      Folder folder = folderAPI.findFolderByPath(folderName, host,user,false);
      boolean hasPermission = false;

      hasPermission = (!folderName.equals("/") && perAPI.doesUserHavePermission(folder, PERMISSION_CAN_ADD_CHILDREN, user, false))
                      || (folderName.equals("/") && perAPI.doesUserHavePermission(host, PERMISSION_CAN_ADD_CHILDREN, user, false));

      if (hasPermission) {
        // Check the folder filters
        if (!checkFolderFilter(folder, fileName)) {
          throw new IOException("The file doesn't comply the folder's filter");
        }

        if (host != null && InodeUtils.isSet(host.getInode())&& InodeUtils.isSet(folder.getInode())) {

          Identifier identifier = APILocator.getIdentifierAPI().find(host,path);

          File file = new File();
          file.setTitle(fileName);
View Full Code Here

    } catch (DotSecurityException e) {
      Logger.error(DotWebdavHelper.class, e.getMessage(), e);
      throw new IOException(e.getMessage());
    }

    Folder folder = new Folder();
    try {
      folder = folderAPI.findFolderByPath(folderName, host,user,false);
    } catch (Exception e2) {
      Logger.error(this, e2.getMessage(), e2);
    }
    if (host != null && InodeUtils.isSet(host.getInode()) && InodeUtils.isSet(folder.getInode())) {
      IFileAsset destinationFile = null;
      java.io.File workingFile = null;
      Folder parent = null;
      Contentlet fileAssetCont = null;
      Identifier identifier  = APILocator.getIdentifierAPI().find(host, path);
      if(identifier!=null && InodeUtils.isSet(identifier.getId()) && identifier.getAssetType().equals("contentlet")){
        fileAssetCont = APILocator.getContentletAPI().findContentletByIdentifier(identifier.getId(), false, APILocator.getLanguageAPI().getDefaultLanguage().getId(), user, false);
        workingFile = fileAssetCont.getBinary(FileAssetAPI.BINARY_FIELD);
        destinationFile = APILocator.getFileAssetAPI().fromContentlet(fileAssetCont);
        parent = APILocator.getFolderAPI().findFolderByPath(identifier.getParentPath(), host, user, false);

        if(fileAssetCont.isArchived()) {
            APILocator.getContentletAPI().unarchive(fileAssetCont, user, false);
        }
      }else if(identifier!=null && InodeUtils.isSet(identifier.getId())){
        destinationFile = fileAPI.getFileByURI(path, host, false, user, false);
        // inode{1}/inode{2}/inode.file_extension
        workingFile = fileAPI.getAssetIOFile((File)destinationFile);

        if(destinationFile.isArchived()) {
            WebAssetFactory.unArchiveAsset((File)destinationFile);
        }
      }

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

      InputStream is = content;
      byte[] currentData = IOUtils.toByteArray(is);

      if(destinationFile==null){
        Contentlet fileAsset = new Contentlet();
        Structure faStructure = StructureCache.getStructureByInode(folder.getDefaultFileType());
        Field fieldVar = faStructure.getFieldVar(FileAssetAPI.BINARY_FIELD);
        fileAsset.setStructureInode(folder.getDefaultFileType());
        fileAsset.setFolder(folder.getInode());
        if (currentData != null) {
          java.io.File tempUserFolder = new java.io.File(APILocator.getFileAPI().getRealAssetPathTmpBinary() + java.io.File.separator + user.getUserId() +
              java.io.File.separator + fieldVar.getFieldContentlet());
          if (!tempUserFolder.exists())
            tempUserFolder.mkdirs();

          java.io.File fileData = new java.io.File(tempUserFolder.getAbsolutePath() + java.io.File.separator + fileName);
          if(fileData.exists())
            fileData.delete();
          // Saving the new working data
          FileChannel writeCurrentChannel = new FileOutputStream(fileData).getChannel();
          writeCurrentChannel.truncate(0);
          ByteBuffer buffer = ByteBuffer.allocate(currentData.length);
          buffer.put(currentData);
          buffer.position(0);
          writeCurrentChannel.write(buffer);
          writeCurrentChannel.force(false);
          writeCurrentChannel.close();
          fileAsset.setStringProperty(FileAssetAPI.TITLE_FIELD, fileName);
          fileAsset.setStringProperty(FileAssetAPI.FILE_NAME_FIELD, fileName);
          fileAsset.setBinary(FileAssetAPI.BINARY_FIELD, fileData);
          fileAsset.setHost(host.getIdentifier());
          fileAsset=APILocator.getContentletAPI().checkin(fileAsset, user, false);

          //Validate if the user have the right permission before
          if(isAutoPub && !perAPI.doesUserHavePermission(fileAsset, PermissionAPI.PERMISSION_PUBLISH, user) ){
            APILocator.getContentletAPI().archive(fileAsset, APILocator.getUserAPI().getSystemUser(), false);
            APILocator.getContentletAPI().delete(fileAsset, APILocator.getUserAPI().getSystemUser(), false);
            throw new DotSecurityException("User does not have permission to publish contentlets");
              }else if(!isAutoPub && !perAPI.doesUserHavePermission(fileAsset, PermissionAPI.PERMISSION_EDIT, user)){
                APILocator.getContentletAPI().archive(fileAsset, APILocator.getUserAPI().getSystemUser(), false);
            APILocator.getContentletAPI().delete(fileAsset, APILocator.getUserAPI().getSystemUser(), false);
            throw new DotSecurityException("User does not have permission to edit contentlets");
              }
          if(isAutoPub && perAPI.doesUserHavePermission(fileAsset, PermissionAPI.PERMISSION_PUBLISH, user)) {
              APILocator.getContentletAPI().publish(fileAsset, user, false);

              Date currentDate = new Date();
              fileResourceCache.add(resourceUri + "|" + user.getUserId(), currentDate.getTime());

          }
        }
      }else{

        if(destinationFile instanceof File){
          // Save the file size
          File file = fileAPI.getFileByURI(path, host, false, user, false);
          file.setSize(currentData.length);
          file.setModDate(modifiedDate);
          file.setModUser(user.getUserId());
          try {
            HibernateUtil.saveOrUpdate(file);
          } catch (DotHibernateException e1) {
            Logger.error(this,e1.getMessage(), e1);
          }
        }

        if (currentData != null) {
          // Saving the new working data
            java.io.File fileData;
            if(destinationFile instanceof File) {
                fileData=workingFile;
            }
            else {
              Structure faStructure = StructureCache.getStructureByInode(folder.getDefaultFileType());
              Field fieldVar = faStructure.getFieldVar(FileAssetAPI.BINARY_FIELD);
              java.io.File tempUserFolder = new java.io.File(APILocator.getFileAPI().getRealAssetPathTmpBinary() + java.io.File.separator + user.getUserId() +
                  java.io.File.separator + fieldVar.getFieldContentlet());
              if (!tempUserFolder.exists())
                tempUserFolder.mkdirs();

              fileData = new java.io.File(tempUserFolder.getAbsolutePath() + java.io.File.separator + fileName);
              if(fileData.exists())
                fileData.delete();
            }
          // Saving the new working data
          FileChannel writeCurrentChannel = new FileOutputStream(fileData).getChannel();
          writeCurrentChannel.truncate(0);
          ByteBuffer buffer = ByteBuffer.allocate(currentData.length);
          buffer.put(currentData);
          buffer.position(0);
          writeCurrentChannel.write(buffer);
          writeCurrentChannel.force(false);
          writeCurrentChannel.close();
          Logger.debug(this, "WEBDAV fileName:" + fileName + ":" + fileData.getAbsolutePath());

          if(destinationFile instanceof File){
            // checks if it's an image
            if (UtilMethods.isImage(fileName) && workingFile != null) {
              try {
                // gets image height
                BufferedImage img = javax.imageio.ImageIO.read(workingFile);
                if(img != null){
                  int height = img.getHeight();
                  ((File)destinationFile).setHeight(height);
                  // gets image width
                  int width = img.getWidth();
                  ((File)destinationFile).setWidth(width);
                }
              } catch (Exception ioe) {
                Logger.error(this.getClass(), ioe.getMessage(), ioe);
              }

            }

          }else{
            fileAssetCont.setInode(null);
            fileAssetCont.setFolder(parent.getInode());
            fileAssetCont.setBinary(FileAssetAPI.BINARY_FIELD, fileData);
            fileAssetCont = APILocator.getContentletAPI().checkin(fileAssetCont, user, false);
            if(isAutoPub && perAPI.doesUserHavePermission(fileAssetCont, PermissionAPI.PERMISSION_PUBLISH, user))
                APILocator.getContentletAPI().publish(fileAssetCont, user, false);
          }
View Full Code Here

      }
    }
  }

  public Folder createFolder(String folderUri, User user) throws IOException, DotDataException {
    Folder folder = null;
    folderUri = stripMapping(folderUri);
    PermissionAPI perAPI = APILocator.getPermissionAPI();
    Logger.debug(this.getClass(), "createFolder");
    String hostName = getHostname(folderUri);
    String path = getPath(folderUri);

    Host host;
    try {
      host = hostAPI.findByName(hostName, user, false);
    } catch (DotDataException e) {
      Logger.error(DotWebdavHelper.class, e.getMessage(), e);
      throw new IOException(e.getMessage());
    } catch (DotSecurityException e) {
      Logger.error(DotWebdavHelper.class, e.getMessage(), e);
      throw new IOException(e.getMessage());
    }

    // CheckPermission
    List<Permission> parentPermissions = new ArrayList<Permission>();
    boolean hasPermission = false;
    boolean validName = true;
    String parentPath = getFolderName(path);
    if (UtilMethods.isSet(parentPath) && !parentPath.equals("/")) {
      Folder parentFolder;
      try {
        parentFolder = folderAPI.findFolderByPath(parentPath,host,user,false);
        hasPermission = perAPI.doesUserHavePermission(parentFolder,  PERMISSION_CAN_ADD_CHILDREN, user, false);
      } catch (Exception e) {
        Logger.error(DotWebdavHelper.class,e.getMessage(),e);
View Full Code Here

TOP

Related Classes of com.dotmarketing.portlets.folders.model.Folder

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.