diff --git a/.settings/org.eclipse.wst.common.component b/.settings/org.eclipse.wst.common.component
index 8907d91..bbf0648 100644
--- a/.settings/org.eclipse.wst.common.component
+++ b/.settings/org.eclipse.wst.common.component
@@ -5,9 +5,6 @@
-
- uses
-
diff --git a/distro/changelog.xml b/distro/changelog.xml
index 007e722..7aad0a4 100644
--- a/distro/changelog.xml
+++ b/distro/changelog.xml
@@ -2,8 +2,7 @@
Removed asl session dependency
- Thumbnails are no longer stored on the FTP server. Storage
- service is used
+ Thumbnails are no now stored on ftp server
diff --git a/src/main/java/org/gcube/portlets/user/shareupdates/server/FilePreviewer.java b/src/main/java/org/gcube/portlets/user/shareupdates/server/FilePreviewer.java
index 22824cf..eb329e4 100644
--- a/src/main/java/org/gcube/portlets/user/shareupdates/server/FilePreviewer.java
+++ b/src/main/java/org/gcube/portlets/user/shareupdates/server/FilePreviewer.java
@@ -10,7 +10,6 @@ import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
-import java.io.InputStream;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.URL;
@@ -24,7 +23,6 @@ import java.util.UUID;
import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;
-import javax.servlet.ServletContext;
import net.coobird.thumbnailator.Thumbnails;
@@ -36,7 +34,8 @@ 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.gcube.contentmanagement.blobstorage.service.IClient;
+import org.gcube.applicationsupportlayer.social.storage.FTPManager;
+import org.gcube.portal.databook.shared.ImageType;
import org.gcube.portlets.user.shareupdates.shared.LinkPreview;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -45,36 +44,37 @@ import com.sun.pdfview.PDFFile;
import com.sun.pdfview.PDFPage;
import com.sun.pdfview.PDFParseException;
/**
- * Parse files and returns an image preview plus description
+ *
* @author Massimiliano Assante, ISTI-CNR
+ *
+ * Parse files and returns an image preview plus description
+ *
*/
public class FilePreviewer {
+ private static Logger _log = LoggerFactory.getLogger(FilePreviewer.class);
- private static final Logger _log = LoggerFactory.getLogger(FilePreviewer.class);
+ private static final String PDF_DEFAULT_IMAGE = "default/pdf.png";
+ private static final String GENERICFILE_DEFAULT_IMAGE = "default/default_generic.png";
private static final String UPLOAD_LOCATION_LOCAL = System.getProperty("java.io.tmpdir");
- public static final String DIR_STORAGE_DEFAULT_ICONS = "default-icons-directory"; // to be append to UPLOAD_DIR
-
+
/**
* 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 fileNameLabel thename of the file
* @param path2Pdf the path of the pdf file
* @param httpUrl the http url where the file is reachable at
- * @param storageClient
* @return
* @throws Exception
*/
- protected static LinkPreview getPdfPreview(
- String fileName,
- String path2Pdf,
- String httpUrl,
- String mimeType,
- IClient sClient,
- ServletContext sContext) throws Exception {
+ protected static LinkPreview getPdfPreview(String fileName, String path2Pdf, String httpUrl, String mimeType) throws Exception {
ArrayList imagesUrl = new ArrayList();
//description
String desc = null;
@@ -97,7 +97,8 @@ public class FilePreviewer {
} catch (PDFParseException ex) {
raf.close();
_log.error("PDF Parse exception, returning default pdf image");
- imagesUrl.add(getDefaultIconUrl(sClient, IconFileNames.PDF.getFileName(), sContext));
+
+ imagesUrl.add(getFTPManager().getBaseURL()+PDF_DEFAULT_IMAGE);
return new LinkPreview(fileName, desc, httpUrl, mimeType, imagesUrl);
}
PDFPage page = pdf.getPage(0);
@@ -110,8 +111,11 @@ public class FilePreviewer {
// 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, rect, null, true, true);
+
Graphics2D bufImageGraphics = bufferedImage.createGraphics();
bufImageGraphics.drawImage(image, 0, 0, scaledWidth, scaledHeight, null);
@@ -121,14 +125,9 @@ public class FilePreviewer {
raf.close();
if (result) {
- try{
- String storagehttpLink = syncUploadThumbnailStorage(new ByteArrayInputStream(out.toByteArray()), sClient, "thumbnail_" + fileName);
- imagesUrl.add(storagehttpLink);
- _log.debug("PDF thumbnail available at: " + storagehttpLink);
- }catch(Exception e){
- _log.error("Failed to create a thumbnail for the preview ...");
- imagesUrl.add(getDefaultIconUrl(sClient, IconFileNames.PDF.getFileName(), sContext));
- }
+ 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, mimeType, imagesUrl);
}
else
@@ -142,7 +141,7 @@ public class FilePreviewer {
* @return
* @throws Exception
*/
- protected static LinkPreview getImagePreview(String fileName, String path2Image, String httpUrl, String mimeType, IClient sClient, ServletContext sContext) {
+ protected static LinkPreview getImagePreview(String fileName, String path2Image, String httpUrl, String mimeType) {
ArrayList imagesUrl = new ArrayList();
Dimension dim;
@@ -160,15 +159,13 @@ public class FilePreviewer {
.width(80)
.outputFormat("jpg")
.toOutputStream(out);
-
- String storagehttpLink = syncUploadThumbnailStorage(new ByteArrayInputStream(out.toByteArray()), sClient, "thumbnail_" + fileName);
- _log.debug("Image thumbnail available at: " + storagehttpLink);
- imagesUrl.add(storagehttpLink);
- }catch (IOException e) {
- _log.error("Failed to get a preview... Using generic image");
- imagesUrl.add(getDefaultIconUrl(sClient, IconFileNames.IMAGE_GENERIC.getFileName(), sContext));
}
-
+ catch (IOException e) {
+ e.printStackTrace();
+ }
+ String httpLink = getFTPManager().uploadImageOnFTPServer(new ByteArrayInputStream(out.toByteArray()), ImageType.JPG);
+ _log.debug("\nFlushed, Image thumbnail available at: " + httpLink);
+ imagesUrl.add(httpLink);
return new LinkPreview(fileName, desc, httpUrl, mimeType, imagesUrl);
}
@@ -180,26 +177,20 @@ public class FilePreviewer {
* @return
* @throws Exception
*/
- protected static LinkPreview getUnhandledTypePreview(
- String fileName,
- String path2Pdf,
- String httpUrl,
- String mimeType,
- IClient sClient,
- ServletContext sContext) throws Exception {
+ protected static LinkPreview getUnhandledTypePreview(String fileName, String path2Pdf, String httpUrl, String mimeType) throws Exception {
ArrayList imagesUrl = new ArrayList();
String extension = getExtension(fileName);
//no description
String desc = "";
if (extension == null)
- imagesUrl.add(getDefaultIconUrl(sClient, IconFileNames.GENERIC.getFileName(), sContext));
+ imagesUrl.add(getFTPManager().getBaseURL()+GENERICFILE_DEFAULT_IMAGE);
else {
int foundIndex = Arrays.binarySearch(handledextensionImages, extension);
if (foundIndex < 0)
- imagesUrl.add(getDefaultIconUrl(sClient, IconFileNames.GENERIC.getFileName(), sContext));
+ imagesUrl.add(getFTPManager().getBaseURL()+GENERICFILE_DEFAULT_IMAGE);
else
- imagesUrl.add(getDefaultIconUrl(sClient, extension+".png", sContext));
+ imagesUrl.add(getFTPManager().getBaseURL()+"default/"+extension+".png");
}
return new LinkPreview(fileName, desc, httpUrl, mimeType, imagesUrl);
}
@@ -270,47 +261,10 @@ public class FilePreviewer {
MediaType mediaType = detector.detect(stream, metadata);
return mediaType.getBaseType().toString();
}
-
- /**
- * Same as above, but having as input an inputstream object
- * @param is
- * @param filenameWithExtension
- * @return
- * @throws IOException
- */
- protected static String getMimeType(InputStream is, String filenameWithExtension) throws IOException {
- TikaConfig config = TikaConfig.getDefaultConfig();
- Detector detector = config.getDetector();
- TikaInputStream stream = TikaInputStream.get(is);
- Metadata metadata = new Metadata();
- metadata.add(Metadata.RESOURCE_NAME_KEY, filenameWithExtension);
- MediaType mediaType = detector.detect(stream, metadata);
- return mediaType.getBaseType().toString();
- }
-
- /**
- * Upload a thumbnail on the storage and returns the http url. The operation is synchronous
- * @param sClient
- * @param byteArrayInputStream
- * @return http url of the thumbnail
- */
- protected static String syncUploadThumbnailStorage(ByteArrayInputStream byteArrayInputStream, IClient sClient, String thumbnailFileName){
- try{
- // a random remote folder/file on the storage for the thumbnail
- String randomUploadFolderName = UUID.randomUUID().toString();
- String remoteFilePath = ShareUpdateServiceImpl.UPLOAD_DIR + "/" + randomUploadFolderName + "/" + thumbnailFileName;
- String thumbnailMime = getMimeType(byteArrayInputStream, null);
- sClient.put(true, thumbnailMime).LFile(byteArrayInputStream).RFile(remoteFilePath);
- return sClient.getHttpUrl(true).RFile(remoteFilePath);
- }catch(Exception e){
- _log.error("Failed to upload thumbnail on storage...");
- return null;
- }
- }
-
+
/**
* Check if a resource at a given url exists or not. If it exists, create a file in the temp dir
- * of the tomcat. Files and folers are deleted on exit.
+ * of the tomcat
* @param urlThumbnail
* @return
*/
@@ -352,66 +306,33 @@ public class FilePreviewer {
* @param urlThumbnail
* @return the url of the thumbnail saved on the storage or null in case of error
*/
- public static String getThumbnailFromUrl(String urlThumbnail, IClient sClient, ServletContext sContext) {
+ public static String saveThumbnailOnFTPAndGetUrl(String urlThumbnail) {
File localFile;
if((localFile = storeAndGetFile(urlThumbnail)) != null){
String mimeType = null;
- try {
- mimeType = FilePreviewer.getMimeType(localFile, localFile.getName());
- String thumbnailUrlStorage = null;
- switch(mimeType){
- case "image/png":
- case "image/gif":
- case "image/tiff":
- case "image/jpg":
- case "image/jpeg":
- case "image/bmp":
- thumbnailUrlStorage = getImagePreview(localFile.getName(), localFile.getAbsolutePath(), null, mimeType, sClient, sContext).getImageUrls().get(0);
- break;
- default: break;
+ if (ShareUpdateServiceImpl.isWithinPortal()) {
+ try {
+ mimeType = FilePreviewer.getMimeType(localFile, localFile.getName());
+ String thumbnailUrlFTP = null;
+ switch(mimeType){
+ case "image/png":
+ case "image/gif":
+ case "image/tiff":
+ case "image/jpg":
+ case "image/jpeg":
+ case "image/bmp":
+ thumbnailUrlFTP = FilePreviewer.getImagePreview(localFile.getName(), localFile.getAbsolutePath(), null, mimeType).getImageUrls().get(0);
+ break;
+ default: break;
+ }
+ return thumbnailUrlFTP;
+ } catch (IOException e) {
+ _log.error("Error while saving thumbnail on ftp", e);
}
- return thumbnailUrlStorage;
- } catch (IOException e) {
- _log.error("Error while saving thumbnail on ftp", e);
}
-
}else
_log.warn("the file at url " + urlThumbnail + " doesn't exist");
return null;
}
-
- /**
- * When the preview methods fail to create a thumbnail and one of the "default icons" needs to be used,
- * the method retrieve the url of these files from the storage. In case the icon is missing, it upload the file too on the storage
- * before returning.
- * @param storage
- * @return
- */
- private static String getDefaultIconUrl(IClient sClient, String iconFile, ServletContext sContext){
-
- try{
- //TODO do I need to download the whole stream?!
- String iconPathOnStorage = ShareUpdateServiceImpl.UPLOAD_DIR + "/" + FilePreviewer.DIR_STORAGE_DEFAULT_ICONS + "/" + iconFile;
- _log.debug("looking for remote file at relative url " + iconPathOnStorage);
-
- InputStream inputStream = sClient.get().RFileAsInputStream(iconPathOnStorage);
- if(inputStream == null){
- _log.info("Ok, the default icon was not on the storage.. going to put it there");
- InputStream localInputStream = sContext.getResourceAsStream("/images/default/" + iconFile);
- if(localInputStream != null){
- sClient.put(true).LFile(localInputStream).RFile(iconPathOnStorage);
- _log.info("Request for upload started for file " + iconPathOnStorage + ", trying to get the http url");
- return sClient.getHttpUrl(true).RFile(iconPathOnStorage);
- }else{
- _log.warn("Failed to get the inputstream of the icon " + iconFile + " form the subpath /images/default/");
- return null;
- }
- }else
- return sClient.getHttpUrl(true).RFile(iconPathOnStorage);
- }catch(Exception e){
- _log.error("Something went wrong with the storage for the default image.. returning null", e);
- }
- return null;
- }
}
diff --git a/src/main/java/org/gcube/portlets/user/shareupdates/server/IconFileNames.java b/src/main/java/org/gcube/portlets/user/shareupdates/server/IconFileNames.java
deleted file mode 100644
index 83d9d87..0000000
--- a/src/main/java/org/gcube/portlets/user/shareupdates/server/IconFileNames.java
+++ /dev/null
@@ -1,38 +0,0 @@
-package org.gcube.portlets.user.shareupdates.server;
-
-/**
- * Icons type
- * @author Costantino Perciante at ISTI-CNR
- * (costantino.perciante@isti.cnr.it)
- */
-public enum IconFileNames {
-
- GENERIC("default_generic.png"),
- IMAGE_GENERIC("default_image.png"),
- DOCX("docx.png"),
- PPTX("pptx.png"),
- PDF("pdf.png"),
- RAR("rar.png"),
- ZIP("zip.png"),
- XLS("xls.png"),
- CSS("css.png"),
- CSV("csv.png"),
- DOC("doc.png"),
- JAVA("java.png"),
- MDB("mdb.png"),
- MP3("mp3.png"),
- PSD("psd.png"),
- TEX("tex.png"),
- PPT("ppt.png"),
- XLSX("xlsx.png");
-
- private String fileName;
-
- IconFileNames(String fileName){
- this.fileName = fileName;
- }
-
- public String getFileName(){
- return this.fileName;
- }
-}
diff --git a/src/main/java/org/gcube/portlets/user/shareupdates/server/ShareUpdateServiceImpl.java b/src/main/java/org/gcube/portlets/user/shareupdates/server/ShareUpdateServiceImpl.java
index 85f9198..b5210af 100644
--- a/src/main/java/org/gcube/portlets/user/shareupdates/server/ShareUpdateServiceImpl.java
+++ b/src/main/java/org/gcube/portlets/user/shareupdates/server/ShareUpdateServiceImpl.java
@@ -100,7 +100,7 @@ public class ShareUpdateServiceImpl extends RemoteServiceServlet implements Shar
*
* @return true if you're running into the portal, false if in development
*/
- private boolean isWithinPortal() {
+ public static boolean isWithinPortal() {
try {
UserLocalServiceUtil.getService();
return true;
@@ -149,7 +149,7 @@ public class ShareUpdateServiceImpl extends RemoteServiceServlet implements Shar
if (urlThumbnail == null)
urlThumbnail = "null";
else
- urlThumbnail = FilePreviewer.getThumbnailFromUrl(urlThumbnail, getStorage(), getServletContext());
+ urlThumbnail = FilePreviewer.saveThumbnailOnFTPAndGetUrl(urlThumbnail);
Date feedDate = new Date();
@@ -576,20 +576,24 @@ public class ShareUpdateServiceImpl extends RemoteServiceServlet implements Shar
return false;
}
}
+
/**
* generate a preview of the file, upload the file on the storage and shorts the link
*/
@Override
public LinkPreview checkUploadedFile(String fileName, String fileabsolutePathOnServer) {
+ PortalContext context = PortalContext.getConfiguration();
+
LinkPreview toReturn = null;
- // a random remote folder/file on the storage for the payload
String randomUploadFolderName = UUID.randomUUID().toString();
String remoteFilePath = UPLOAD_DIR + "/" + randomUploadFolderName + "/" + fileName;
-
//get the Storage Client
- IClient storageClient = getStorage();
+ String currScope = ScopeProvider.instance.get();
+ ScopeProvider.instance.set(context.getCurrentScope(getThreadLocalRequest()));
+ IClient storageClient = new StorageClient(STORAGE_OWNER, AccessType.SHARED, MemoryType.PERSISTENT).getClient();
+ ScopeProvider.instance.set(currScope);
String httpURL = "";
String smpURI = "";
@@ -607,48 +611,54 @@ public class ShareUpdateServiceImpl extends RemoteServiceServlet implements Shar
}
Thread thread = new Thread(new UploadToStorageThread(storageClient, fileName, fileabsolutePathOnServer, remoteFilePath, mimeType));
thread.start();
-
- try {
- //get the url to show (though it could not be ready for download at this stage)
- httpURL = smpURI;
-
- switch (mimeType) {
- case "application/pdf":
- toReturn = FilePreviewer.getPdfPreview(fileName, fileabsolutePathOnServer, httpURL, mimeType, storageClient, getServletContext());
- break;
- case "application/vnd.openxmlformats-officedocument.wordprocessingml.document":
- mimeType = "application/wordprocessor";
- return FilePreviewer.getUnhandledTypePreview(fileName, fileabsolutePathOnServer, httpURL, mimeType, storageClient, getServletContext());
- case "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet":
- mimeType = "application/spreadsheet";
- return FilePreviewer.getUnhandledTypePreview(fileName, fileabsolutePathOnServer, httpURL, mimeType, storageClient, getServletContext());
- case "application/vnd.openxmlformats-officedocument.presentationml.presentation":
- mimeType = "application/presentation";
- return FilePreviewer.getUnhandledTypePreview(fileName, fileabsolutePathOnServer, httpURL, mimeType, storageClient, getServletContext());
- 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, storageClient, getServletContext());
- break;
- default:
- return FilePreviewer.getUnhandledTypePreview(fileName, fileabsolutePathOnServer, httpURL, mimeType, storageClient, getServletContext());
- }
- } catch (Exception e) {
- _log.error("Error while resolving or previewing file", e);
- try {
- return FilePreviewer.getUnhandledTypePreview(fileName, fileabsolutePathOnServer, httpURL, "Error During upload on Server!", storageClient, getServletContext());
- } catch (Exception e1) {
- _log.error("Unable to extract a preview ...", e1);
- }
- }
}
+
+ try {
+ //get the url to show (though it could not be ready for download at this stage)
+ httpURL = smpURI;
+
+ switch (mimeType) {
+ case "application/pdf":
+ toReturn = FilePreviewer.getPdfPreview(fileName, fileabsolutePathOnServer, httpURL, mimeType);
+ break;
+ case "application/vnd.openxmlformats-officedocument.wordprocessingml.document":
+ mimeType = "application/wordprocessor";
+ return FilePreviewer.getUnhandledTypePreview(fileName, fileabsolutePathOnServer, httpURL, mimeType);
+ case "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet":
+ mimeType = "application/spreadsheet";
+ return FilePreviewer.getUnhandledTypePreview(fileName, fileabsolutePathOnServer, httpURL, mimeType);
+ case "application/vnd.openxmlformats-officedocument.presentationml.presentation":
+ mimeType = "application/presentation";
+ return FilePreviewer.getUnhandledTypePreview(fileName, fileabsolutePathOnServer, httpURL, mimeType);
+ 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);
+
+ }
+
+ } catch (Exception e) {
+ _log.error("Error while resolving or previewing file");
+ e.printStackTrace();
+ try {
+ return FilePreviewer.getUnhandledTypePreview(fileName, fileabsolutePathOnServer, httpURL, "Error During upload on Server!");
+ } catch (Exception e1) {
+ e1.printStackTrace();
+ }
+
+ }
+
_log.debug("smpURI=" + smpURI);
_log.debug("Returning httpURL=" + httpURL);
return toReturn;
}
+
/**
* tries the following in the indicated order for Populating the Link preview
* Open Graph protocol
@@ -854,20 +864,4 @@ public class ShareUpdateServiceImpl extends RemoteServiceServlet implements Shar
}
return toReturn;
}
-
- /**
- * Return the storage client object
- * @return
- */
- private IClient getStorage(){
-
- String currScope = ScopeProvider.instance.get();
- PortalContext context = PortalContext.getConfiguration();
- ScopeProvider.instance.set(context.getCurrentScope(getThreadLocalRequest()));
- IClient storageClient = new StorageClient(STORAGE_OWNER, AccessType.SHARED, MemoryType.PERSISTENT).getClient();
- ScopeProvider.instance.set(currScope);
- return storageClient;
-
- }
-
}