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.action;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.json.JSONFactoryUtil;
019    import com.liferay.portal.kernel.json.JSONObject;
020    import com.liferay.portal.kernel.servlet.ServletResponseConstants;
021    import com.liferay.portal.kernel.util.FileUtil;
022    import com.liferay.portal.kernel.util.PropsKeys;
023    import com.liferay.portal.kernel.util.StringPool;
024    import com.liferay.portal.kernel.util.WebKeys;
025    import com.liferay.portal.security.auth.PrincipalException;
026    import com.liferay.portal.security.permission.ActionKeys;
027    import com.liferay.portal.security.permission.PermissionChecker;
028    import com.liferay.portal.security.permission.ResourcePermissionCheckerUtil;
029    import com.liferay.portal.theme.ThemeDisplay;
030    import com.liferay.portal.util.PrefsPropsUtil;
031    import com.liferay.portlet.blogs.CoverImageNameException;
032    import com.liferay.portlet.blogs.CoverImageSizeException;
033    import com.liferay.portlet.blogs.service.permission.BlogsPermission;
034    import com.liferay.portlet.documentlibrary.FileNameException;
035    import com.liferay.portlet.documentlibrary.antivirus.AntivirusScannerException;
036    
037    import javax.portlet.ActionRequest;
038    import javax.portlet.ActionResponse;
039    
040    /**
041     * @author Sergio Gonz??lez
042     */
043    public abstract class BaseImageSelectorAction
044            extends com.liferay.portal.action.BaseImageSelectorAction {
045    
046            @Override
047            public void checkPermission(
048                            long groupId, PermissionChecker permissionChecker)
049                    throws PortalException {
050    
051                    boolean containsResourcePermission =
052                            ResourcePermissionCheckerUtil.containsResourcePermission(
053                                    permissionChecker, BlogsPermission.RESOURCE_NAME, groupId,
054                                    ActionKeys.ADD_ENTRY);
055    
056                    if (!containsResourcePermission) {
057                            throw new PrincipalException();
058                    }
059            }
060    
061            @Override
062            public void validateFile(String fileName, String contentType, long size)
063                    throws PortalException {
064    
065                    String extension = FileUtil.getExtension(fileName);
066    
067                    String[] imageExtensions = PrefsPropsUtil.getStringArray(
068                            PropsKeys.BLOGS_IMAGE_EXTENSIONS, StringPool.COMMA);
069    
070                    for (String imageExtension : imageExtensions) {
071                            if (StringPool.STAR.equals(imageExtension) ||
072                                    imageExtension.equals(StringPool.PERIOD + extension)) {
073    
074                                    return;
075                            }
076                    }
077    
078                    throw new CoverImageNameException(
079                            "Invalid cover image for file name " + fileName);
080            }
081    
082            protected abstract long getMaxFileSize();
083    
084            @Override
085            protected void handleUploadException(
086                            ActionRequest actionRequest, ActionResponse actionResponse,
087                            Exception e, JSONObject jsonObject)
088                    throws Exception {
089    
090                    jsonObject.put("success", Boolean.FALSE);
091    
092                    if (e instanceof AntivirusScannerException ||
093                            e instanceof CoverImageNameException ||
094                            e instanceof CoverImageSizeException ||
095                            e instanceof FileNameException) {
096    
097                            String errorMessage = StringPool.BLANK;
098                            int errorType = 0;
099    
100                            ThemeDisplay themeDisplay =
101                                    (ThemeDisplay)actionRequest.getAttribute(WebKeys.THEME_DISPLAY);
102    
103                            if (e instanceof AntivirusScannerException) {
104                                    errorType =
105                                            ServletResponseConstants.SC_FILE_ANTIVIRUS_EXCEPTION;
106                                    AntivirusScannerException ase = (AntivirusScannerException)e;
107    
108                                    errorMessage = themeDisplay.translate(ase.getMessageKey());
109                            }
110                            else if (e instanceof CoverImageNameException) {
111                                    errorType =
112                                            ServletResponseConstants.SC_FILE_EXTENSION_EXCEPTION;
113                            }
114                            else if (e instanceof CoverImageSizeException) {
115                                    errorType = ServletResponseConstants.SC_FILE_SIZE_EXCEPTION;
116                            }
117                            else if (e instanceof FileNameException) {
118                                    errorType = ServletResponseConstants.SC_FILE_NAME_EXCEPTION;
119                            }
120    
121                            JSONObject errorJSONObject = JSONFactoryUtil.createJSONObject();
122    
123                            errorJSONObject.put("errorType", errorType);
124                            errorJSONObject.put("message", errorMessage);
125    
126                            jsonObject.put("error", errorJSONObject);
127    
128                            writeJSON(actionRequest, actionResponse, jsonObject);
129                    }
130                    else {
131                            throw e;
132                    }
133            }
134    
135    }