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.log.Log;
020 import com.liferay.portal.kernel.log.LogFactoryUtil;
021 import com.liferay.portal.kernel.repository.model.FileEntry;
022 import com.liferay.portal.kernel.util.FileUtil;
023 import com.liferay.portal.kernel.util.GetterUtil;
024 import com.liferay.portal.kernel.util.StringBundler;
025 import com.liferay.portal.kernel.util.StringPool;
026 import com.liferay.portal.portletfilerepository.PortletFileRepositoryUtil;
027 import com.liferay.portlet.blogs.constants.BlogsConstants;
028 import com.liferay.portlet.blogs.model.BlogsEntry;
029
030 import java.io.File;
031 import java.io.InputStream;
032
033 import java.util.ArrayList;
034 import java.util.List;
035 import java.util.regex.Matcher;
036 import java.util.regex.Pattern;
037
038
042 public class BlogsEntryAttachmentFileEntryHelper {
043
044 public List<BlogsEntryAttachmentFileEntryReference>
045 addBlogsEntryAttachmentFileEntries(
046 long groupId, long userId, long blogsEntryId, long folderId,
047 List<FileEntry> tempFileEntries)
048 throws PortalException {
049
050 List<BlogsEntryAttachmentFileEntryReference>
051 blogsEntryAttachmentFileEntryReferences = new ArrayList<>();
052
053 for (FileEntry tempFileEntry : tempFileEntries) {
054 FileEntry blogsEntryAttachmentFileEntry =
055 addBlogsEntryAttachmentFileEntry(
056 groupId, userId, blogsEntryId, folderId,
057 tempFileEntry.getTitle(), tempFileEntry.getMimeType(),
058 tempFileEntry.getContentStream());
059
060 blogsEntryAttachmentFileEntryReferences.add(
061 new BlogsEntryAttachmentFileEntryReference(
062 tempFileEntry.getFileEntryId(),
063 blogsEntryAttachmentFileEntry));
064 }
065
066 return blogsEntryAttachmentFileEntryReferences;
067 }
068
069 public FileEntry addBlogsEntryAttachmentFileEntry(
070 long groupId, long userId, long blogsEntryId, long folderId,
071 String fileName, String mimeType, File file)
072 throws PortalException {
073
074 String uniqueFileName = getUniqueFileName(groupId, fileName, folderId);
075
076 return PortletFileRepositoryUtil.addPortletFileEntry(
077 groupId, userId, BlogsEntry.class.getName(), blogsEntryId,
078 BlogsConstants.SERVICE_NAME, folderId, file, uniqueFileName,
079 mimeType, true);
080 }
081
082 public FileEntry addBlogsEntryAttachmentFileEntry(
083 long groupId, long userId, long blogsEntryId, long folderId,
084 String fileName, String mimeType, InputStream is)
085 throws PortalException {
086
087 String uniqueFileName = getUniqueFileName(groupId, fileName, folderId);
088
089 return PortletFileRepositoryUtil.addPortletFileEntry(
090 groupId, userId, BlogsEntry.class.getName(), blogsEntryId,
091 BlogsConstants.SERVICE_NAME, folderId, is, uniqueFileName, mimeType,
092 true);
093 }
094
095 public List<FileEntry> getTempBlogsEntryAttachmentFileEntries(
096 String content)
097 throws PortalException {
098
099 List<FileEntry> tempBlogsEntryAttachmentFileEntries = new ArrayList<>();
100
101 Pattern pattern = Pattern.compile(
102 EditorConstants.ATTRIBUTE_DATA_IMAGE_ID + "=.(\\d+)");
103
104 Matcher matcher = pattern.matcher(content);
105
106 while (matcher.find()) {
107 long fileEntryId = GetterUtil.getLong(matcher.group(1));
108
109 FileEntry tempFileEntry =
110 PortletFileRepositoryUtil.getPortletFileEntry(fileEntryId);
111
112 tempBlogsEntryAttachmentFileEntries.add(tempFileEntry);
113 }
114
115 return tempBlogsEntryAttachmentFileEntries;
116 }
117
118 public String updateContent(
119 String content, List<BlogsEntryAttachmentFileEntryReference>
120 blogsEntryAttachmentFileEntryReferences) {
121
122 for (BlogsEntryAttachmentFileEntryReference
123 blogsEntryAttachmentFileEntryReference :
124 blogsEntryAttachmentFileEntryReferences) {
125
126 StringBundler sb = new StringBundler(8);
127
128 sb.append("<\\s*?img");
129 sb.append(_ATTRIBUTE_LIST_REGEXP);
130 sb.append(EditorConstants.ATTRIBUTE_DATA_IMAGE_ID);
131 sb.append("\\s*?=\\s*?\"");
132 sb.append(
133 blogsEntryAttachmentFileEntryReference.
134 getTempBlogsEntryAttachmentFileEntryId());
135 sb.append("\"");
136 sb.append(_ATTRIBUTE_LIST_REGEXP);
137 sb.append("/>");
138
139 content = content.replaceAll(
140 sb.toString(),
141 getBlogsEntryAttachmentFileEntryImgTag(
142 blogsEntryAttachmentFileEntryReference.
143 getBlogsEntryAttachmentFileEntry()));
144 }
145
146 return content;
147 }
148
149 protected String getBlogsEntryAttachmentFileEntryImgTag(
150 FileEntry blogsEntryAttachmentFileEntry) {
151
152 String fileEntryURL = PortletFileRepositoryUtil.getPortletFileEntryURL(
153 null, blogsEntryAttachmentFileEntry, StringPool.BLANK);
154
155 return "<img src=\"" + fileEntryURL + "\" />";
156 }
157
158 protected String getUniqueFileName(
159 long groupId, String fileName, long folderId)
160 throws PortalException {
161
162 fileName = FileUtil.stripParentheticalSuffix(fileName);
163
164 FileEntry fileEntry = _fetchPortletFileEntry(
165 groupId, fileName, folderId);
166
167 if (fileEntry == null) {
168 return fileName;
169 }
170
171 int suffix = 1;
172
173 for (int i = 0; i < _UNIQUE_FILE_NAME_TRIES; i++) {
174 String curFileName = FileUtil.appendParentheticalSuffix(
175 fileName, String.valueOf(suffix));
176
177 fileEntry = _fetchPortletFileEntry(groupId, curFileName, folderId);
178
179 if (fileEntry == null) {
180 return curFileName;
181 }
182
183 suffix++;
184 }
185
186 throw new PortalException(
187 "Unable to get a unique file name for " + fileName + " in folder " +
188 folderId);
189 }
190
191 private FileEntry _fetchPortletFileEntry(
192 long groupId, String fileName, long folderId) {
193
194 try {
195 return PortletFileRepositoryUtil.getPortletFileEntry(
196 groupId, folderId, fileName);
197 }
198 catch (PortalException pe) {
199 if (_log.isDebugEnabled()) {
200 _log.debug(pe, pe);
201 }
202
203 return null;
204 }
205 }
206
207 private static final String _ATTRIBUTE_LIST_REGEXP =
208 "(\\s*?\\w+\\s*?=\\s*?\"[^\"]*\")*?\\s*?";
209
210 private static final int _UNIQUE_FILE_NAME_TRIES = 50;
211
212 private static final Log _log = LogFactoryUtil.getLog(
213 BlogsEntryAttachmentFileEntryHelper.class);
214
215 }