⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cmsfckeditorfilebrowser.java

📁 OpenCms 是一个J2EE的产品
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
    }

    /**
     * Returns the Type parameter.<p>
     * 
     * @return the Type parameter
     */
    public String getParamType() {

        return m_paramType;
    }

    /**
     * Sets the Command parameter.<p>
     * 
     * @param paramCommand the Command parameter
     */
    public void setParamCommand(String paramCommand) {

        m_paramCommand = paramCommand;
    }

    /**
     * Sets the CurrentFolder parameter.<p>
     * 
     * @param paramCurrentFolder the CurrentFolder parameter
     */
    public void setParamCurrentFolder(String paramCurrentFolder) {

        if (CmsStringUtil.isEmpty(paramCurrentFolder)) {
            m_paramCurrentFolder = "/";
        } else {
            m_paramCurrentFolder = paramCurrentFolder;
        }
    }

    /**
     * Sets the NewFolderName parameter.<p>
     * 
     * @param paramNewFolderName the NewFolderName parameter
     */
    public void setParamNewFolderName(String paramNewFolderName) {

        m_paramNewFolderName = paramNewFolderName;
    }

    /**
     * Sets the ServerPath parameter.<p>
     * 
     * @param paramServerPath the ServerPath parameter
     */
    public void setParamServerPath(String paramServerPath) {

        if (CmsStringUtil.isEmpty(paramServerPath)) {
            m_paramServerPath = OpenCms.getSystemInfo().getOpenCmsContext() + getParamCurrentFolder();
        } else {
            m_paramServerPath = OpenCms.getSystemInfo().getOpenCmsContext() + paramServerPath;
        }
    }

    /**
     * Sets the Type parameter.<p>
     * 
     * @param paramType the Type parameter
     */
    public void setParamType(String paramType) {

        if (CmsStringUtil.isEmpty(paramType)) {
            m_paramType = "";
        } else {
            m_paramType = paramType;
        }
    }

    /**
     * Creates a folder in the file browser and returns the XML containing the error code.<p>
     * 
     * @return the XML containing the error code for the folder creation
     */
    protected String createFolder() {

        createXMLHead();
        Element error = getDocument().getRootElement().addElement(NODE_ERROR);
        try {
            getCms().createResource(
                getParamCurrentFolder() + getParamNewFolderName(),
                CmsResourceTypeFolder.RESOURCE_TYPE_ID);
            // no error occured, return error code 0
            error.addAttribute(ATTR_NUMBER, ERROR_CREATEFOLDER_OK);
        } catch (Exception e) {
            // check cause of error to return a specific error code
            if (e instanceof CmsVfsResourceAlreadyExistsException) {
                // resource already exists
                error.addAttribute(ATTR_NUMBER, ERROR_CREATEFOLDER_EXISTS);
            } else if (e instanceof CmsIllegalArgumentException) {
                // invalid folder name
                error.addAttribute(ATTR_NUMBER, ERROR_CREATEFOLDER_INVALIDNAME);
            } else if (e instanceof CmsPermissionViolationException) {
                // no permissions to create the folder
                error.addAttribute(ATTR_NUMBER, ERROR_CREATEFOLDER_NOPERMISSIONS);
            } else {
                // unknown error
                error.addAttribute(ATTR_NUMBER, ERROR_CREATEFOLDER_UNKNOWNERROR);
            }

        }

        try {
            return CmsXmlUtils.marshal(getDocument(), CmsEncoder.ENCODING_UTF_8);
        } catch (CmsException e) {
            // should never happen
            return "";
        }
    }

    /**
     * Creates the XML head that is used for every XML file browser response except the upload response.<p>
     */
    protected void createXMLHead() {

        // add the connector node
        Element connector = getDocument().addElement(NODE_CONNECTOR);
        connector.addAttribute(ATTR_COMMAND, getParamCommand());
        connector.addAttribute(ATTR_RESOURCETYPE, getParamType());
        Element currFolder = connector.addElement(NODE_CURRENTFOLDER);
        currFolder.addAttribute(ATTR_PATH, getParamCurrentFolder());
        currFolder.addAttribute(ATTR_URL, getParamServerPath());

    }

    /**
     * Returns the XML document instance that is used to build the response XML.<p>
     * 
     * @return the XML document instance that is used to build the response XML
     */
    protected Document getDocument() {

        if (m_document == null) {
            m_document = DocumentHelper.createDocument();
        }
        return m_document;
    }

    /**
     * Returns the XML to list folders and/or files in the file browser window.<p>
     * 
     * @param includeFiles flag to indicate if files are included
     * @return the XML to list folders and/or files in the file browser window
     */
    protected String getFolders(boolean includeFiles) {

        createXMLHead();
        Element folders = getDocument().getRootElement().addElement(NODE_FOLDERS);
        Element files = null;

        // generate resource filter
        CmsResourceFilter filter;
        if (includeFiles) {
            // create filter to get folders and files
            filter = CmsResourceFilter.DEFAULT.addRequireVisible();
            files = getDocument().getRootElement().addElement(NODE_FILES);
        } else {
            // create filter to get only folders
            filter = CmsResourceFilter.DEFAULT_FOLDERS.addRequireVisible();
        }

        try {
            List resources = getCms().readResources(getParamCurrentFolder(), filter, false);
            Iterator i = resources.iterator();
            while (i.hasNext()) {
                CmsResource res = (CmsResource)i.next();
                if (res.isFolder()) {
                    // resource is a folder, create folder node
                    Element folder = folders.addElement(NODE_FOLDER);
                    String folderName = CmsResource.getName(res.getRootPath());
                    folderName = CmsStringUtil.substitute(folderName, "/", "");
                    folder.addAttribute(ATTR_NAME, folderName);
                } else {
                    // resource is a file
                    boolean showFile = true;
                    // check if required file type is an image and filter found resources if set
                    if (TYPE_IMAGE.equals(getParamType())) {
                        showFile = (res.getTypeId() == CmsResourceTypeImage.getStaticTypeId());
                    }
                    if (showFile) {
                        // create file node
                        Element file = files.addElement(NODE_FILE);
                        file.addAttribute(ATTR_NAME, CmsResource.getName(res.getRootPath()));
                        file.addAttribute(ATTR_SIZE, "" + (res.getLength() / 1024));
                    }
                }

            }
            return CmsXmlUtils.marshal(getDocument(), CmsEncoder.ENCODING_UTF_8);
        } catch (CmsException e) {
            // error getting resource list, return empty String
            return "";
        }
    }

    /**
     * @see org.opencms.workplace.CmsWorkplace#initWorkplaceRequestValues(org.opencms.workplace.CmsWorkplaceSettings, javax.servlet.http.HttpServletRequest)
     */
    protected void initWorkplaceRequestValues(CmsWorkplaceSettings settings, HttpServletRequest request) {

        // fill the parameter values in the get/set methods and check for multipart file items
        fillParamValues(request);

        // set the dialog type
        setParamDialogtype(DIALOG_TYPE);
        // set the action for the JSP switch 
        if (COMMAND_FILEUPLOAD.equals(getParamCommand())) {
            // upload file
            setAction(ACTION_FILEUPLOAD);
        } else if (COMMAND_CREATEFOLDER.equals(getParamCommand())) {
            // create folder
            setAction(ACTION_CREATEFOLDER);
        } else if (COMMAND_GETFOLDERS.equals(getParamCommand())) {
            // get folders
            setAction(ACTION_GETFOLDERS);
        } else {
            // default: get files and folders
            setAction(ACTION_GETFOLDERS_FILES);
        }

        // get the top response
        CmsFlexController controller = CmsFlexController.getController(getJsp().getRequest());
        HttpServletResponse res = controller.getTopResponse();
        // set the response headers depending on the command to execute
        CmsRequestUtil.setNoCacheHeaders(res);
        String contentType = CONTENTTYPE_XML;
        if (getAction() == ACTION_FILEUPLOAD) {
            contentType = CONTENTTYPE_HTML;
        }
        res.setContentType(contentType);
    }

    /**
     * Uploads a file to the OpenCms VFS and returns the necessary JavaScript for the file browser.<p>
     * 
     * @return the necessary JavaScript for the file browser
     */
    protected String uploadFile() {

        String errorCode = ERROR_UPLOAD_OK;
        try {
            // get the file item from the multipart request
            Iterator i = m_multiPartFileItems.iterator();
            FileItem fi = null;
            while (i.hasNext()) {
                fi = (FileItem)i.next();
                if (fi.getName() != null) {
                    // found the file object, leave iteration
                    break;
                } else {
                    // this is no file object, check next item
                    continue;
                }
            }

            if (fi != null) {
                String fileName = fi.getName();
                long size = fi.getSize();
                long maxFileSizeBytes = OpenCms.getWorkplaceManager().getFileBytesMaxUploadSize(getCms());
                // check file size
                if (maxFileSizeBytes > 0 && size > maxFileSizeBytes) {
                    // file size is larger than maximum allowed file size, throw an error
                    throw new Exception();
                }
                byte[] content = fi.get();
                fi.delete();

                // single file upload
                String newResname = CmsResource.getName(fileName.replace('\\', '/'));
                // determine Title property value to set on new resource
                String title = newResname;
                if (title.lastIndexOf('.') != -1) {
                    title = title.substring(0, title.lastIndexOf('.'));
                }
                List properties = new ArrayList(1);
                CmsProperty titleProp = new CmsProperty();
                titleProp.setName(CmsPropertyDefinition.PROPERTY_TITLE);
                if (OpenCms.getWorkplaceManager().isDefaultPropertiesOnStructure()) {
                    titleProp.setStructureValue(title);
                } else {
                    titleProp.setResourceValue(title);
                }
                properties.add(titleProp);

                // determine the resource type id from the given information
                int resTypeId = OpenCms.getResourceManager().getDefaultTypeForName(newResname).getTypeId();

                // calculate absolute path of uploaded resource
                newResname = getParamCurrentFolder() + newResname;

                if (!getCms().existsResource(newResname, CmsResourceFilter.IGNORE_EXPIRATION)) {
                    try {
                        // create the resource
                        getCms().createResource(newResname, resTypeId, content, properties);
                    } catch (CmsDbSqlException sqlExc) {
                        // SQL error, probably the file is too large for the database settings, delete file
                        getCms().lockResource(newResname);
                        getCms().deleteResource(newResname, CmsResource.DELETE_PRESERVE_SIBLINGS);
                        throw sqlExc;
                    }
                } else {
                    // resource exists, overwrite existing resource
                    checkLock(newResname);
                    CmsFile file = getCms().readFile(newResname, CmsResourceFilter.IGNORE_EXPIRATION);
                    byte[] contents = file.getContents();
                    try {
                        getCms().replaceResource(newResname, resTypeId, content, null);
                    } catch (CmsDbSqlException sqlExc) {
                        // SQL error, probably the file is too large for the database settings, restore content
                        file.setContents(contents);
                        getCms().writeFile(file);
                        throw sqlExc;
                    }
                }
            } else {
                // no upload file found
                throw new Exception();
            }
        } catch (Throwable e) {
            // something went wrong, change error code
            errorCode = ERROR_UPLOAD_INVALID;
        }

        // create JavaScript to return to file browser
        StringBuffer result = new StringBuffer(256);
        result.append("<html><head><script type=\"text/javascript\">\n");
        result.append("window.parent.frames[\"frmUpload\"].OnUploadCompleted(");
        result.append(errorCode);
        result.append(");\n");
        result.append("</script></head></html>");
        return result.toString();
    }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -