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, List<BlogsEntryAttachmentFileEntryReference>
133                            blogsEntryAttachmentFileEntryReferences) {
134    
135                    for (BlogsEntryAttachmentFileEntryReference
136                                    blogsEntryAttachmentFileEntryReference :
137                                            blogsEntryAttachmentFileEntryReferences) {
138    
139                            StringBundler sb = new StringBundler(8);
140    
141                            sb.append("<\\s*?img");
142                            sb.append(_ATTRIBUTE_LIST_REGEXP);
143                            sb.append(EditorConstants.ATTRIBUTE_DATA_IMAGE_ID);
144                            sb.append("\\s*?=\\s*?\"");
145                            sb.append(
146                                    blogsEntryAttachmentFileEntryReference.
147                                            getTempBlogsEntryAttachmentFileEntryId());
148                            sb.append("\"");
149                            sb.append(_ATTRIBUTE_LIST_REGEXP);
150                            sb.append("/>");
151    
152                            content = content.replaceAll(
153                                    sb.toString(),
154                                    getBlogsEntryAttachmentFileEntryImgTag(
155                                            blogsEntryAttachmentFileEntryReference.
156                                                    getBlogsEntryAttachmentFileEntry()));
157                    }
158    
159                    return content;
160            }
161    
162            protected String getBlogsEntryAttachmentFileEntryImgTag(
163                    FileEntry blogsEntryAttachmentFileEntry) {
164    
165                    String fileEntryURL = PortletFileRepositoryUtil.getPortletFileEntryURL(
166                            null, blogsEntryAttachmentFileEntry, StringPool.BLANK);
167    
168                    return "<img src=\"" + fileEntryURL + "\" />";
169            }
170    
171            protected String getUniqueFileName(
172                            long groupId, String fileName, long folderId)
173                    throws PortalException {
174    
175                    fileName = FileUtil.stripParentheticalSuffix(fileName);
176    
177                    FileEntry fileEntry = _fetchPortletFileEntry(
178                            groupId, fileName, folderId);
179    
180                    if (fileEntry == null) {
181                            return fileName;
182                    }
183    
184                    int suffix = 1;
185    
186                    for (int i = 0; i < _UNIQUE_FILE_NAME_TRIES; i++) {
187                            String curFileName = FileUtil.appendParentheticalSuffix(
188                                    fileName, String.valueOf(suffix));
189    
190                            fileEntry = _fetchPortletFileEntry(groupId, curFileName, folderId);
191    
192                            if (fileEntry == null) {
193                                    return curFileName;
194                            }
195    
196                            suffix++;
197                    }
198    
199                    throw new PortalException(
200                            "Unable to get a unique file name for " + fileName + " in folder " +
201                                    folderId);
202            }
203    
204            private FileEntry _fetchPortletFileEntry(
205                    long groupId, String fileName, long folderId) {
206    
207                    try {
208                            return PortletFileRepositoryUtil.getPortletFileEntry(
209                                    groupId, folderId, fileName);
210                    }
211                    catch (PortalException pe) {
212                            if (_log.isDebugEnabled()) {
213                                    _log.debug(pe, pe);
214                            }
215    
216                            return null;
217                    }
218            }
219    
220            private static final String _ATTRIBUTE_LIST_REGEXP =
221                    "(\\s*?\\w+\\s*?=\\s*?\"[^\"]*\")*?\\s*?";
222    
223            private static final int _UNIQUE_FILE_NAME_TRIES = 50;
224    
225            private static final Log _log = LogFactoryUtil.getLog(
226                    BlogsEntryAttachmentFileEntryHelper.class);
227    
228    }