Search in sources :

Example 1 with FileEntry

use of com.liferay.portal.kernel.repository.model.FileEntry in project liferay-ide by liferay.

the class KBArticleImporter method addKBArticleMarkdown.

protected KBArticle addKBArticleMarkdown(long userId, long groupId, long parentKBFolderId, long parentResourceClassNameId, long parentResourcePrimaryKey, String markdown, String fileEntryName, ZipReader zipReader, Map<String, String> metadata, PrioritizationStrategy prioritizationStrategy, ServiceContext serviceContext) throws KBArticleImportException, SystemException {
    if (Validator.isNull(markdown)) {
        throw new KBArticleImportException("Markdown is null for file entry " + fileEntryName);
    }
    KBArticleMarkdownConverter kbArticleMarkdownConverter = new KBArticleMarkdownConverter(markdown, fileEntryName, metadata);
    String urlTitle = kbArticleMarkdownConverter.getUrlTitle();
    KBArticle kbArticle = KBArticleLocalServiceUtil.fetchKBArticleByUrlTitle(groupId, parentKBFolderId, urlTitle);
    boolean newKBArticle = false;
    if (kbArticle == null) {
        newKBArticle = true;
    }
    try {
        if (kbArticle == null) {
            int workflowAction = serviceContext.getWorkflowAction();
            serviceContext.setWorkflowAction(WorkflowConstants.ACTION_SAVE_DRAFT);
            kbArticle = KBArticleLocalServiceUtil.addKBArticle(userId, parentResourceClassNameId, parentResourcePrimaryKey, kbArticleMarkdownConverter.getTitle(), urlTitle, markdown, null, kbArticleMarkdownConverter.getSourceURL(), null, null, serviceContext);
            serviceContext.setWorkflowAction(workflowAction);
        }
    } catch (Exception e) {
        StringBundler sb = new StringBundler(4);
        sb.append("Unable to add basic KB article for file entry ");
        sb.append(fileEntryName);
        sb.append(": ");
        sb.append(e.getLocalizedMessage());
        throw new KBArticleImportException(sb.toString(), e);
    }
    try {
        String html = kbArticleMarkdownConverter.processAttachmentsReferences(userId, kbArticle, zipReader, new HashMap<String, FileEntry>());
        kbArticle = KBArticleLocalServiceUtil.updateKBArticle(userId, kbArticle.getResourcePrimKey(), kbArticleMarkdownConverter.getTitle(), html, kbArticle.getDescription(), kbArticleMarkdownConverter.getSourceURL(), null, null, null, serviceContext);
        if (newKBArticle) {
            prioritizationStrategy.addKBArticle(kbArticle, fileEntryName);
        } else {
            prioritizationStrategy.updateKBArticle(kbArticle, fileEntryName);
        }
        return kbArticle;
    } catch (Exception e) {
        StringBundler sb = new StringBundler(4);
        sb.append("Unable to update KB article for file entry ");
        sb.append(fileEntryName);
        sb.append(": ");
        sb.append(e.getLocalizedMessage());
        throw new KBArticleImportException(sb.toString(), e);
    }
}
Also used : KBArticle(com.liferay.knowledgebase.model.KBArticle) KBArticleMarkdownConverter(com.liferay.knowledgebase.admin.importer.util.KBArticleMarkdownConverter) KBArticleImportException(com.liferay.knowledgebase.KBArticleImportException) FileEntry(com.liferay.portal.kernel.repository.model.FileEntry) PortalException(com.liferay.portal.kernel.exception.PortalException) SystemException(com.liferay.portal.kernel.exception.SystemException) IOException(java.io.IOException) KBArticleImportException(com.liferay.knowledgebase.KBArticleImportException) StringBundler(com.liferay.portal.kernel.util.StringBundler)

Example 2 with FileEntry

use of com.liferay.portal.kernel.repository.model.FileEntry in project liferay-ide by liferay.

the class KBArticleImporterUtil method addImageFileEntry.

