implemented the file preview for non images with the icons depdending on the extemsion
git-svn-id: https://svn.research-infrastructures.eu/d4science/gcube/trunk/portlets/user/share-updates@90599 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
parent
e99f65e9f5
commit
7ab68fbc7d
|
@ -1,6 +1,6 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<classpath>
|
||||
<classpathentry kind="src" output="target/share-updates-1.1.0-SNAPSHOT/WEB-INF/classes" path="src/main/java">
|
||||
<classpathentry kind="src" output="target/share-updates-1.1.1-SNAPSHOT/WEB-INF/classes" path="src/main/java">
|
||||
<attributes>
|
||||
<attribute name="optional" value="true"/>
|
||||
<attribute name="maven.pomderived" value="true"/>
|
||||
|
@ -31,5 +31,5 @@
|
|||
<attribute name="maven.pomderived" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="output" path="target/share-updates-1.1.0-SNAPSHOT/WEB-INF/classes"/>
|
||||
<classpathentry kind="output" path="target/share-updates-1.1.1-SNAPSHOT/WEB-INF/classes"/>
|
||||
</classpath>
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
eclipse.preferences.version=1
|
||||
jarsExcludedFromWebInfLib=
|
||||
lastWarOutDir=/Users/massi/Documents/workspace/share-updates/target/share-updates-1.1.0-SNAPSHOT
|
||||
lastWarOutDir=/Users/massi/Documents/workspace/share-updates/target/share-updates-1.1.1-SNAPSHOT
|
||||
warSrcDir=src/main/webapp
|
||||
warSrcDirIsOutput=false
|
||||
|
|
7
pom.xml
7
pom.xml
|
@ -13,7 +13,7 @@
|
|||
<groupId>org.gcube.portlets.user</groupId>
|
||||
<artifactId>share-updates</artifactId>
|
||||
<packaging>war</packaging>
|
||||
<version>1.1.0-SNAPSHOT</version>
|
||||
<version>1.1.1-SNAPSHOT</version>
|
||||
|
||||
<name>gCube Share Updates Portlet</name>
|
||||
<description>
|
||||
|
@ -112,6 +112,11 @@
|
|||
<artifactId>pdfbox</artifactId>
|
||||
<version>1.8.3</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>eu.medsea.mimeutil</groupId>
|
||||
<artifactId>mime-util</artifactId>
|
||||
<version>2.1.3</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>commons-fileupload</groupId>
|
||||
<artifactId>commons-fileupload</artifactId>
|
||||
|
|
|
@ -12,6 +12,8 @@ import java.io.RandomAccessFile;
|
|||
import java.nio.ByteBuffer;
|
||||
import java.nio.channels.FileChannel;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
|
||||
|
@ -35,12 +37,19 @@ import com.sun.pdfview.PDFParseException;
|
|||
*/
|
||||
public class FilePreviewer {
|
||||
private static Logger _log = LoggerFactory.getLogger(FilePreviewer.class);
|
||||
|
||||
private static final String PDF_DEFAULT_IMAGE = "default_pdf.png";
|
||||
|
||||
|
||||
private static final String PDF_DEFAULT_IMAGE = "default/pdf.png";
|
||||
private static final String NOTHUMB_DEFAULT_IMAGE = "default/default_image.png";
|
||||
private static final String GENERICFILE_DEFAULT_IMAGE = "default/default_generic.png";
|
||||
/**
|
||||
* these are the extension for which I have an icon image preview
|
||||
*/
|
||||
private static final String[] handledextensionImages = {"css", "csv", "doc", "docx", "java", "mdb", "mp3", "pdf", "ppt", "pptx", "psd", "rar", "tex", "txt", "xls", "xlsx", "zip"};
|
||||
|
||||
private static FTPManager getFTPManager() {
|
||||
return FTPManager.getInstance();
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param fileName thename of the file
|
||||
|
@ -49,7 +58,7 @@ public class FilePreviewer {
|
|||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
protected static LinkPreview getPdfPreview(String fileName, String path2Pdf, String httpUrl) throws Exception {
|
||||
protected static LinkPreview getPdfPreview(String fileName, String path2Pdf, String httpUrl, String mimeType) throws Exception {
|
||||
ArrayList<String> imagesUrl = new ArrayList<String>();
|
||||
//description
|
||||
String desc = null;
|
||||
|
@ -62,6 +71,7 @@ public class FilePreviewer {
|
|||
}
|
||||
//thumbnail preview
|
||||
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());
|
||||
|
@ -73,7 +83,7 @@ public class FilePreviewer {
|
|||
_log.error("PDF Parse exception, returning default pdf image");
|
||||
|
||||
imagesUrl.add(getFTPManager().getBaseURL()+PDF_DEFAULT_IMAGE);
|
||||
return new LinkPreview(fileName, desc, httpUrl, "d4science.org", imagesUrl);
|
||||
return new LinkPreview(fileName, desc, httpUrl, mimeType, imagesUrl);
|
||||
}
|
||||
PDFPage page = pdf.getPage(0);
|
||||
|
||||
|
@ -102,12 +112,45 @@ public class FilePreviewer {
|
|||
String httpLink = getFTPManager().uploadImageOnFTPServer(new ByteArrayInputStream(out.toByteArray()), ImageType.JPG);
|
||||
_log.debug("PDF thumbnail available at: " + httpLink);
|
||||
imagesUrl.add(httpLink);
|
||||
return new LinkPreview(fileName, desc, httpUrl, "d4science.org", imagesUrl);
|
||||
return new LinkPreview(fileName, desc, httpUrl, mimeType, imagesUrl);
|
||||
}
|
||||
else
|
||||
throw new IOException("Could not process pdf file");
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* @param fileName thename of the file
|
||||
* @param path2Pdf the path of the pdf file
|
||||
* @param httpUrl the http url where the file is reachable at
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
protected static LinkPreview getUnhandledTypePreview(String fileName, String path2Pdf, String httpUrl, String mimeType) throws Exception {
|
||||
|
||||
ArrayList<String> imagesUrl = new ArrayList<String>();
|
||||
String extension = getExtension(fileName);
|
||||
//no description
|
||||
String desc = "";
|
||||
if (extension == null)
|
||||
imagesUrl.add(getFTPManager().getBaseURL()+GENERICFILE_DEFAULT_IMAGE);
|
||||
else {
|
||||
int foundIndex = Arrays.binarySearch(handledextensionImages, extension);
|
||||
if (foundIndex < 0)
|
||||
imagesUrl.add(getFTPManager().getBaseURL()+GENERICFILE_DEFAULT_IMAGE);
|
||||
else
|
||||
imagesUrl.add(getFTPManager().getBaseURL()+"default/"+extension+".png");
|
||||
}
|
||||
return new LinkPreview(fileName, desc, httpUrl, mimeType, imagesUrl);
|
||||
}
|
||||
|
||||
private static String getExtension(String fileName) {
|
||||
int lastDot = fileName.lastIndexOf(".");
|
||||
String extension = fileName.substring(lastDot+1);
|
||||
_log.debug("EXTENSION FOUND = " + extension);
|
||||
return extension;
|
||||
}
|
||||
/**
|
||||
*
|
||||
* @param path2File
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
package org.gcube.portlets.user.shareupdates.server;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.HttpURLConnection;
|
||||
|
@ -22,13 +23,13 @@ import org.gcube.application.framework.core.session.ASLSession;
|
|||
import org.gcube.application.framework.core.session.SessionManager;
|
||||
import org.gcube.applicationsupportlayer.social.ApplicationNotificationsManager;
|
||||
import org.gcube.applicationsupportlayer.social.NotificationsManager;
|
||||
import org.gcube.applicationsupportlayer.social.storage.FTPManager;
|
||||
import org.gcube.applicationsupportlayer.social.storage.UriResolverReaderParameter;
|
||||
import org.gcube.common.homelibrary.home.HomeLibrary;
|
||||
import org.gcube.common.homelibrary.home.exceptions.HomeNotFoundException;
|
||||
import org.gcube.common.homelibrary.home.exceptions.InternalErrorException;
|
||||
import org.gcube.common.homelibrary.home.workspace.Workspace;
|
||||
import org.gcube.common.homelibrary.home.workspace.exceptions.WorkspaceFolderNotFoundException;
|
||||
import org.gcube.common.homelibrary.util.MimeTypeUtil;
|
||||
import org.gcube.common.portal.PortalContext;
|
||||
import org.gcube.common.scope.api.ScopeProvider;
|
||||
import org.gcube.common.scope.impl.ScopeBean;
|
||||
|
@ -101,10 +102,6 @@ public class ShareUpdateServiceImpl extends RemoteServiceServlet implements Shar
|
|||
* The Cassandra store interface
|
||||
*/
|
||||
private DatabookStore store;
|
||||
/**
|
||||
* the FTP Storage
|
||||
*/
|
||||
private FTPManager ftpStore;
|
||||
/**
|
||||
* used for debugging in eclipse
|
||||
*/
|
||||
|
@ -113,8 +110,7 @@ public class ShareUpdateServiceImpl extends RemoteServiceServlet implements Shar
|
|||
* connect to cassandra at startup
|
||||
*/
|
||||
public void init() {
|
||||
store = new DBCassandraAstyanaxImpl();
|
||||
ftpStore = FTPManager.getInstance();
|
||||
store = new DBCassandraAstyanaxImpl();
|
||||
}
|
||||
|
||||
public void destroy() {
|
||||
|
@ -134,7 +130,7 @@ public class ShareUpdateServiceImpl extends RemoteServiceServlet implements Shar
|
|||
user = "test.user";
|
||||
user = "massimiliano.assante";
|
||||
SessionManager.getInstance().getASLSession(sessionID, user).setScope("/gcube/devsec/devVRE");
|
||||
withinPortal = false;
|
||||
withinPortal = false;
|
||||
}
|
||||
else {
|
||||
withinPortal = true;
|
||||
|
@ -337,29 +333,40 @@ public class ShareUpdateServiceImpl extends RemoteServiceServlet implements Shar
|
|||
|
||||
String randomUploadFolderName = UUID.randomUUID().toString();
|
||||
String remoteFilePath = UPLOAD_DIR + "/" + randomUploadFolderName + "/" + fileName;
|
||||
|
||||
//get the Storage Client
|
||||
String currScope = ScopeProvider.instance.get();
|
||||
ScopeProvider.instance.set("/"+PortalContext.getConfiguration().getInfrastructureName());
|
||||
IClient storageClient = new StorageClient(STORAGE_OWNER, AccessType.SHARED, MemoryType.PERSISTENT).getClient();
|
||||
ScopeProvider.instance.set(currScope);
|
||||
|
||||
|
||||
|
||||
String httpURL = "";
|
||||
|
||||
//get the url to show, before actually uploading it
|
||||
String smpURI = storageClient.getUrl().RFile(remoteFilePath);
|
||||
|
||||
//The uploader Thread starts here asyncronously
|
||||
//The storage uploader Thread starts here asyncronously
|
||||
Thread thread = new Thread(new UploadToStorageThread(storageClient, fileName, fileabsolutePathOnServer, remoteFilePath));
|
||||
thread.start();
|
||||
|
||||
|
||||
try {
|
||||
String mimeType = getMimeType(new File(fileabsolutePathOnServer));
|
||||
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, "application/pdf", true);
|
||||
httpURL = resolver.resolveAsUriRequest(smpURI, fileName, mimeType, true);
|
||||
|
||||
//TODO: switch
|
||||
toReturn = FilePreviewer.getPdfPreview(fileName, fileabsolutePathOnServer, httpURL);
|
||||
switch (mimeType) {
|
||||
case "application/pdf":
|
||||
toReturn = FilePreviewer.getPdfPreview(fileName, fileabsolutePathOnServer, httpURL, mimeType);
|
||||
break;
|
||||
case "application/vnd.openxmlformats-officedocument.wordprocessingml.document":
|
||||
mimeType = "application/word";
|
||||
case "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet":
|
||||
mimeType = "application/excel";
|
||||
default:
|
||||
return FilePreviewer.getUnhandledTypePreview(fileName, fileabsolutePathOnServer, httpURL, mimeType);
|
||||
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
_log.error("Error while resolving or previewing file");
|
||||
e.printStackTrace();
|
||||
|
@ -371,7 +378,17 @@ public class ShareUpdateServiceImpl extends RemoteServiceServlet implements Shar
|
|||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* @param file
|
||||
* @return
|
||||
* @throws MagicParseException
|
||||
* @throws MagicMatchNotFoundException
|
||||
* @throws MagicException
|
||||
*/
|
||||
private String getMimeType(File file) {
|
||||
return MimeTypeUtil.getMimeType(file);
|
||||
}
|
||||
|
||||
/**
|
||||
* return the id as key and the names as value of the vre a user is subscribed to
|
||||
|
|
|
@ -157,6 +157,7 @@
|
|||
font-weight: bold;
|
||||
line-height: 15px;
|
||||
width: 465px;
|
||||
word-break:break-all;
|
||||
}
|
||||
|
||||
.link-url {
|
||||
|
|
Loading…
Reference in New Issue