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);
}
}
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;
}
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();
}
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;
}
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);
}
Aggregations