protected static FileEntry addImageFileEntry(long userId, KBArticle kbArticle, String imageFileName, InputStream inputStream, Map<String, FileEntry> fileEntriesMap) throws PortalException, SystemException {
    FileEntry fileEntry = fileEntriesMap.get(imageFileName);
    if (fileEntry != null) {
        return fileEntry;
    }
    String mimeType = MimeTypesUtil.getContentType(imageFileName);
    try {
        PortletFileRepositoryUtil.getPortletFileEntry(kbArticle.getGroupId(), kbArticle.getAttachmentsFolderId(), imageFileName);
        PortletFileRepositoryUtil.deletePortletFileEntry(kbArticle.getGroupId(), kbArticle.getAttachmentsFolderId(), imageFileName);
    } catch (NoSuchFileEntryException nsfee) {
    }
    fileEntry = PortletFileRepositoryUtil.addPortletFileEntry(kbArticle.getGroupId(), userId, KBArticle.class.getName(), kbArticle.getClassPK(), PortletKeys.KNOWLEDGE_BASE_ARTICLE, kbArticle.getAttachmentsFolderId(), inputStream, imageFileName, mimeType, false);
    fileEntriesMap.put(imageFileName, fileEntry);
    return fileEntry;
}
Also used : NoSuchFileEntryException(com.liferay.portlet.documentlibrary.NoSuchFileEntryException) FileEntry(com.liferay.portal.kernel.repository.model.FileEntry)

Example 3 with FileEntry

use of com.liferay.portal.kernel.repository.model.FileEntry in project liferay-ide by liferay.

the class KBArticleMarkdownConverter method processAttachmentsReferences.

public String processAttachmentsReferences(long userId, KBArticle kbArticle, ZipReader zipReader, Map<String, FileEntry> fileEntriesMap) throws PortalException, SystemException {
    Set<Integer> indexes = new TreeSet<Integer>();
    int index = 0;
    while ((index = _html.indexOf("<img", index)) > -1) {
        indexes.add(index);
        index += 4;
    }
    if (indexes.isEmpty()) {
        return _html;
    }
    StringBundler sb = new StringBundler();
    int previousIndex = 0;
    for (int curIndex : indexes) {
        if (curIndex < 0) {
            break;
        }
        if (curIndex > previousIndex) {
            // Append text from previous position up to image tag
            String text = _html.substring(previousIndex, curIndex);
            sb.append(text);
        }
        int pos = _html.indexOf("/>", curIndex);
        if (pos < 0) {
            if (_log.isDebugEnabled()) {
                _log.debug("Expected close tag for image " + _html.substring(curIndex));
            }
            sb.append(_html.substring(curIndex));
            previousIndex = curIndex;
            break;
        }
        String text = _html.substring(curIndex, pos);
        String imageFileName = KBArticleImporterUtil.extractImageFileName(text);
        FileEntry imageFileEntry = KBArticleImporterUtil.addImageFileEntry(imageFileName, userId, kbArticle, zipReader, fileEntriesMap);
        if (imageFileEntry == null) {
            if (_log.isWarnEnabled()) {
                _log.warn("Unable to find image source " + text);
            }
            sb.append("<img alt=\"missing image\" src=\"\" ");
        } else {
            String imageSrc = StringPool.BLANK;
            try {
                imageSrc = DLUtil.getPreviewURL(imageFileEntry, imageFileEntry.getFileVersion(), null, StringPool.BLANK);
            } catch (PortalException pe) {
                if (_log.isWarnEnabled()) {
                    _log.warn("Unable to obtain image URL from file entry " + imageFileEntry.getFileEntryId());
                }
            }
            sb.append("<img alt=\"");
            sb.append(HtmlUtil.escapeAttribute(imageFileEntry.getTitle()));
            sb.append("\" src=\"");
            sb.append(imageSrc);
            sb.append("\" ");
        }
        previousIndex = pos;
    }
    if (previousIndex < _html.length()) {
        sb.append(_html.substring(previousIndex));
    }
    return sb.toString();
}
Also used : TreeSet(java.util.TreeSet) FileEntry(com.liferay.portal.kernel.repository.model.FileEntry) PortalException(com.liferay.portal.kernel.exception.PortalException) StringBundler(com.liferay.portal.kernel.util.StringBundler)

