--- /dev/null
+/*******************************************************************************
+ * Copyright � Igor Barma, Alexandre Desoubeaux, Christian Martel, Eric Brun, Mathieu Amblard, Gwenael Gevet, Pierre Guillot, 2012
+ * Copyright Alexandre Desoubeaux, Christian Martel, Cedric Lecarpentier, Alexandre Lefevre, Marc Salvat 2014-2016
+ * Copyright Alexandre Desoubeaux, Christian Martel, Cedric Lecarpentier, Marc Salvat, Marc Suarez, Harifetra Ramamonjy 2017
+ *
+ * This file is part of the work and learning management system Pentila Nero.
+ *
+ * Pentila Nero is free software. You can redistribute it and/or modify since
+ * you respect the terms of either (at least one of the both license) :
+ * - under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ * - the CeCILL-C as published by CeCILL-C; either version 1 of the
+ * License, or any later version
+ * - the GNU Lesser General Public License as published by the
+ * Free Software Foundation, either version 3 of the license,
+ * or (at your option) any later version.
+ *
+ * There are special exceptions to the terms and conditions of the
+ * licenses as they are applied to this software. View the full text of
+ * the exception in file LICENSE-PROJECT.txt in the directory of this software
+ * distribution.
+ *
+ * Pentila Nero is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * Licenses for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * and the CeCILL-C and the GNU Lesser General Public License along with
+ * Pentila Nero. If not, see :
+ * <http://www.gnu.org/licenses/> and
+ * <http://www.cecill.info/licences.fr.html>.
+ ******************************************************************************/
+package com.pentila.entSavoie.blog.action;
+
+import java.awt.image.RenderedImage;
+import java.io.ByteArrayOutputStream;
+import java.io.File;
+import java.io.InputStream;
+import java.util.Map;
+
+import javax.imageio.ImageIO;
+import javax.portlet.ActionRequest;
+import javax.portlet.ActionResponse;
+import javax.portlet.PortletConfig;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.apache.struts.action.ActionForm;
+import org.apache.struts.action.ActionForward;
+import org.apache.struts.action.ActionMapping;
+
+import com.liferay.portal.kernel.image.ImageToolUtil;
+import com.liferay.portal.kernel.util.FileUtil;
+import com.liferay.portal.kernel.util.ParamUtil;
+import com.liferay.portal.model.CompanyConstants;
+import com.liferay.portal.struts.ActionConstants;
+import com.liferay.portal.struts.PortletAction;
+import com.liferay.portal.util.PortalUtil;
+import com.liferay.portlet.documentlibrary.conversion.util.ENTDocumentConversionServiceUtil;
+import com.liferay.portlet.documentlibrary.store.DLStoreUtil;
+import com.pentila.entSavoie.blog.model.BlogItem;
+import com.pentila.entSavoie.blog.service.BlogItemLocalServiceUtil;
+import com.pentila.entSavoie.servlet.FLVPseudoStreamer;
+import com.pentila.entSavoie.servlet.ServletResponseServiceUtil;
+import com.pentila.entSavoie.utils.URIFormat;
+
+public class GetBlogItemFileAction extends PortletAction {
+
+ public ActionForward strutsExecute(
+ ActionMapping mapping, ActionForm form, HttpServletRequest request,
+ HttpServletResponse response)
+ throws Exception {
+
+ try {
+ long itemId = ParamUtil.getLong(request, "itemId");
+ String fileName = ParamUtil.getString(request, "fileName");
+ String fileType = ParamUtil.getString(request, "fileType"); // attachment, mediaOriginal, mediaConverted, image
+
+ fileName = URIFormat.decodeURIComponent(fileName);
+
+ BlogItem item = BlogItemLocalServiceUtil.getBlogItem(itemId);
+ long companyId = item.getCompanyId();
+ String path = "";
+
+ if(fileType.equals("attachment")){
+ path = item.getAttachmentsDir() + "/" + fileName;
+ getFile(companyId, path, fileName, response, request);
+ } else if(fileType.equals("mediaOriginal")){
+ path = item.getMediaOriginalFileDir() + "/" + fileName;
+ getFile(companyId, path, fileName, response, request);
+ } else if(fileType.equals("mediaConverted")){
+ path = item.getMediaConvertedFileDir() + "/" + fileName;
+ getFile(companyId, path, fileName, response, request);
+ } else if(fileType.equals("image")){
+ path = item.getImageFileDir() + "/" + fileName;
+ imageThumbnailFile(companyId, path, fileName, request, response);
+ }
+
+ return null;
+ }
+ catch (Exception e) {
+ e.printStackTrace();
+ PortalUtil.sendError(e, request, response);
+
+ return null;
+ }
+ }
+
+ public void processAction(
+ ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
+ ActionRequest actionRequest, ActionResponse actionResponse)
+ throws Exception {
+
+ try {
+ long itemId = ParamUtil.getLong(actionRequest, "itemId");
+ String fileName = ParamUtil.getString(actionRequest, "fileName");
+ String fileType = ParamUtil.getString(actionRequest, "fileType"); // attachment, mediaOriginal, mediaConverted, image
+
+ HttpServletResponse response = PortalUtil.getHttpServletResponse(
+ actionResponse);
+ HttpServletRequest request = PortalUtil.getHttpServletRequest(actionRequest);
+
+ fileName = URIFormat.decodeURIComponent(fileName);
+
+ BlogItem item = BlogItemLocalServiceUtil.getBlogItem(itemId);
+
+ long companyId = item.getCompanyId();
+ String path = "";
+
+ if(fileType.equals("attachment")){
+ path = item.getAttachmentsDir() + "/" + fileName;
+ getFile(companyId, path, fileName, response, request);
+ } else if(fileType.equals("mediaOriginal")){
+ path = item.getMediaOriginalFileDir() + "/" + fileName;
+ getFile(companyId, path, fileName, response, request);
+ } else if(fileType.equals("mediaConverted")){
+ path = item.getMediaConvertedFileDir() + "/" + fileName;
+ getFile(companyId, path, fileName, response, request);
+ } else if(fileType.equals("image")){
+ path = item.getImageFileDir() + "/" + fileName;
+ imageThumbnailFile(companyId, path, fileName, request, response);
+ }
+
+ setForward(actionRequest, ActionConstants.COMMON_NULL);
+ }
+ catch (Exception e) {
+ e.printStackTrace();
+ PortalUtil.sendError(e, actionRequest, actionResponse);
+ }
+ }
+
+ protected void imageThumbnailFile(
+ long companyId, String path, String fileName, HttpServletRequest request, HttpServletResponse response)
+ throws Exception {
+
+ InputStream is = null;
+ File tmpFile = null;
+
+ try {
+
+ is = DLStoreUtil.getFileAsStream(
+ companyId, CompanyConstants.SYSTEM, path);
+
+ tmpFile = FileUtil.createTempFile();
+ FileUtil.write(tmpFile, is);
+
+ RenderedImage renderedImage = ImageToolUtil.read(tmpFile).getRenderedImage();
+ RenderedImage thumbnail = ImageToolUtil.scale(renderedImage, 64, 64);
+
+ ByteArrayOutputStream bam = new ByteArrayOutputStream();
+
+ ImageIO.write(thumbnail, "png", bam);
+
+ String contentType = "image/png";
+ fileName = convertNonAscii(fileName);
+
+ String forceContentDisposition = ParamUtil.getString(request, "contentDispo");
+ String fcd = forceContentDisposition == null ? "" : forceContentDisposition;
+
+ ServletResponseServiceUtil.sendFile(
+ response, fileName, bam.toByteArray(), contentType, fcd);
+
+ }
+ finally {
+ ServletResponseServiceUtil.cleanUp(is);
+ if (tmpFile != null) {
+ tmpFile.delete();
+ }
+ }
+
+ }
+
+ protected void getFile(
+ long companyId, String path, String fileName, HttpServletResponse response, HttpServletRequest request)
+ throws Exception {
+
+ InputStream is = null;
+
+ try {
+
+ String targetExtension = ParamUtil.getString(request, "targetExtension");
+
+ // réalise la conversion du fichier ou nous retourne celui-ci si pas de conversion voulu
+ Map<String, Object> fais = ENTDocumentConversionServiceUtil.getFileAsInputStream(
+ path,
+ companyId,
+ targetExtension,
+ fileName
+ );
+ String contentType = (String)fais.get("contentType");
+ int contentLength = (Integer)fais.get("contentLength");
+ fileName = (String)fais.get("fileName");
+ is = (InputStream)fais.get("stream");
+
+
+ String ppos = request.getParameter("start");
+ long pos = ppos == null || ppos.length() == 0 ? 0 : Long.parseLong(ppos);
+
+ String forceContentDisposition = ParamUtil.getString(request, "contentDispo");
+ String fcd = forceContentDisposition == null ? "" : forceContentDisposition;
+
+
+ if (pos!=0 && contentType.equals("application/x-shockwave-flash") && fcd.isEmpty()) {
+ FLVPseudoStreamer flvPseudoStreamer = new FLVPseudoStreamer();
+ flvPseudoStreamer.service(request, response, fileName, is, contentLength, contentType);
+ }
+ else {
+ ServletResponseServiceUtil.sendFile(
+ response, fileName, is, contentLength, contentType, fcd);
+ }
+
+ }
+ finally {
+ if(is != null){
+ is.close();
+ }
+ }
+ }
+
+ private static final String PLAIN_ASCII =
+ "AaEeIiOoUu" // grave
+ + "AaEeIiOoUuYy" // acute
+ + "AaEeIiOoUuYy" // circumflex
+ + "AaOoNn" // tilde
+ + "AaEeIiOoUuYy" // umlaut
+ + "Aa" // ring
+ + "Cc" // cedilla
+ + "OoUu" // double acute
+ ;
+
+ private static final String UNICODE =
+ "\u00C0\u00E0\u00C8\u00E8\u00CC\u00EC\u00D2\u00F2\u00D9\u00F9"
+ + "\u00C1\u00E1\u00C9\u00E9\u00CD\u00ED\u00D3\u00F3\u00DA\u00FA\u00DD\u00FD"
+ + "\u00C2\u00E2\u00CA\u00EA\u00CE\u00EE\u00D4\u00F4\u00DB\u00FB\u0176\u0177"
+ + "\u00C3\u00E3\u00D5\u00F5\u00D1\u00F1"
+ + "\u00C4\u00E4\u00CB\u00EB\u00CF\u00EF\u00D6\u00F6\u00DC\u00FC\u0178\u00FF"
+ + "\u00C5\u00E5"
+ + "\u00C7\u00E7"
+ + "\u0150\u0151\u0170\u0171"
+ ;
+
+ // remove accentued from a string and replace with ascii equivalent
+ public static String convertNonAscii(String s) {
+ if (s == null) return null;
+ StringBuilder sb = new StringBuilder();
+ int n = s.length();
+ for (int i = 0; i < n; i++) {
+ char c = s.charAt(i);
+ int pos = UNICODE.indexOf(c);
+ if (pos > -1){
+ sb.append(PLAIN_ASCII.charAt(pos));
+ }
+ else {
+ sb.append(c);
+ }
+ }
+ return sb.toString();
+ }
+
+ protected boolean isCheckMethodOnProcessAction() {
+ return _CHECK_METHOD_ON_PROCESS_ACTION;
+ }
+
+ private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
+
+}