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.portal.action;
016    
017    import com.liferay.portal.kernel.editor.EditorConstants;
018    import com.liferay.portal.kernel.exception.PortalException;
019    import com.liferay.portal.kernel.json.JSONFactoryUtil;
020    import com.liferay.portal.kernel.json.JSONObject;
021    import com.liferay.portal.kernel.repository.model.FileEntry;
022    import com.liferay.portal.kernel.upload.LiferayFileItemException;
023    import com.liferay.portal.kernel.upload.UploadException;
024    import com.liferay.portal.kernel.upload.UploadPortletRequest;
025    import com.liferay.portal.kernel.util.ParamUtil;
026    import com.liferay.portal.kernel.util.StreamUtil;
027    import com.liferay.portal.kernel.util.StringPool;
028    import com.liferay.portal.kernel.util.StringUtil;
029    import com.liferay.portal.kernel.util.TempFileEntryUtil;
030    import com.liferay.portal.portletfilerepository.PortletFileRepositoryUtil;
031    import com.liferay.portal.security.permission.PermissionChecker;
032    import com.liferay.portal.struts.PortletAction;
033    import com.liferay.portal.theme.ThemeDisplay;
034    import com.liferay.portal.util.PortalUtil;
035    import com.liferay.portal.util.WebKeys;
036    import com.liferay.portlet.documentlibrary.FileSizeException;
037    
038    import java.io.InputStream;
039    
040    import javax.portlet.ActionRequest;
041    import javax.portlet.ActionResponse;
042    import javax.portlet.PortletConfig;
043    
044    import org.apache.struts.action.ActionForm;
045    import org.apache.struts.action.ActionMapping;
046    
047    /**
048     * @author Sergio Gonz??lez
049     * @author Roberto D??az
050     */
051    public abstract class BaseImageSelectorAction extends PortletAction {
052    
053            @Override
054            public void processAction(
055                            ActionMapping actionMapping, ActionForm actionForm,
056                            PortletConfig portletConfig, ActionRequest actionRequest,
057                            ActionResponse actionResponse)
058                    throws Exception {
059    
060                    UploadPortletRequest uploadPortletRequest =
061                            PortalUtil.getUploadPortletRequest(actionRequest);
062    
063                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
064                            WebKeys.THEME_DISPLAY);
065    
066                    checkPermission(
067                            themeDisplay.getScopeGroupId(),
068                            themeDisplay.getPermissionChecker());
069    
070                    UploadException uploadException =
071                            (UploadException)actionRequest.getAttribute(
072                                    WebKeys.UPLOAD_EXCEPTION);
073    
074                    if (uploadException != null) {
075                            if (uploadException.isExceededLiferayFileItemSizeLimit()) {
076                                    throw new LiferayFileItemException();
077                            }
078                            else if (uploadException.isExceededSizeLimit()) {
079                                    throw new FileSizeException(uploadException.getCause());
080                            }
081    
082                            throw new PortalException(uploadException.getCause());
083                    }
084    
085                    JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
086    
087                    String randomId = ParamUtil.getString(uploadPortletRequest, "randomId");
088    
089                    JSONObject imageJSONObject = null;
090    
091                    try {
092                            imageJSONObject = getImageJSONObject(actionRequest);
093    
094                            jsonObject.put("success", Boolean.TRUE);
095                    }
096                    catch (Exception e) {
097                            handleUploadException(actionRequest, actionResponse, e, jsonObject);
098                    }
099    
100                    imageJSONObject.put("randomId", randomId);
101    
102                    jsonObject.put("image", imageJSONObject);
103    
104                    writeJSON(actionRequest, actionResponse, jsonObject);
105            }
106    
107            protected abstract void checkPermission(
108                            long groupId, PermissionChecker permissionChecker)
109                    throws PortalException;
110    
111            protected JSONObject getImageJSONObject(ActionRequest actionRequest)
112                    throws Exception {
113    
114                    UploadPortletRequest uploadPortletRequest =
115                            PortalUtil.getUploadPortletRequest(actionRequest);
116    
117                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
118                            WebKeys.THEME_DISPLAY);
119    
120                    JSONObject imageJSONObject = JSONFactoryUtil.createJSONObject();
121    
122                    InputStream inputStream = null;
123    
124                    try {
125                            imageJSONObject.put(
126                                    "attributeDataImageId",
127                                    EditorConstants.ATTRIBUTE_DATA_IMAGE_ID);
128    
129                            String fileName = uploadPortletRequest.getFileName(
130                                    "imageSelectorFileName");
131                            String contentType = uploadPortletRequest.getContentType(
132                                    "imageSelectorFileName");
133                            long size = uploadPortletRequest.getSize("imageSelectorFileName");
134    
135                            validateFile(fileName, contentType, size);
136    
137                            inputStream = uploadPortletRequest.getFileAsStream(
138                                    "imageSelectorFileName");
139    
140                            FileEntry fileEntry = TempFileEntryUtil.addTempFileEntry(
141                                    themeDisplay.getScopeGroupId(), themeDisplay.getUserId(),
142                                    _TEMP_FOLDER_NAME, StringUtil.randomString() + fileName,
143                                    inputStream, contentType);
144    
145                            imageJSONObject.put("fileEntryId", fileEntry.getFileEntryId());
146    
147                            imageJSONObject.put(
148                                    "url",
149                                    PortletFileRepositoryUtil.getPortletFileEntryURL(
150                                            themeDisplay, fileEntry, StringPool.BLANK));
151    
152                            return imageJSONObject;
153                    }
154                    finally {
155                            StreamUtil.cleanUp(inputStream);
156                    }
157            }
158    
159            protected abstract void handleUploadException(
160                            ActionRequest actionRequest, ActionResponse actionResponse,
161                            Exception e, JSONObject jsonObject)
162                    throws Exception;
163    
164            protected abstract void validateFile(
165                            String fileName, String contentType, long size)
166                    throws PortalException;
167    
168            private static final String _TEMP_FOLDER_NAME =
169                    BaseImageSelectorAction.class.getName();
170    
171    }