Example 4 with FileEntry

use of com.liferay.portal.kernel.repository.model.FileEntry in project liferay-ide by liferay.

the class WebServerServlet method getLastModified.

@Override
protected long getLastModified(HttpServletRequest request) {
    try {
        Date modifiedDate = null;
        Image image = getImage(request, true);
        if (image != null) {
            modifiedDate = image.getModifiedDate();
        } else {
            String path = HttpUtil.fixPath(request.getPathInfo());
            String[] pathArray = StringUtil.split(path, CharPool.SLASH);
            if (pathArray.length == 0) {
                return -1;
            }
            if (pathArray[0].equals("language")) {
                return -1;
            }
            FileEntry fileEntry = null;
            try {
                fileEntry = getFileEntry(pathArray);
            } catch (Exception e) {
            }
            if (fileEntry == null) {
                return -1;
            } else {
                String version = ParamUtil.getString(request, "version");
                if (Validator.isNotNull(version)) {
                    FileVersion fileVersion = fileEntry.getFileVersion(version);
                    modifiedDate = fileVersion.getModifiedDate();
                } else {
                    modifiedDate = fileEntry.getModifiedDate();
                }
            }
        }
        if (modifiedDate == null) {
            modifiedDate = PortalUtil.getUptime();
        }
        return (modifiedDate.getTime() / 1000) * 1000;
    } catch (PrincipalException pe) {
        if (_log.isWarnEnabled()) {
            _log.warn(pe, pe);
        }
    } catch (Exception e) {
        _log.error(e, e);
    }
    return -1;
}
Also used : PrincipalException(com.liferay.portal.security.auth.PrincipalException) FileVersion(com.liferay.portal.kernel.repository.model.FileVersion) FileEntry(com.liferay.portal.kernel.repository.model.FileEntry) DLFileEntry(com.liferay.portlet.documentlibrary.model.DLFileEntry) Image(com.liferay.portal.model.Image) RenderedImage(java.awt.image.RenderedImage) Date(java.util.Date) ServletException(javax.servlet.ServletException) RepositoryException(com.liferay.portal.kernel.repository.RepositoryException) NoSuchFileEntryException(com.liferay.portlet.documentlibrary.NoSuchFileEntryException) NoSuchFolderException(com.liferay.portlet.documentlibrary.NoSuchFolderException) PrincipalException(com.liferay.portal.security.auth.PrincipalException) PortalException(com.liferay.portal.kernel.exception.PortalException) SystemException(com.liferay.portal.kernel.exception.SystemException) IOException(java.io.IOException) NoSuchGroupException(com.liferay.portal.NoSuchGroupException)

Example 5 with FileEntry

use of com.liferay.portal.kernel.repository.model.FileEntry in project liferay-ide by liferay.

the class WebServerServlet method sendDocumentLibrary.

