001
014
015 package com.liferay.portlet.blogs;
016
017 import com.liferay.portal.kernel.editor.EditorConstants;
018 import com.liferay.portal.kernel.exception.PortalException;
019 import com.liferay.portal.kernel.repository.model.FileEntry;
020 import com.liferay.portal.kernel.repository.model.Folder;
021 import com.liferay.portal.kernel.util.GetterUtil;
022 import com.liferay.portal.kernel.util.StringBundler;
023 import com.liferay.portal.kernel.util.StringPool;
024 import com.liferay.portal.portletfilerepository.PortletFileRepositoryUtil;
025 import com.liferay.portal.util.PortletKeys;
026 import com.liferay.portlet.blogs.model.BlogsEntry;
027 import com.liferay.portlet.blogs.service.BlogsEntryLocalServiceUtil;
028
029 import java.io.InputStream;
030
031 import java.util.ArrayList;
032 import java.util.List;
033 import java.util.regex.Matcher;
034 import java.util.regex.Pattern;
035
036
040 public class BlogsEntryAttachmentFileEntryHelper {
041
042 public List<BlogsEntryAttachmentFileEntryReference>
043 addBlogsEntryAttachmentFileEntries(
044 long groupId, long userId, long blogsEntryId,
045 List<FileEntry> tempFileEntries)
046 throws PortalException {
047
048 List<BlogsEntryAttachmentFileEntryReference>
049 blogsEntryAttachmentFileEntryReferences = new ArrayList<>();
050
051 for (FileEntry tempFileEntry : tempFileEntries) {
052 FileEntry blogsAttachmentEntryFileEntry =
053 addBlogsEntryAttachmentFileEntry(
054 groupId, userId, blogsEntryId, tempFileEntry.getTitle(),
055 tempFileEntry.getMimeType(),
056 tempFileEntry.getContentStream());
057
058 blogsEntryAttachmentFileEntryReferences.add(
059 new BlogsEntryAttachmentFileEntryReference(
060 tempFileEntry.getFileEntryId(),
061 blogsAttachmentEntryFileEntry));
062 }
063
064 return blogsEntryAttachmentFileEntryReferences;
065 }
066
067 public List<FileEntry> getTempBlogsEntryAttachmentFileEntries(
068 String content)
069 throws PortalException {
070
071 List<FileEntry> tempBlogsEntryAttachmentFileEntries = new ArrayList<>();
072
073 Pattern pattern = Pattern.compile(
074 EditorConstants.ATTRIBUTE_DATA_IMAGE_ID + "=.(\\d+)");
075
076 Matcher matcher = pattern.matcher(content);
077
078 while (matcher.find()) {
079 long fileEntryId = GetterUtil.getLong(matcher.group(1));
080
081 FileEntry tempFileEntry =
082 PortletFileRepositoryUtil.getPortletFileEntry(fileEntryId);
083
084 tempBlogsEntryAttachmentFileEntries.add(tempFileEntry);
085 }
086
087 return tempBlogsEntryAttachmentFileEntries;
088 }
089
090 public String updateContent(
091 String content, List<BlogsEntryAttachmentFileEntryReference>
092 blogsEntryAttachmentFileEntryReferences) {
093
094 for (BlogsEntryAttachmentFileEntryReference
095 blogsEntryAttachmentFileEntryReference :
096 blogsEntryAttachmentFileEntryReferences) {
097
098 StringBundler sb = new StringBundler(5);
099
100 sb.append("<img.*");
101 sb.append(EditorConstants.ATTRIBUTE_DATA_IMAGE_ID);
102 sb.append("=\\s?\"");
103 sb.append(
104 blogsEntryAttachmentFileEntryReference.
105 getTempBlogsEntryAttachmentFileEntryId());
106 sb.append("\".*src=\\s?\"(.*)\".*/>");
107
108 content = content.replaceAll(
109 sb.toString(),
110 getBlogsEntryAttachmentFileEntryLink(
111 blogsEntryAttachmentFileEntryReference.
112 getBlogsEntryAttachmentFileEntry()));
113 }
114
115 return content;
116 }
117
118 protected FileEntry addBlogsEntryAttachmentFileEntry(
119 long groupId, long userId, long blogsEntryId, String fileName,
120 String mimeType, InputStream is)
121 throws PortalException {
122
123 Folder folder = BlogsEntryLocalServiceUtil.addAttachmentsFolder(
124 userId, groupId);
125
126 return PortletFileRepositoryUtil.addPortletFileEntry(
127 groupId, userId, BlogsEntry.class.getName(), blogsEntryId,
128 PortletKeys.BLOGS, folder.getFolderId(), is, fileName, mimeType,
129 true);
130 }
131
132 protected String getBlogsEntryAttachmentFileEntryLink(
133 FileEntry blogsEntryAttachmentEntryFileEntry) {
134
135 String fileEntryURL = PortletFileRepositoryUtil.getPortletFileEntryURL(
136 null, blogsEntryAttachmentEntryFileEntry, StringPool.BLANK);
137
138 return "<img src=\"" + fileEntryURL + "\" />";
139 }
140
141 }