Package com.dotmarketing.portlets.fileassets.business

Examples of com.dotmarketing.portlets.fileassets.business.IFileAsset


                    nav.setTarget(itemLink.getTarget());
                    nav.setPermissionId(itemLink.getPermissionId());
                    children.add(nav);
                }
                else if(item instanceof IFileAsset) {
                    IFileAsset itemFile=(IFileAsset)item;
                    ident=APILocator.getIdentifierAPI().find(itemFile.getPermissionId());
                    NavResult nav=new NavResult(folder.getInode(),host.getIdentifier());
                    nav.setTitle(itemFile.getFriendlyName());
                    nav.setHref(ident.getURI());
                    nav.setOrder(itemFile.getMenuOrder());
                    nav.setType("file");
                    nav.setPermissionId(itemFile.getPermissionId());
                    children.add(nav);
                }
            }
           
            navCache.putNav(host.getIdentifier(), folder.getInode(), result);
View Full Code Here


            Folder folder = APILocator.getFolderAPI().findFolderByPath(pluginFolderPath + "/" + filePath,host,APILocator.getUserAPI().getSystemUser(),false);
            if( !InodeUtils.isSet(folder.getInode())){     
              folder = APILocator.getFolderAPI().createFolders(pluginFolderPath + "/" + filePath, host,APILocator.getUserAPI().getSystemUser(),false);
            }
            //GetPrevious version if exists
            IFileAsset currentFile = null;
            Identifier currentId = APILocator.getIdentifierAPI().find(host, pluginFolderPath+"/"+filePathAndName);
            if(currentId!=null && InodeUtils.isSet(currentId.getId()) && currentId.getAssetType().equals("contentlet")){
              Contentlet cont = APILocator.getContentletAPI().findContentletByIdentifier(currentId.getId(), true, APILocator.getLanguageAPI().getDefaultLanguage().getId(), APILocator.getUserAPI().getSystemUser(),false);
              if(cont!=null && InodeUtils.isSet(cont.getInode())){
                currentFile = APILocator.getFileAssetAPI().fromContentlet(cont);
                cont.setStringProperty(FileAssetAPI.TITLE_FIELD, UtilMethods.getFileName(fileName));
                cont.setFolder(folder.getInode());
                cont.setHost(host.getIdentifier());
                cont.setBinary(FileAssetAPI.BINARY_FIELD, temporaryFile);
                APILocator.getContentletAPI().checkin(cont, APILocator.getUserAPI().getSystemUser(),false);
                APILocator.getVersionableAPI().setWorking(cont);
                APILocator.getVersionableAPI().setLive(cont);
                if (cont.isLive()){
                  LiveCache.removeAssetFromCache(cont);
                  LiveCache.addToLiveAssetToCache(cont);
                }else{
                  LiveCache.removeAssetFromCache(cont);
                  LiveCache.addToLiveAssetToCache(cont);
                }
                WorkingCache.removeAssetFromCache(cont);
                WorkingCache.addToWorkingAssetToCache(cont);
              }
            }else if(currentId!=null && InodeUtils.isSet(currentId.getId())){
              currentFile = APILocator.getFileAPI().getFileByURI(pluginFolderPath+"/"+filePathAndName, host, true, APILocator.getUserAPI().getSystemUser(),false);
              com.dotmarketing.portlets.files.model.File file = new com.dotmarketing.portlets.files.model.File();
              file.setFileName(fileName);
              file.setFriendlyName(UtilMethods.getFileName(fileName));
              file.setTitle(UtilMethods.getFileName(fileName));
              file.setMimeType(APILocator.getFileAPI().getMimeType(fileName));
              file.setOwner(systemUser.getUserId());
              file.setModUser(systemUser.getUserId());
              file.setModDate(new Date());
              file.setParent(folder.getIdentifier());
              file.setSize((int)temporaryFile.length());
             
              HibernateUtil.saveOrUpdate(file);
              APILocator.getFileAPI().invalidateCache(file);
              // get the file Identifier
              Identifier ident = null;
              if (InodeUtils.isSet(currentFile.getInode())){
                ident = APILocator.getIdentifierAPI().find((com.dotmarketing.portlets.files.model.File)currentFile);
                APILocator.getFileAPI().invalidateCache((com.dotmarketing.portlets.files.model.File)currentFile);
              }else{
                ident = new Identifier();
              }
View Full Code Here

    } catch (Exception e1) {
      Logger.error(FileTool.class,e1.getMessage(),e1);
      return new File();
    }
        p = p.substring(5, p.lastIndexOf("."));
        IFileAsset file = null;
    try {
      if(id!=null && InodeUtils.isSet(id.getId()) && id.getAssetType().equals("contentlet")){
        Contentlet cont = APILocator.getContentletAPI().findContentletByIdentifier(id.getId(), true, APILocator.getLanguageAPI().getDefaultLanguage().getId(), APILocator.getUserAPI().getSystemUser(), false);
        if(cont!=null && InodeUtils.isSet(cont.getInode())){
          file = APILocator.getFileAssetAPI().fromContentlet(cont);
View Full Code Here

    }
        List<HashMap<String, IFileAsset>> result = new ArrayList<HashMap<String, IFileAsset>>();
        HashMap<String, IFileAsset> resultFile;
        List<IFileAsset> noPhotoFile = new ArrayList<IFileAsset>();
        int pos;
        IFileAsset fileListFile;
        String videoPhotoFileName;

        for (IFileAsset file: fileList) {
          resultFile = new HashMap<String, IFileAsset>();
      if (file.getExtension().toLowerCase().endsWith("flv")) {
        resultFile.put("movie", file);

        videoPhotoFileName = file.getURI().substring(0,file.getURI().length()-4) + ".jpg";
        for (pos = 0; pos < fileList.size(); ++pos) {
          fileListFile = fileList.get(pos);
          if (videoPhotoFileName.equals(fileListFile.getURI())) {
            resultFile.put("photo", fileListFile);
            noPhotoFile.add(fileListFile);
            break;
          }
        }
View Full Code Here

       
      }

      String inode = null;

      IFileAsset file = null;
      Identifier identifier = null;
      boolean canRead = false;
      if(UtilMethods.isSet(relativePath) && relativePath.contains("fileAsset")){
        String[] splits = relativePath.split(Pattern.quote(File.separator));
        if(splits.length>0){
          inode = splits[3];
          Contentlet cont =  null;
          try{
            cont = APILocator.getContentletAPI().find(inode, user, true);
            file = APILocator.getFileAssetAPI().fromContentlet(cont);
            identifier = APILocator.getIdentifierAPI().find(cont);
            canRead = true;
          }catch(Exception e){ 
            Logger.warn(this, "Unable to find file asset contentlet with inode " + inode, e);
          }
        }
      }else if(UtilMethods.isSet(uri) && uri.contains("fileAsset")){
        String[] splits = uri.split(Pattern.quote(File.separator));
        if(splits.length>0){
          inode = splits[3];
          Contentlet cont =  null;
          try{
            cont = APILocator.getContentletAPI().find(inode, user, true);
            file = APILocator.getFileAssetAPI().fromContentlet(cont);
            identifier = APILocator.getIdentifierAPI().find(cont);
            canRead = true;
          }catch(Exception e){ 
            Logger.warn(this, "Unable to find file asset contentlet with inode " + inode, e);
          }
        }
      }else{
     
        inode = UtilMethods.getFileName(f.getName());
        file = APILocator.getFileAPI().find(inode, user, true);
        identifier = APILocator.getIdentifierAPI().find((com.dotmarketing.portlets.files.model.File)file);
        com.dotmarketing.portlets.files.model.File fProxy = new com.dotmarketing.portlets.files.model.File();
              fProxy.setIdentifier(identifier.getInode());
              canRead = permissionAPI.doesUserHavePermission(fProxy, PERMISSION_READ, user, true);
      }
//      Identifier identifier = APILocator.getIdentifierAPI().findFromInode(Long.parseLong(inode));
     

      String mimeType = APILocator.getFileAPI().getMimeType(f.getName());
      if (mimeType == null)
        mimeType = "application/octet-stream";

      response.setContentType(mimeType);

      //Checking permissions
          /**
           * Build a fake proxy file object so we
           * can get inheritable permissions on it
           * without having to hit cache or db
           */
           

            if (!canRead) {
              if(user == null){
                request.getSession(true).setAttribute(com.dotmarketing.util.WebKeys.REDIRECT_AFTER_LOGIN, request.getRequestURI());//DOTCMS-5682
                //Sending user to unauthorized the might send him to login
                response.sendError(401, "The requested file is unauthorized");
              }else{
                //sending the user to forbidden
                response.sendError(403, "The requested file is forbidden");
              }
               return;
            }
           
            response.setHeader("Content-Disposition","filename=\"" + file.getFileName() + "\"");

      /*
       * Setting the proper content headers
       */
      if(request.getParameter("dotcms_force_download") != null || request.getParameter("force_download") != null) {
        String url = request.getRequestURL().toString();
        String filename = url.substring(url.lastIndexOf("/") + 1, url.length());
        filename = file.getFileName();
        response.setHeader("Content-Type", "application/force-download");
        response.setHeader("Content-Disposition", "attachment; filename=\"" + filename + "\"");
      } else {
        boolean _adminMode = false;
        try {
            _adminMode = (session!=null && session.getAttribute(com.dotmarketing.util.WebKeys.ADMIN_MODE_SESSION) != null);
        }catch(Exception e){

        }

          // Set the expiration time
        if (!_adminMode) {

            if(!DownloadUtil.isModifiedEtag(request, response, inode, f.lastModified(), f.length())) {
                return;
            }

                /* if we are in ADMIN MODE, don't cache */
        }else{
            GregorianCalendar expiration = new GregorianCalendar();
          expiration.add(java.util.Calendar.MONTH, -1);
          response.setHeader("Expires", DownloadUtil.httpDate.get().format(expiration.getTime()));
          response.setHeader("Cache-Control", "max-age=-1");
        }

      }


      ServletOutputStream out = null;
      FileChannel from = null;
      WritableByteChannel to = null;
      RandomAccessFile input = null;
      try {
        out = response.getOutputStream();
        from = new FileInputStream(f).getChannel();
        to = Channels.newChannel(out);
        //DOTCMS-5716
        //32 MB at a time 
        int maxTransferSize = (32 * 1024 * 1024) ;
        long size = from.size();
        long position = 0;
       
        boolean responseSent = false;
        byte[] data = Files.toByteArray(f);
              //ServletOutputStream sos = response.getOutputStream();
        //extract range header
        String rangeHeader = request.getHeader("range");
        if(UtilMethods.isSet(rangeHeader)){
           response.setHeader("Accept-Ranges", "bytes");
          // Range header should match format "bytes=n-n,n-n,n-n...". If not, then return 416.
          if (!rangeHeader.matches("^bytes=\\d*-\\d*(,\\d*-\\d*)*$")) {
            response.setHeader("Content-Range", "bytes */" + data.length); // Required in 416.
            response.sendError(HttpServletResponse.SC_REQUESTED_RANGE_NOT_SATISFIABLE);
            return;
          }
          //parse multiple range bytes
          ArrayList<SpeedyAssetServletUtil.ByteRange> ranges = SpeedyAssetServletUtil.parseRange(rangeHeader, data.length);
          if (ranges != null){
            SpeedyAssetServletUtil.ByteRange full = new SpeedyAssetServletUtil.ByteRange(0, f.length() - 1, f.length());
            if (ranges.isEmpty() || ranges.get(0).equals(full)) {
              // Return full file.
              input = new RandomAccessFile(f, "r");
              SpeedyAssetServletUtil.ByteRange r = full;
              response.setContentType(file.getMimeType());
              response.setHeader("Content-Range", "bytes " + r.start + "-" + r.end + "/" + r.total);
              response.setHeader("Content-Length", String.valueOf(r.length));
              // Copy full range.
              SpeedyAssetServletUtil.copy(input, out, r.start, r.length);
            } else if (ranges.size() == 1){
              SpeedyAssetServletUtil.ByteRange range = ranges.get(0);
              input = new RandomAccessFile(f, "r");
              // Check if Range is syntactically valid. If not, then return 416.
              if (range.start > range.end) {
                response.setHeader("Content-Range", "bytes */" + data.length); // Required in 416.
                response.sendError(HttpServletResponse.SC_REQUESTED_RANGE_NOT_SATISFIABLE);
                return;
              }
              response.setContentType(file.getMimeType());
              response.setHeader("Content-Range", "bytes " + range.start + "-" + range.end + "/" + range.total);
              response.setHeader("Content-Length", String.valueOf(range.length));
                    response.setStatus(HttpServletResponse.SC_PARTIAL_CONTENT); // 206.
              SpeedyAssetServletUtil.copy(input, out, range.start, range.length);
            }else{
              response.setContentType("multipart/byteranges; boundary=" + SpeedyAssetServletUtil.MULTIPART_BOUNDARY);
              response.setStatus(HttpServletResponse.SC_PARTIAL_CONTENT);
                input = new RandomAccessFile(f, "r");
              for (SpeedyAssetServletUtil.ByteRange r : ranges) {
                if (r.start > r.end) {
                  response.setHeader("Content-Range", "bytes */" + data.length); // Required in 416.
                  response.sendError(HttpServletResponse.SC_REQUESTED_RANGE_NOT_SATISFIABLE);
                  return;
                }
                // Add multipart boundary and header fields for every range.
                out.println();
                out.println("--" + SpeedyAssetServletUtil.MULTIPART_BOUNDARY);
                out.println("Content-Type: " + file.getMimeType());
                out.println("Content-Range: bytes " + r.start + "-" + r.end + "/" + r.total);
                out.println();

                // Copy single part range of multi part range.
                SpeedyAssetServletUtil.copy(input, out, r.start, r.length);
View Full Code Here

            } catch (Exception e) {
                Logger.error(FolderResourceImpl.class,e.getMessage(),e);
                throw new DotRuntimeException(e.getMessage(), e);
            }
            try {
              IFileAsset f = null;
              dotDavHelper.setResourceContent(path + newName, in, contentType, null, java.util.Calendar.getInstance().getTime(), user, isAutoPub);
                f = dotDavHelper.loadFile(path + newName,user);
                FileResourceImpl fr = new FileResourceImpl(f, f.getFileName());
                return fr;
               
            }catch (DotSecurityException dotE){
              Logger.error(this, "An error occurred while creating new file: " + (newName != null ? newName : "Unknown")
                    + " in this path: " + (path != null ? path : "Unknown") + " "
View Full Code Here

            String fid = (String)conAPI.getFieldValue(content, f);
            if(!UtilMethods.isSet(fid)){
              return null;
            }
            Identifier i = APILocator.getIdentifierAPI().find(fid);
            IFileAsset file = null;
            String p = "";
            if (EDIT_OR_PREVIEW_MODE){
              p = WorkingCache.getPathFromCache(i.getURI(),InodeUtils.isSet(i.getHostId())?i.getHostId():host.getIdentifier());
            }else{
              p = LiveCache.getPathFromCache(i.getURI(),InodeUtils.isSet(i.getHostId())?i.getHostId():host.getIdentifier());
            }
            p = p.substring(5, p.lastIndexOf("."));
            if(i!=null && InodeUtils.isSet(i.getId()) && i.getAssetType().equals("contentlet")){
              return i.getPath();
            }
            file = fileAPI.find(p,user,false);
            if(file != null && UtilMethods.isSet(file.getInode())){
              return ((File)file).getURI();
            }else{
              return null;
            }
          }else{
            return null;
          }
        }else{
          return content.getMap().get(fieldVariableName);
        }
      }
      if(f != null && f.getFieldType().equals(Field.FieldType.CATEGORY.toString())){
        return perAPI.filterCollection(new ArrayList<Category>((Set<Category>)conAPI.getFieldValue(content, f)), PermissionAPI.PERMISSION_USE, true, user);
      }else if(f != null && (f.getFieldType().equals(Field.FieldType.FILE.toString()) || f.getFieldType().equals(Field.FieldType.IMAGE.toString()))){
        String fid = (String)conAPI.getFieldValue(content, f);
        if(!UtilMethods.isSet(fid)){
          return null;
        }
        Identifier i = APILocator.getIdentifierAPI().find(fid);
        FileMap fm = new FileMap();
        IFileAsset file = null;
        if (EDIT_OR_PREVIEW_MODE){
          String p = WorkingCache.getPathFromCache(i.getURI(), InodeUtils.isSet(i.getHostId())?i.getHostId():host.getIdentifier());
          p = p.substring(5, p.lastIndexOf("."));
          if(i!=null && InodeUtils.isSet(i.getId()) && i.getAssetType().equals("contentlet")){
            Contentlet fileAsset  = APILocator.getContentletAPI().find(p.substring(0, p.indexOf(java.io.File.separator)), user!=null?user:APILocator.getUserAPI().getAnonymousUser(), false);
            if(fileAsset != null && UtilMethods.isSet(fileAsset.getInode())){
                FileAssetMap fam=new FileAssetMap();
                            FileAsset fa=APILocator.getFileAssetAPI().fromContentlet(fileAsset);
                            fam.setBinary(FileAssetAPI.BINARY_FIELD, fa.getFileAsset());
                            BeanUtils.copyProperties(fam, fa);
                            return fam;
                //return new ContentMap(fileAsset, user, EDIT_OR_PREVIEW_MODE,host,context);
            }
          }else{
            file = fileAPI.find(p,user,true);
          }
//          file = (File)APILocator.getVersionableAPI().findWorkingVersion(i, File.class);
        }else{
          String p = LiveCache.getPathFromCache(i.getURI(),InodeUtils.isSet(i.getHostId())?i.getHostId():host.getIdentifier());
          p = p.substring(5, p.lastIndexOf("."));
          if(i!=null && InodeUtils.isSet(i.getId()) && i.getAssetType().equals("contentlet")){
            Contentlet fileAsset  = APILocator.getContentletAPI().find(p.substring(0, p.indexOf(java.io.File.separator)), user!=null?user:APILocator.getUserAPI().getAnonymousUser(), false);
            if(fileAsset != null && UtilMethods.isSet(fileAsset.getInode())){
              FileAssetMap fam=new FileAssetMap();
              FileAsset fa=APILocator.getFileAssetAPI().fromContentlet(fileAsset);
              fam.setBinary(FileAssetAPI.BINARY_FIELD, fa.getFileAsset());
              BeanUtils.copyProperties(fam, fa);
              return fam;
                //return new ContentMap(fileAsset, user, EDIT_OR_PREVIEW_MODE,host,context);
            }
          }else{
            file = fileAPI.find(p,user,true);
          }
//          file = (File) APILocator.getVersionableAPI().findLiveVersion(i, File.class);
        }
        if(file != null && UtilMethods.isSet(file.getInode())){
          BeanUtils.copyProperties(fm, file);
          return fm;
        }else{
          return null;
        }
View Full Code Here

TOP

Related Classes of com.dotmarketing.portlets.fileassets.business.IFileAsset

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.