image preview for PDF done

git-svn-id: https://svn.research-infrastructures.eu/d4science/gcube/trunk/portlets/user/share-updates@90273 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Massimiliano Assante 2014-01-20 18:35:57 +00:00
parent 24b693bf81
commit aca1ae6aa9
10 changed files with 151 additions and 16 deletions

View File

@ -4,7 +4,7 @@
<wb-resource deploy-path="/" source-path="/src/main/webapp" tag="defaultRootSource"/> <wb-resource deploy-path="/" source-path="/src/main/webapp" tag="defaultRootSource"/>
<wb-resource deploy-path="/WEB-INF/classes" source-path="/src/main/java"/> <wb-resource deploy-path="/WEB-INF/classes" source-path="/src/main/java"/>
<wb-resource deploy-path="/WEB-INF/classes" source-path="/target/generated-sources/gwt"/> <wb-resource deploy-path="/WEB-INF/classes" source-path="/target/generated-sources/gwt"/>
<dependent-module archiveName="fileupload-progress-bar-0.1.0-SNAPSHOT.jar" deploy-path="/WEB-INF/lib" handle="module:/resource/fileupload-progress-bar/fileupload-progress-bar"> <dependent-module archiveName="fileupload-progress-bar-1.0.0-SNAPSHOT.jar" deploy-path="/WEB-INF/lib" handle="module:/resource/fileupload-progress-bar/fileupload-progress-bar">
<dependency-type>uses</dependency-type> <dependency-type>uses</dependency-type>
</dependent-module> </dependent-module>
<property name="java-output-path" value="/${module}/target/www/WEB-INF/classes"/> <property name="java-output-path" value="/${module}/target/www/WEB-INF/classes"/>

View File

@ -99,9 +99,14 @@
<dependency> <dependency>
<groupId>org.gcube.portlets.widgets</groupId> <groupId>org.gcube.portlets.widgets</groupId>
<artifactId>fileupload-progress-bar</artifactId> <artifactId>fileupload-progress-bar</artifactId>
<version>[0.1.0-SNAPSHOT, 1.0.0-SNAPSHOT)</version> <version>[1.0.0-SNAPSHOT, 2.0.0-SNAPSHOT)</version>
<scope>compile</scope> <scope>compile</scope>
</dependency> </dependency>
<dependency>
<groupId>org.swinglabs</groupId>
<artifactId>pdf-renderer</artifactId>
<version>1.0.5</version>
</dependency>
<dependency> <dependency>
<groupId>commons-fileupload</groupId> <groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId> <artifactId>commons-fileupload</artifactId>

View File