protected void sendDocumentLibrary(HttpServletRequest request, HttpServletResponse response, User user, String path, String[] pathArray) throws Exception {
    if (!PropsValues.WEB_SERVER_SERVLET_DIRECTORY_INDEXING_ENABLED) {
        response.setStatus(HttpServletResponse.SC_FORBIDDEN);
        return;
    }
    long groupId = _getGroupId(user.getCompanyId(), pathArray[0]);
    long folderId = DLFolderConstants.DEFAULT_PARENT_FOLDER_ID;
    for (int i = 1; i < pathArray.length; i++) {
        String name = pathArray[i];
        try {
            Folder folder = DLAppServiceUtil.getFolder(groupId, folderId, name);
            folderId = folder.getFolderId();
        } catch (NoSuchFolderException nsfe) {
            if (i != pathArray.length - 1) {
                throw nsfe;
            }
            String title = name;
            sendFile(response, user, groupId, folderId, title);
            return;
        }
    }
    try {
        sendFile(response, user, groupId, folderId, "index.html");
        return;
    } catch (Exception e) {
        if ((e instanceof NoSuchFileEntryException) || (e instanceof PrincipalException)) {
            try {
                sendFile(response, user, groupId, folderId, "index.htm");
                return;
            } catch (NoSuchFileEntryException nsfee) {
            } catch (PrincipalException pe) {
            }
        } else {
            throw e;
        }
    }
    List<WebServerEntry> webServerEntries = new ArrayList<WebServerEntry>();
    webServerEntries.add(new WebServerEntry(path, "../"));
    List<Folder> folders = DLAppServiceUtil.getFolders(groupId, folderId);
    for (Folder folder : folders) {
        WebServerEntry webServerEntry = new WebServerEntry(path, folder.getName() + StringPool.SLASH, folder.getCreateDate(), folder.getModifiedDate(), folder.getDescription(), 0);
        webServerEntries.add(webServerEntry);
    }
    List<FileEntry> fileEntries = DLAppServiceUtil.getFileEntries(groupId, folderId);
    for (FileEntry fileEntry : fileEntries) {
        WebServerEntry webServerEntry = new WebServerEntry(path, fileEntry.getTitle(), fileEntry.getCreateDate(), fileEntry.getModifiedDate(), fileEntry.getDescription(), fileEntry.getSize());
        webServerEntries.add(webServerEntry);
    }
    sendHTML(response, path, webServerEntries);
}
Also used : ArrayList(java.util.ArrayList) Folder(com.liferay.portal.kernel.repository.model.Folder) ServletException(javax.servlet.ServletException) RepositoryException(com.liferay.portal.kernel.repository.RepositoryException) NoSuchFileEntryException(com.liferay.portlet.documentlibrary.NoSuchFileEntryException) NoSuchFolderException(com.liferay.portlet.documentlibrary.NoSuchFolderException) PrincipalException(com.liferay.portal.security.auth.PrincipalException) PortalException(com.liferay.portal.kernel.exception.PortalException) SystemException(com.liferay.portal.kernel.exception.SystemException) IOException(java.io.IOException) NoSuchGroupException(com.liferay.portal.NoSuchGroupException) NoSuchFolderException(com.liferay.portlet.documentlibrary.NoSuchFolderException) PrincipalException(com.liferay.portal.security.auth.PrincipalException) NoSuchFileEntryException(com.liferay.portlet.documentlibrary.NoSuchFileEntryException) FileEntry(com.liferay.portal.kernel.repository.model.FileEntry) DLFileEntry(com.liferay.portlet.documentlibrary.model.DLFileEntry)

Aggregations

FileEntry (com.liferay.portal.kernel.repository.model.FileEntry)34 DLFileEntry (com.liferay.portlet.documentlibrary.model.DLFileEntry)15 PortalException (com.liferay.portal.kernel.exception.PortalException)8 Folder (com.liferay.portal.kernel.repository.model.Folder)7 ServiceContext (com.liferay.portal.service.ServiceContext)6 IOException (java.io.IOException)6 SystemException (com.liferay.portal.kernel.exception.SystemException)5 JSONObject (com.liferay.portal.kernel.json.JSONObject)5 Element (com.liferay.portal.kernel.xml.Element)5 User (com.liferay.portal.model.User)5 NoSuchFileEntryException (com.liferay.portlet.documentlibrary.NoSuchFileEntryException)5 InputStream (java.io.InputStream)5 StringBundler (com.liferay.portal.kernel.util.StringBundler)4 ArrayList (java.util.ArrayList)4 NoSuchGroupException (com.liferay.portal.NoSuchGroupException)3 RepositoryException (com.liferay.portal.kernel.repository.RepositoryException)3 FileVersion (com.liferay.portal.kernel.repository.model.FileVersion)3 Group (com.liferay.portal.model.Group)3 Repository (com.liferay.portal.model.Repository)3 PrincipalException (com.liferay.portal.security.auth.PrincipalException)3