added thumbnail generation and upload on FTP server, only upload a copy on my workspace is missing

git-svn-id: https://svn.research-infrastructures.eu/d4science/gcube/trunk/portlets/user/share-updates@90680 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Massimiliano Assante 2014-01-28 18:32:14 +00:00
parent 7ab68fbc7d
commit 0a1b754573
3 changed files with 77 additions and 9 deletions

15
pom.xml
View File

@ -13,7 +13,7 @@
<groupId>org.gcube.portlets.user</groupId>
<artifactId>share-updates</artifactId>
<packaging>war</packaging>
<version>1.1.1-SNAPSHOT</version>
<version>1.1.2-SNAPSHOT</version>
<name>gCube Share Updates Portlet</name>
<description>
@ -113,9 +113,16 @@
<version>1.8.3</version>
</dependency>
<dependency>
<groupId>eu.medsea.mimeutil</groupId>
<artifactId>mime-util</artifactId>
<version>2.1.3</version>
<groupId>org.apache.tika</groupId>
<artifactId>tika-core</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>org.imgscalr</groupId>
<artifactId>imgscalr-lib</artifactId>
<version>4.2</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>

View File

@ -22,6 +22,8 @@ import org.apache.pdfbox.util.PDFTextStripper;
import org.gcube.applicationsupportlayer.social.storage.FTPManager;
import org.gcube.portal.databook.shared.ImageType;
import org.gcube.portlets.user.shareupdates.shared.LinkPreview;
import org.imgscalr.Scalr;
import org.imgscalr.Scalr.Method;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -117,8 +119,34 @@ public class FilePreviewer {
else
throw new IOException("Could not process pdf file");
}
/**
*
* @param fileName thename of the file
* @param path2Image the path of the image file
* @param httpUrl the http url where the file is reachable at
* @return
* @throws Exception
*/
protected static LinkPreview getImagePreview(String fileName, String path2Image, String httpUrl, String mimeType) throws Exception {
ArrayList<String> imagesUrl = new ArrayList<String>();
//description
String desc = "";
BufferedImage img = ImageIO.read(new File(path2Image)); // load image
BufferedImage thumbnail = Scalr.resize(img, Method.QUALITY, 80, Scalr.OP_ANTIALIAS);
ByteArrayOutputStream out = new ByteArrayOutputStream();
boolean result = ImageIO.write(thumbnail, "JPG", out);
if (result) {
String httpLink = getFTPManager().uploadImageOnFTPServer(new ByteArrayInputStream(out.toByteArray()), ImageType.JPG);
_log.debug("Image thumbnail available at: " + httpLink);
imagesUrl.add(httpLink);
return new LinkPreview(fileName, desc, httpUrl, mimeType, imagesUrl);
}
else
throw new IOException("Could not process pdf file");
}
/**
*
* @param fileName thename of the file

View File

@ -1,12 +1,16 @@
package org.gcube.portlets.user.shareupdates.server;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
@ -19,6 +23,14 @@ import javax.net.ssl.X509TrustManager;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.validator.routines.UrlValidator;
import org.apache.tika.config.TikaConfig;
import org.apache.tika.detect.Detector;
import org.apache.tika.io.TikaInputStream;
import org.apache.tika.metadata.Metadata;
import org.apache.tika.mime.MediaType;
import org.apache.tika.parser.AutoDetectParser;
import org.apache.tika.parser.Parser;
import org.apache.tika.sax.BodyContentHandler;
import org.gcube.application.framework.core.session.ASLSession;
import org.gcube.application.framework.core.session.SessionManager;
import org.gcube.applicationsupportlayer.social.ApplicationNotificationsManager;
@ -67,6 +79,7 @@ import org.slf4j.LoggerFactory;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.tidy.Tidy;
import org.xml.sax.ContentHandler;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.liferay.portal.kernel.exception.PortalException;
@ -349,7 +362,7 @@ public class ShareUpdateServiceImpl extends RemoteServiceServlet implements Shar
try {
String mimeType = getMimeType(new File(fileabsolutePathOnServer));
String mimeType = getMimeType(new File(fileabsolutePathOnServer), fileName);
UriResolverReaderParameter resolver = new UriResolverReaderParameter();
//get the url to show (though it could not be ready for download at this stage)
httpURL = resolver.resolveAsUriRequest(smpURI, fileName, mimeType, true);
@ -359,9 +372,22 @@ public class ShareUpdateServiceImpl extends RemoteServiceServlet implements Shar
toReturn = FilePreviewer.getPdfPreview(fileName, fileabsolutePathOnServer, httpURL, mimeType);
break;
case "application/vnd.openxmlformats-officedocument.wordprocessingml.document":
mimeType = "application/word";
mimeType = "application/wordprocessor";
break;
case "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet":
mimeType = "application/excel";
mimeType = "application/spreadsheet";
break;
case "application/vnd.openxmlformats-officedocument.presentationml.presentation":
mimeType = "application/presentation";
break;
case "image/png":
case "image/gif":
case "image/tiff":
case "image/jpg":
case "image/jpeg":
case "image/bmp":
toReturn = FilePreviewer.getImagePreview(fileName, fileabsolutePathOnServer, httpURL, mimeType);
break;
default:
return FilePreviewer.getUnhandledTypePreview(fileName, fileabsolutePathOnServer, httpURL, mimeType);
@ -382,12 +408,19 @@ public class ShareUpdateServiceImpl extends RemoteServiceServlet implements Shar
*
* @param file
* @return
* @throws IOException
* @throws MagicParseException
* @throws MagicMatchNotFoundException
* @throws MagicException
*/
private String getMimeType(File file) {
return MimeTypeUtil.getMimeType(file);
private String getMimeType(File file, String filenameWithExtension) throws IOException {
TikaConfig config = TikaConfig.getDefaultConfig();
Detector detector = config.getDetector();
TikaInputStream stream = TikaInputStream.get(file);
Metadata metadata = new Metadata();
metadata.add(Metadata.RESOURCE_NAME_KEY, filenameWithExtension);
MediaType mediaType = detector.detect(stream, metadata);
return mediaType.getBaseType().toString();
}
/**