1 /*******************************************************************************
2 * Copyright � Igor Barma, Alexandre Desoubeaux, Christian Martel, Eric Brun, Mathieu Amblard, Gwenael Gevet, Pierre Guillot, 2012
3 * Copyright Alexandre Desoubeaux, Christian Martel, Cedric Lecarpentier, Alexandre Lefevre, Marc Salvat 2014-2016
4 * Copyright Alexandre Desoubeaux, Christian Martel, Cedric Lecarpentier, Marc Salvat, Marc Suarez, Harifetra Ramamonjy 2017
6 * This file is part of the work and learning management system Pentila Nero.
8 * Pentila Nero is free software. You can redistribute it and/or modify since
9 * you respect the terms of either (at least one of the both license) :
10 * - under the terms of the GNU Affero General Public License as
11 * published by the Free Software Foundation, either version 3 of the
12 * License, or (at your option) any later version.
13 * - the CeCILL-C as published by CeCILL-C; either version 1 of the
14 * License, or any later version
15 * - the GNU Lesser General Public License as published by the
16 * Free Software Foundation, either version 3 of the license,
17 * or (at your option) any later version.
19 * There are special exceptions to the terms and conditions of the
20 * licenses as they are applied to this software. View the full text of
21 * the exception in file LICENSE-PROJECT.txt in the directory of this software
24 * Pentila Nero is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * Licenses for more details.
29 * You should have received a copy of the GNU Affero General Public License
30 * and the CeCILL-C and the GNU Lesser General Public License along with
31 * Pentila Nero. If not, see :
32 * <http://www.gnu.org/licenses/> and
33 * <http://www.cecill.info/licences.fr.html>.
34 ******************************************************************************/
35 package com.pentila.entSavoie.blog.action;
37 import java.awt.image.RenderedImage;
38 import java.io.ByteArrayOutputStream;
40 import java.io.InputStream;
42 import javax.imageio.ImageIO;
43 import javax.portlet.ActionRequest;
44 import javax.portlet.ActionResponse;
45 import javax.portlet.PortletConfig;
46 import javax.servlet.http.HttpServletRequest;
47 import javax.servlet.http.HttpServletResponse;
49 import org.apache.struts.action.ActionForm;
50 import org.apache.struts.action.ActionForward;
51 import org.apache.struts.action.ActionMapping;
53 import com.liferay.portal.kernel.image.ImageToolUtil;
54 import com.liferay.portal.kernel.util.FileUtil;
55 import com.liferay.portal.kernel.util.MimeTypesOverrideUtil;
56 import com.liferay.portal.kernel.util.ParamUtil;
57 import com.liferay.portal.model.CompanyConstants;
58 import com.liferay.portal.service.UserLocalServiceUtil;
59 import com.liferay.portal.struts.ActionConstants;
60 import com.liferay.portal.struts.PortletAction;
61 import com.liferay.portal.util.PortalUtil;
62 import com.liferay.portlet.documentlibrary.store.DLStoreUtil;
63 import com.pentila.entSavoie.blog.model.BlogChannel;
64 import com.pentila.entSavoie.blog.service.BlogChannelLocalServiceUtil;
65 import com.pentila.entSavoie.servlet.ServletResponseServiceUtil;
66 import com.pentila.entSavoie.utils.URIFormat;
68 public class GetBlogChannelFileAction extends PortletAction {
70 public ActionForward strutsExecute(
71 ActionMapping mapping, ActionForm form, HttpServletRequest request,
72 HttpServletResponse response)
76 long channelId = ParamUtil.getLong(request, "channelId");
77 String fileName = ParamUtil.getString(request, "fileName");
78 String fileType = ParamUtil.getString(request, "fileType"); // image, imageInternet
80 fileName = URIFormat.decodeURIComponent(fileName);
82 BlogChannel channel = BlogChannelLocalServiceUtil.getBlogChannel(channelId);
83 long companyId = UserLocalServiceUtil.getUser(channel.getUserId()).getCompanyId();
86 if(fileType.equals("image")){
87 path = channel.getImageFileDir() + "/" + fileName;
88 imageThumbnailFile(companyId, path, fileName, request, response);
89 } else if(fileType.equals("imageInternet")){
90 path = channel.getImageInternetFileDir() + "/" + fileName;
91 getFile(companyId, path, fileName, response, request);
98 PortalUtil.sendError(e, request, response);
104 public void processAction(
105 ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
106 ActionRequest actionRequest, ActionResponse actionResponse)
110 long channelId = ParamUtil.getLong(actionRequest, "channelId");
111 String fileName = ParamUtil.getString(actionRequest, "fileName");
112 String fileType = ParamUtil.getString(actionRequest, "fileType"); // image, imageInternet
114 HttpServletResponse response = PortalUtil.getHttpServletResponse(
116 HttpServletRequest request = PortalUtil.getHttpServletRequest(actionRequest);
118 fileName = URIFormat.decodeURIComponent(fileName);
120 BlogChannel channel = BlogChannelLocalServiceUtil.getBlogChannel(channelId);
121 long companyId = UserLocalServiceUtil.getUser(channel.getUserId()).getCompanyId();
124 if(fileType.equals("image")){
125 path = channel.getImageFileDir() + "/" + fileName;
126 imageThumbnailFile(companyId, path, fileName, request, response);
127 } else if(fileType.equals("imageInternet")){
128 path = channel.getImageInternetFileDir() + "/" + fileName;
129 getFile(companyId, path, fileName, response, request);
132 setForward(actionRequest, ActionConstants.COMMON_NULL);
134 catch (Exception e) {
136 PortalUtil.sendError(e, actionRequest, actionResponse);
140 protected void imageThumbnailFile(
141 long companyId, String path, String fileName, HttpServletRequest request, HttpServletResponse response)
144 InputStream is = null;
149 is = DLStoreUtil.getFileAsStream(
150 companyId, CompanyConstants.SYSTEM, path);
152 tmpFile = FileUtil.createTempFile();
153 FileUtil.write(tmpFile, is);
155 RenderedImage renderedImage = ImageToolUtil.read(tmpFile).getRenderedImage();
156 RenderedImage thumbnail = ImageToolUtil.scale(renderedImage, 64, 64);
158 ByteArrayOutputStream bam = new ByteArrayOutputStream();
160 ImageIO.write(thumbnail, "png", bam);
162 String contentType = "image/png";
163 fileName = convertNonAscii(fileName);
165 String forceContentDisposition = ParamUtil.getString(request, "contentDispo");
166 String fcd = forceContentDisposition == null ? "" : forceContentDisposition;
168 ServletResponseServiceUtil.sendFile(
169 response, fileName, bam.toByteArray(), contentType, fcd);
173 ServletResponseServiceUtil.cleanUp(is);
174 if (tmpFile != null) {
181 protected void getFile(
182 long companyId, String path, String fileName, HttpServletResponse response, HttpServletRequest request)
185 InputStream is = null;
189 String forceContentDisposition = ParamUtil.getString(request, "contentDispo");
191 is = DLStoreUtil.getFileAsStream(
192 companyId, CompanyConstants.SYSTEM, path);
194 int contentLength = (int) DLStoreUtil.getFileSize(companyId, CompanyConstants.SYSTEM, path);
195 String contentType = MimeTypesOverrideUtil.getContentType(fileName);
196 String fcd = forceContentDisposition == null ? "" : forceContentDisposition;
198 ServletResponseServiceUtil.sendFile(
199 response, fileName, is, contentLength, contentType, fcd);
209 private static final String PLAIN_ASCII =
210 "AaEeIiOoUu" // grave
211 + "AaEeIiOoUuYy" // acute
212 + "AaEeIiOoUuYy" // circumflex
214 + "AaEeIiOoUuYy" // umlaut
217 + "OoUu" // double acute
220 private static final String UNICODE =
221 "\u00C0\u00E0\u00C8\u00E8\u00CC\u00EC\u00D2\u00F2\u00D9\u00F9"
222 + "\u00C1\u00E1\u00C9\u00E9\u00CD\u00ED\u00D3\u00F3\u00DA\u00FA\u00DD\u00FD"
223 + "\u00C2\u00E2\u00CA\u00EA\u00CE\u00EE\u00D4\u00F4\u00DB\u00FB\u0176\u0177"
224 + "\u00C3\u00E3\u00D5\u00F5\u00D1\u00F1"
225 + "\u00C4\u00E4\u00CB\u00EB\u00CF\u00EF\u00D6\u00F6\u00DC\u00FC\u0178\u00FF"
228 + "\u0150\u0151\u0170\u0171"
231 // remove accentued from a string and replace with ascii equivalent
232 public static String convertNonAscii(String s) {
233 if (s == null) return null;
234 StringBuilder sb = new StringBuilder();
236 for (int i = 0; i < n; i++) {
237 char c = s.charAt(i);
238 int pos = UNICODE.indexOf(c);
240 sb.append(PLAIN_ASCII.charAt(pos));
246 return sb.toString();
249 protected boolean isCheckMethodOnProcessAction() {
250 return _CHECK_METHOD_ON_PROCESS_ACTION;
253 private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;