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.exception.PortalException;
018    import com.liferay.portal.kernel.json.JSONFactoryUtil;
019    import com.liferay.portal.kernel.json.JSONObject;
020    import com.liferay.portal.kernel.repository.model.FileEntry;
021    import com.liferay.portal.kernel.upload.LiferayFileItemException;
022    import com.liferay.portal.kernel.upload.UploadException;
023    import com.liferay.portal.kernel.upload.UploadServletRequest;
024    import com.liferay.portal.kernel.util.MimeTypesUtil;
025    import com.liferay.portal.kernel.util.StringPool;
026    import com.liferay.portal.kernel.util.StringUtil;
027    import com.liferay.portal.kernel.util.TempFileEntryUtil;
028    import com.liferay.portal.portletfilerepository.PortletFileRepositoryUtil;
029    import com.liferay.portal.struts.JSONAction;
030    import com.liferay.portal.theme.ThemeDisplay;
031    import com.liferay.portal.util.PortalUtil;
032    import com.liferay.portal.util.WebKeys;
033    import com.liferay.portlet.documentlibrary.FileSizeException;
034    
035    import java.io.InputStream;
036    
037    import javax.servlet.http.HttpServletRequest;
038    import javax.servlet.http.HttpServletResponse;
039    
040    import org.apache.struts.action.ActionForm;
041    import org.apache.struts.action.ActionMapping;
042    
043    /**
044     * @author Sergio Gonz??lez
045     */
046    public class ImageSelectorAction extends JSONAction {
047    
048            @Override
049            public String getJSON(
050                            ActionMapping actionMapping, ActionForm actionForm,
051                            HttpServletRequest request, HttpServletResponse response)
052                    throws Exception {
053    
054                    UploadServletRequest uploadPortletRequest =
055                            PortalUtil.getUploadServletRequest(request);
056    
057                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
058                            WebKeys.THEME_DISPLAY);
059    
060                    UploadException uploadException = (UploadException)request.getAttribute(
061                            WebKeys.UPLOAD_EXCEPTION);
062    
063                    if (uploadException != null) {
064                            if (uploadException.isExceededLiferayFileItemSizeLimit()) {
065                                    throw new LiferayFileItemException();
066                            }
067                            else if (uploadException.isExceededSizeLimit()) {
068                                    throw new FileSizeException(uploadException.getCause());
069                            }
070    
071                            throw new PortalException(uploadException.getCause());
072                    }
073    
074                    JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
075    
076                    try {
077                            JSONObject imageJSONObject = JSONFactoryUtil.createJSONObject();
078    
079                            String fileName = uploadPortletRequest.getFileName(
080                                    "imageSelectorFileName");
081    
082                            Class<?> clazz = getClass();
083    
084                            String tempFolderName = clazz.getName();
085    
086                            InputStream inputStream = uploadPortletRequest.getFileAsStream(
087                                    "imageSelectorFileName");
088    
089                            FileEntry fileEntry = TempFileEntryUtil.addTempFileEntry(
090                                    themeDisplay.getScopeGroupId(), themeDisplay.getUserId(),
091                                    tempFolderName, StringUtil.randomString() + fileName,
092                                    inputStream, MimeTypesUtil.getContentType(fileName));
093    
094                            imageJSONObject.put("fileEntryId", fileEntry.getFileEntryId());
095    
096                            imageJSONObject.put(
097                                    "url",
098                                    PortletFileRepositoryUtil.getPortletFileEntryURL(
099                                            themeDisplay, fileEntry, StringPool.BLANK));
100    
101                            jsonObject.put("image", imageJSONObject);
102    
103                            jsonObject.put("success", Boolean.TRUE);
104                    }
105                    catch (Exception e) {
106                            jsonObject.put("success", Boolean.FALSE);
107                    }
108    
109                    return jsonObject.toString();
110            }
111    
112    }