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;
43 import javax.imageio.ImageIO;
44 import javax.portlet.ActionRequest;
45 import javax.portlet.ActionResponse;
46 import javax.portlet.PortletConfig;
47 import javax.servlet.http.HttpServletRequest;
48 import javax.servlet.http.HttpServletResponse;
50 import org.apache.struts.action.ActionForm;
51 import org.apache.struts.action.ActionForward;
52 import org.apache.struts.action.ActionMapping;
54 import com.liferay.portal.kernel.image.ImageToolUtil;
55 import com.liferay.portal.kernel.util.FileUtil;
56 import com.liferay.portal.kernel.util.ParamUtil;
57 import com.liferay.portal.model.CompanyConstants;
58 import com.liferay.portal.struts.ActionConstants;
59 import com.liferay.portal.struts.PortletAction;
60 import com.liferay.portal.util.PortalUtil;
61 import com.liferay.portlet.documentlibrary.conversion.util.ENTDocumentConversionServiceUtil;
62 import com.liferay.portlet.documentlibrary.store.DLStoreUtil;
63 import com.pentila.entSavoie.blog.model.BlogItem;
64 import com.pentila.entSavoie.blog.service.BlogItemLocalServiceUtil;
65 import com.pentila.entSavoie.servlet.FLVPseudoStreamer;
66 import com.pentila.entSavoie.servlet.ServletResponseServiceUtil;
67 import com.pentila.entSavoie.utils.URIFormat;
69 public class GetBlogItemFileAction extends PortletAction {
71 public ActionForward strutsExecute(
72 ActionMapping mapping, ActionForm form, HttpServletRequest request,
73 HttpServletResponse response)
77 long itemId = ParamUtil.getLong(request, "itemId");
78 String fileName = ParamUtil.getString(request, "fileName");
79 String fileType = ParamUtil.getString(request, "fileType"); // attachment, mediaOriginal, mediaConverted, image
81 fileName = URIFormat.decodeURIComponent(fileName);
83 BlogItem item = BlogItemLocalServiceUtil.getBlogItem(itemId);
84 long companyId = item.getCompanyId();
87 if(fileType.equals("attachment")){
88 path = item.getAttachmentsDir() + "/" + fileName;
89 getFile(companyId, path, fileName, response, request);
90 } else if(fileType.equals("mediaOriginal")){
91 path = item.getMediaOriginalFileDir() + "/" + fileName;
92 getFile(companyId, path, fileName, response, request);
93 } else if(fileType.equals("mediaConverted")){
94 path = item.getMediaConvertedFileDir() + "/" + fileName;
95 getFile(companyId, path, fileName, response, request);
96 } else if(fileType.equals("image")){
97 path = item.getImageFileDir() + "/" + fileName;
98 imageThumbnailFile(companyId, path, fileName, request, response);
103 catch (Exception e) {
105 PortalUtil.sendError(e, request, response);
111 public void processAction(
112 ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
113 ActionRequest actionRequest, ActionResponse actionResponse)
117 long itemId = ParamUtil.getLong(actionRequest, "itemId");
118 String fileName = ParamUtil.getString(actionRequest, "fileName");
119 String fileType = ParamUtil.getString(actionRequest, "fileType"); // attachment, mediaOriginal, mediaConverted, image
121 HttpServletResponse response = PortalUtil.getHttpServletResponse(
123 HttpServletRequest request = PortalUtil.getHttpServletRequest(actionRequest);
125 fileName = URIFormat.decodeURIComponent(fileName);
127 BlogItem item = BlogItemLocalServiceUtil.getBlogItem(itemId);
129 long companyId = item.getCompanyId();
132 if(fileType.equals("attachment")){
133 path = item.getAttachmentsDir() + "/" + fileName;
134 getFile(companyId, path, fileName, response, request);
135 } else if(fileType.equals("mediaOriginal")){
136 path = item.getMediaOriginalFileDir() + "/" + fileName;
137 getFile(companyId, path, fileName, response, request);
138 } else if(fileType.equals("mediaConverted")){
139 path = item.getMediaConvertedFileDir() + "/" + fileName;
140 getFile(companyId, path, fileName, response, request);
141 } else if(fileType.equals("image")){
142 path = item.getImageFileDir() + "/" + fileName;
143 imageThumbnailFile(companyId, path, fileName, request, response);
146 setForward(actionRequest, ActionConstants.COMMON_NULL);
148 catch (Exception e) {
150 PortalUtil.sendError(e, actionRequest, actionResponse);
154 protected void imageThumbnailFile(
155 long companyId, String path, String fileName, HttpServletRequest request, HttpServletResponse response)
158 InputStream is = null;
163 is = DLStoreUtil.getFileAsStream(
164 companyId, CompanyConstants.SYSTEM, path);
166 tmpFile = FileUtil.createTempFile();
167 FileUtil.write(tmpFile, is);
169 RenderedImage renderedImage = ImageToolUtil.read(tmpFile).getRenderedImage();
170 RenderedImage thumbnail = ImageToolUtil.scale(renderedImage, 64, 64);
172 ByteArrayOutputStream bam = new ByteArrayOutputStream();
174 ImageIO.write(thumbnail, "png", bam);
176 String contentType = "image/png";
177 fileName = convertNonAscii(fileName);
179 String forceContentDisposition = ParamUtil.getString(request, "contentDispo");
180 String fcd = forceContentDisposition == null ? "" : forceContentDisposition;
182 ServletResponseServiceUtil.sendFile(
183 response, fileName, bam.toByteArray(), contentType, fcd);
187 ServletResponseServiceUtil.cleanUp(is);
188 if (tmpFile != null) {
195 protected void getFile(
196 long companyId, String path, String fileName, HttpServletResponse response, HttpServletRequest request)
199 InputStream is = null;
203 String targetExtension = ParamUtil.getString(request, "targetExtension");
205 // réalise la conversion du fichier ou nous retourne celui-ci si pas de conversion voulu
206 Map<String, Object> fais = ENTDocumentConversionServiceUtil.getFileAsInputStream(
212 String contentType = (String)fais.get("contentType");
213 int contentLength = (Integer)fais.get("contentLength");
214 fileName = (String)fais.get("fileName");
215 is = (InputStream)fais.get("stream");
218 String ppos = request.getParameter("start");
219 long pos = ppos == null || ppos.length() == 0 ? 0 : Long.parseLong(ppos);
221 String forceContentDisposition = ParamUtil.getString(request, "contentDispo");
222 String fcd = forceContentDisposition == null ? "" : forceContentDisposition;
225 if (pos!=0 && contentType.equals("application/x-shockwave-flash") && fcd.isEmpty()) {
226 FLVPseudoStreamer flvPseudoStreamer = new FLVPseudoStreamer();
227 flvPseudoStreamer.service(request, response, fileName, is, contentLength, contentType);
230 ServletResponseServiceUtil.sendFile(
231 response, fileName, is, contentLength, contentType, fcd);
242 private static final String PLAIN_ASCII =
243 "AaEeIiOoUu" // grave
244 + "AaEeIiOoUuYy" // acute
245 + "AaEeIiOoUuYy" // circumflex
247 + "AaEeIiOoUuYy" // umlaut
250 + "OoUu" // double acute
253 private static final String UNICODE =
254 "\u00C0\u00E0\u00C8\u00E8\u00CC\u00EC\u00D2\u00F2\u00D9\u00F9"
255 + "\u00C1\u00E1\u00C9\u00E9\u00CD\u00ED\u00D3\u00F3\u00DA\u00FA\u00DD\u00FD"
256 + "\u00C2\u00E2\u00CA\u00EA\u00CE\u00EE\u00D4\u00F4\u00DB\u00FB\u0176\u0177"
257 + "\u00C3\u00E3\u00D5\u00F5\u00D1\u00F1"
258 + "\u00C4\u00E4\u00CB\u00EB\u00CF\u00EF\u00D6\u00F6\u00DC\u00FC\u0178\u00FF"
261 + "\u0150\u0151\u0170\u0171"
264 // remove accentued from a string and replace with ascii equivalent
265 public static String convertNonAscii(String s) {
266 if (s == null) return null;
267 StringBuilder sb = new StringBuilder();
269 for (int i = 0; i < n; i++) {
270 char c = s.charAt(i);
271 int pos = UNICODE.indexOf(c);
273 sb.append(PLAIN_ASCII.charAt(pos));
279 return sb.toString();
282 protected boolean isCheckMethodOnProcessAction() {
283 return _CHECK_METHOD_ON_PROCESS_ACTION;
286 private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;