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 blogsEntryAttachmentFileEntry =
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 blogsEntryAttachmentFileEntry));
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(8);
099
100 sb.append("<\\s*?img");
101 sb.append(_ATTRIBUTE_LIST_REGEXP);
102 sb.append(EditorConstants.ATTRIBUTE_DATA_IMAGE_ID);
103 sb.append("\\s*?=\\s*?\"");
104 sb.append(
105 blogsEntryAttachmentFileEntryReference.
106 getTempBlogsEntryAttachmentFileEntryId());
107 sb.append("\"");
108 sb.append(_ATTRIBUTE_LIST_REGEXP);
109 sb.append("/>");
110
111 content = content.replaceAll(
112 sb.toString(),
113 getBlogsEntryAttachmentFileEntryImgTag(
114 blogsEntryAttachmentFileEntryReference.
115 getBlogsEntryAttachmentFileEntry()));
116 }
117
118 return content;
119 }
120
121 protected FileEntry addBlogsEntryAttachmentFileEntry(
122 long groupId, long userId, long blogsEntryId, String fileName,
123 String mimeType, InputStream is)
124 throws PortalException {
125
126 Folder folder = BlogsEntryLocalServiceUtil.addAttachmentsFolder(
127 userId, groupId);
128
129 return PortletFileRepositoryUtil.addPortletFileEntry(
130 groupId, userId, BlogsEntry.class.getName(), blogsEntryId,
131 PortletKeys.BLOGS, folder.getFolderId(), is, fileName, mimeType,
132 true);
133 }
134
135 protected String getBlogsEntryAttachmentFileEntryImgTag(
136 FileEntry blogsEntryAttachmentFileEntry) {
137
138 String fileEntryURL = PortletFileRepositoryUtil.getPortletFileEntryURL(
139 null, blogsEntryAttachmentFileEntry, StringPool.BLANK);
140
141 return "<img src=\"" + fileEntryURL + "\" />";
142 }
143
144 private static final String _ATTRIBUTE_LIST_REGEXP =
145 "(\\s*?\\w+\\s*?=\\s*?\"[^\"]*\")*?\\s*?";
146
147 }