001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
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    /**
039     * @author Sergio Gonz??lez
040     * @author Roberto D??az
041     */
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, byte[] bytes)
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, bytes, uniqueFileName,
079                            mimeType, true);
080            }
081    
082            public FileEntry addBlogsEntryAttachmentFileEntry(
083                            long groupId, long userId, long blogsEntryId, long folderId,
084                            String fileName, String mimeType, File file)
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, file, uniqueFileName,
092                            mimeType, true);
093            }
094    
095            public FileEntry addBlogsEntryAttachmentFileEntry(
096                            long groupId, long userId, long blogsEntryId, long folderId,
097                            String fileName, String mimeType, InputStream is)
098                    throws PortalException {
099    
100                    String uniqueFileName = getUniqueFileName(groupId, fileName, folderId);
101    
102                    return PortletFileRepositoryUtil.addPortletFileEntry(
103                            groupId, userId, BlogsEntry.class.getName(), blogsEntryId,
104                            BlogsConstants.SERVICE_NAME, folderId, is, uniqueFileName, mimeType,
105                            true);
106            }
107    
108            public List<FileEntry> getTempBlogsEntryAttachmentFileEntries(
109                            String content)
110                    throws PortalException {
111    
112                    List<FileEntry> tempBlogsEntryAttachmentFileEntries = new ArrayList<>();
113    
114                    Pattern pattern = Pattern.compile(
115                            EditorConstants.ATTRIBUTE_DATA_IMAGE_ID + "=.(\\d+)");
116    
117                    Matcher matcher = pattern.matcher(content);
118    
119                    while (matcher.find()) {
120                            long fileEntryId = GetterUtil.getLong(matcher.group(1));
121    
122                            FileEntry tempFileEntry =
123                                    PortletFileRepositoryUtil.getPortletFileEntry(fileEntryId);
124    
125                            tempBlogsEntryAttachmentFileEntries.add(tempFileEntry);
126                    }
127    
128                    return tempBlogsEntryAttachmentFileEntries;
129            }
130    
131            public String updateContent(
132                    String content,
133                    List<BlogsEntryAttachmentFileEntryReference>
134                            blogsEntryAttachmentFileEntryReferences) {
135    
136                    for (BlogsEntryAttachmentFileEntryReference
137                                    blogsEntryAttachmentFileEntryReference :
138                                            blogsEntryAttachmentFileEntryReferences) {
139    
140                            StringBundler sb = new StringBundler(8);
141    
142                            sb.append("<\\s*?img");
143                            sb.append(_ATTRIBUTE_LIST_REGEXP);
144                            sb.append(EditorConstants.ATTRIBUTE_DATA_IMAGE_ID);
145                            sb.append("\\s*?=\\s*?\"");
146                            sb.append(
147                                    blogsEntryAttachmentFileEntryReference.
148                                            getTempBlogsEntryAttachmentFileEntryId());
149                            sb.append("\"");
150                            sb.append(_ATTRIBUTE_LIST_REGEXP);
151                            sb.append("/>");
152    
153                            content = content.replaceAll(
154                                    sb.toString(),
155                                    getBlogsEntryAttachmentFileEntryImgTag(
156                                            blogsEntryAttachmentFileEntryReference.
157                                                    getBlogsEntryAttachmentFileEntry()));
158                    }
159    
160                    return content;
161            }
162    
163            protected String getBlogsEntryAttachmentFileEntryImgTag(
164                    FileEntry blogsEntryAttachmentFileEntry) {
165    
166                    String fileEntryURL = PortletFileRepositoryUtil.getPortletFileEntryURL(
167                            null, blogsEntryAttachmentFileEntry, StringPool.BLANK);
168    
169                    return "<img src=\"" + fileEntryURL + "\" />";
170            }
171    
172            protected String getUniqueFileName(
173                            long groupId, String fileName, long folderId)
174                    throws PortalException {
175    
176                    fileName = FileUtil.stripParentheticalSuffix(fileName);
177    
178                    FileEntry fileEntry = _fetchPortletFileEntry(
179                            groupId, fileName, folderId);
180    
181                    if (fileEntry == null) {
182                            return fileName;
183                    }
184    
185                    int suffix = 1;
186    
187                    for (int i = 0; i < _UNIQUE_FILE_NAME_TRIES; i++) {
188                            String curFileName = FileUtil.appendParentheticalSuffix(
189                                    fileName, String.valueOf(suffix));
190    
191                            fileEntry = _fetchPortletFileEntry(groupId, curFileName, folderId);
192    
193                            if (fileEntry == null) {
194                                    return curFileName;
195                            }
196    
197                            suffix++;
198                    }
199    
200                    throw new PortalException(
201                            "Unable to get a unique file name for " + fileName + " in folder " +
202                                    folderId);
203            }
204    
205            private FileEntry _fetchPortletFileEntry(
206                    long groupId, String fileName, long folderId) {
207    
208                    try {
209                            return PortletFileRepositoryUtil.getPortletFileEntry(
210                                    groupId, folderId, fileName);
211                    }
212                    catch (PortalException pe) {
213                            if (_log.isDebugEnabled()) {
214                                    _log.debug(pe, pe);
215                            }
216    
217                            return null;
218                    }
219            }
220    
221            private static final String _ATTRIBUTE_LIST_REGEXP =
222                    "(\\s*?\\w+\\s*?=\\s*?\"[^\"]*\")*?\\s*?";
223    
224            private static final int _UNIQUE_FILE_NAME_TRIES = 50;
225    
226            private static final Log _log = LogFactoryUtil.getLog(
227                    BlogsEntryAttachmentFileEntryHelper.class);
228    
229    }