@ -24,9 +24,12 @@ public class LinkPreviewer extends Composite {
private LinkPreview toShow; private LinkPreview toShow;
@UiField HTML closeImage; private SaveInWorkspaceBox saveCopy;
@UiField ImageSwitcher switcher;
@UiField
HTML closeImage;
@UiField
ImageSwitcher switcher;
@UiField @UiField
HTML titleArea; HTML titleArea;
@UiField @UiField
@ -37,8 +40,10 @@ public class LinkPreviewer extends Composite {
CheckBox hideCheckBox; CheckBox hideCheckBox;
@UiField @UiField
CheckBox hideImageCheckBox; CheckBox hideImageCheckBox;
@UiField
Placeholder uploadInWS;
public LinkPreviewer(ShareUpdateForm parent, LinkPreview toShow) { public LinkPreviewer(ShareUpdateForm parent, LinkPreview toShow, boolean isFilePreview) {
initWidget(uiBinder.createAndBindUi(this)); initWidget(uiBinder.createAndBindUi(this));
closeImage.setStyleName("su-closeImage"); closeImage.setStyleName("su-closeImage");
closeImage.setTitle("Cancel"); closeImage.setTitle("Cancel");
@ -51,6 +56,10 @@ public class LinkPreviewer extends Composite {
String desc = toShow.getDescription(); String desc = toShow.getDescription();
descText.setHTML((desc.length() > 256) ? desc.substring(0, 256)+"..." : desc); descText.setHTML((desc.length() > 256) ? desc.substring(0, 256)+"..." : desc);
switcher.setImages(toShow.getImageUrls()); switcher.setImages(toShow.getImageUrls());
if (isFilePreview) {
saveCopy = new SaveInWorkspaceBox();
uploadInWS.add(saveCopy);
}
} }

View File

@ -32,5 +32,6 @@
</td> </td>
</tr> </tr>
</table> </table>
<m:Placeholder ui:field="uploadInWS"></m:Placeholder>
</g:HTMLPanel> </g:HTMLPanel>
</ui:UiBinder> </ui:UiBinder>

View File

@ -6,6 +6,6 @@ import com.google.gwt.user.client.ui.SimplePanel;
* @author massi * @author massi
* *
*/ */
public class LinkPlaceholder extends SimplePanel { public class Placeholder extends SimplePanel {
} }

View File

@ -0,0 +1,38 @@
package org.gcube.portlets.user.shareupdates.client.view;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HasText;
import com.google.gwt.user.client.ui.Widget;
public class SaveInWorkspaceBox extends Composite {
private static SaveInWorkspaceBoxUiBinder uiBinder = GWT
.create(SaveInWorkspaceBoxUiBinder.class);
interface SaveInWorkspaceBoxUiBinder extends
UiBinder<Widget, SaveInWorkspaceBox> {
}
public SaveInWorkspaceBox() {
initWidget(uiBinder.createAndBindUi(this));
}
@UiField
CheckBox saveCheckBox;
public SaveInWorkspaceBox(String firstName) {
initWidget(uiBinder.createAndBindUi(this));
}
}

View File

@ -0,0 +1,9 @@
<!DOCTYPE ui:UiBinder SYSTEM "http://dl.google.com/gwt/DTD/xhtml.ent">
<ui:UiBinder xmlns:ui="urn:ui:com.google.gwt.uibinder"
xmlns:g="urn:import:com.google.gwt.user.client.ui">
<g:HTMLPanel styleName="link-previewer">
<div style="padding-left: 10px;">
<g:CheckBox ui:field="saveCheckBox">Also save a copy in my Workspace</g:CheckBox>
</div>
</g:HTMLPanel>
</ui:UiBinder>

View File

@ -82,7 +82,7 @@ public class ShareUpdateForm extends Composite {
@UiField @UiField
HTMLPanel mainPanel; HTMLPanel mainPanel;
@UiField @UiField
LinkPlaceholder preview; Placeholder preview;
@UiField @UiField
Button submitButton; Button submitButton;
@ -336,7 +336,7 @@ public class ShareUpdateForm extends Composite {
public void onSuccess(LinkPreview result) { public void onSuccess(LinkPreview result) {
preview.clear(); preview.clear();
if (result != null) if (result != null)
addPreview(result); addPreview(result, false);
} }
}); });
break; break;
@ -366,7 +366,7 @@ public class ShareUpdateForm extends Composite {
preview.clear(); preview.clear();
uploadProgress.setVisible(false); uploadProgress.setVisible(false);
if (result != null) if (result != null)
addPreview(result); addPreview(result, true);
} }
}); });
} }
@ -393,9 +393,9 @@ public class ShareUpdateForm extends Composite {
* add the link preview in the view * add the link preview in the view
* @param result * @param result
*/ */
private void addPreview(LinkPreview result) { private void addPreview(LinkPreview result, boolean isFilePreview) {
uploadProgress.setVisible(false); uploadProgress.setVisible(false);
myLinkPreviewer = new LinkPreviewer(this, result); myLinkPreviewer = new LinkPreviewer(this, result, isFilePreview);
preview.add(myLinkPreviewer); preview.add(myLinkPreviewer);
} }
/** /**

View File

@ -25,7 +25,7 @@
</tr> </tr>
</table> </table>
<j:UploadProgressPanel ui:field="uploadProgress"></j:UploadProgressPanel> <j:UploadProgressPanel ui:field="uploadProgress"></j:UploadProgressPanel>
<m:LinkPlaceholder ui:field="preview"></m:LinkPlaceholder> <m:Placeholder ui:field="preview"></m:Placeholder>
<table class="toolsContainer"> <table class="toolsContainer">
<tr> <tr>

View File

@ -1,17 +1,28 @@
package org.gcube.portlets.user.shareupdates.server; package org.gcube.portlets.user.shareupdates.server;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection; import java.net.HttpURLConnection;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.URL; import java.net.URL;
import java.net.URLConnection; import java.net.URLConnection;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.UUID; import java.util.UUID;
import javax.imageio.ImageIO;
import javax.net.ssl.SSLContext; import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager; import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager; import javax.net.ssl.X509TrustManager;
@ -22,6 +33,7 @@ import org.gcube.application.framework.core.session.ASLSession;
import org.gcube.application.framework.core.session.SessionManager; import org.gcube.application.framework.core.session.SessionManager;
import org.gcube.applicationsupportlayer.social.ApplicationNotificationsManager; import org.gcube.applicationsupportlayer.social.ApplicationNotificationsManager;
import org.gcube.applicationsupportlayer.social.NotificationsManager; import org.gcube.applicationsupportlayer.social.NotificationsManager;
import org.gcube.applicationsupportlayer.social.ftp.FTPManager;
import org.gcube.common.homelibrary.home.HomeLibrary; import org.gcube.common.homelibrary.home.HomeLibrary;
import org.gcube.common.homelibrary.home.exceptions.HomeNotFoundException; import org.gcube.common.homelibrary.home.exceptions.HomeNotFoundException;
import org.gcube.common.homelibrary.home.exceptions.InternalErrorException; import org.gcube.common.homelibrary.home.exceptions.InternalErrorException;
@ -37,6 +49,7 @@ import org.gcube.portal.databook.server.DatabookStore;
import org.gcube.portal.databook.shared.ClientFeed; import org.gcube.portal.databook.shared.ClientFeed;
import org.gcube.portal.databook.shared.Feed; import org.gcube.portal.databook.shared.Feed;
import org.gcube.portal.databook.shared.FeedType; import org.gcube.portal.databook.shared.FeedType;
import org.gcube.portal.databook.shared.ImageType;
import org.gcube.portal.databook.shared.PrivacyLevel; import org.gcube.portal.databook.shared.PrivacyLevel;
import org.gcube.portal.databook.shared.UserInfo; import org.gcube.portal.databook.shared.UserInfo;
import org.gcube.portal.databook.shared.ex.FeedIDNotFoundException; import org.gcube.portal.databook.shared.ex.FeedIDNotFoundException;
@ -69,6 +82,9 @@ import com.liferay.portal.service.OrganizationLocalServiceUtil;
import com.liferay.portal.service.UserLocalServiceUtil; import com.liferay.portal.service.UserLocalServiceUtil;
import com.liferay.portal.theme.ThemeDisplay; import com.liferay.portal.theme.ThemeDisplay;
import com.sun.net.ssl.HttpsURLConnection; import com.sun.net.ssl.HttpsURLConnection;
import com.sun.pdfview.PDFFile;
import com.sun.pdfview.PDFPage;
import com.sun.pdfview.PDFParseException;
/** /**
* The server side implementation of the RPC service. * The server side implementation of the RPC service.
@ -79,6 +95,10 @@ public class ShareUpdateServiceImpl extends RemoteServiceServlet implements Shar
* *
*/ */
private static final String ADMIN_ROLE = "Administrator"; private static final String ADMIN_ROLE = "Administrator";
/**
*
*/
private static final String PDF_DEFAULT_IMAGE = "default_pdf.png";
/** /**
* *
*/ */
@ -87,6 +107,8 @@ public class ShareUpdateServiceImpl extends RemoteServiceServlet implements Shar
* The store interface * The store interface
*/ */
private DatabookStore store; private DatabookStore store;
private FTPManager ftpStore;
/** /**
* used for debugging in eclipse * used for debugging in eclipse
*/ */
@ -96,6 +118,7 @@ public class ShareUpdateServiceImpl extends RemoteServiceServlet implements Shar
*/ */
public void init() { public void init() {
store = new DBCassandraAstyanaxImpl(); store = new DBCassandraAstyanaxImpl();
ftpStore = FTPManager.getManager();
} }
public void destroy() { public void destroy() {
@ -314,17 +337,67 @@ public class ShareUpdateServiceImpl extends RemoteServiceServlet implements Shar
*/ */
@Override @Override
public LinkPreview checkUploadedFile(String fileName, String fileabsolutePathOnServer) { public LinkPreview checkUploadedFile(String fileName, String fileabsolutePathOnServer) {
String imageUrl = null;
try { try {
Thread.sleep(2000); imageUrl = getPdfPreviewImage(fileabsolutePathOnServer);
} catch (InterruptedException e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
ArrayList<String> imagesUrl = new ArrayList<String>(); ArrayList<String> imagesUrl = new ArrayList<String>();
imagesUrl.add("http://www.ask-cato.com/wp-content/uploads/2012/02/PDF5.gif"); imagesUrl.add(imageUrl);
return new LinkPreview(fileName, "the Desc", "http://www.lalala.com", "d4science.org", imagesUrl); return new LinkPreview(fileName, "the Desc", "http://www.lalala.com", "d4science.org", imagesUrl);
} }
private String getPdfPreviewImage(String path2Pdf) throws Exception {
File pdfFile = new File(path2Pdf);
RandomAccessFile raf = new RandomAccessFile(pdfFile, "r");
FileChannel channel = raf.getChannel();
ByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());
PDFFile pdf = null;
try {
pdf = new PDFFile(buf);
} catch (PDFParseException ex) {
raf.close();
_log.error("PDF Parse exception, returning default pdf image");
return ftpStore.getBaseURL()+PDF_DEFAULT_IMAGE;
}
PDFPage page = pdf.getPage(0);
int width = (int) page.getBBox().getWidth();
int height = (int) page.getBBox().getHeight();
int scaledWidth = width/8;
int scaledHeight = height/8;
// create the image
Rectangle rect = new Rectangle(0, 0, width, height);
BufferedImage bufferedImage = new BufferedImage(scaledWidth, scaledHeight, BufferedImage.TYPE_INT_RGB);
Image image = page.getImage(scaledWidth, scaledHeight, // width & height
rect, // clip rect
null, // null for the ImageObserver
true, // fill background with white
true // block until drawing is done
);
Graphics2D bufImageGraphics = bufferedImage.createGraphics();
bufImageGraphics.drawImage(image, 0, 0, scaledWidth, scaledHeight, null);
//images are very small in this case we can use in-memory streams
ByteArrayOutputStream out = new ByteArrayOutputStream();
boolean result = ImageIO.write(bufferedImage, "JPG", out);
raf.close();
if (result) {
String httpLink = ftpStore.uploadImageOnFTPServer(new ByteArrayInputStream(out.toByteArray()), ImageType.JPG);
_log.debug("PDF thumbnail available at: " + httpLink);
return httpLink;
}
else
throw new IOException("Could not process pdf file");
}
/** /**
* return the id as key and the names as value of the vre a user is subscribed to * return the id as key and the names as value of the vre a user is subscribed to
* @param username * @param username