the preview image of a link preview saved in cassandra is the one retrieved from the ftp storage
git-svn-id: https://svn.research-infrastructures.eu/d4science/gcube/trunk/portlets/user/share-updates@134212 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
parent
d0222dc355
commit
7c01599325
|
@ -1,6 +1,7 @@
|
||||||
package org.gcube.portlets.user.shareupdates.server;
|
package org.gcube.portlets.user.shareupdates.server;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
import java.io.FileOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.HttpURLConnection;
|
import java.net.HttpURLConnection;
|
||||||
import java.net.MalformedURLException;
|
import java.net.MalformedURLException;
|
||||||
|
@ -18,6 +19,7 @@ import java.util.UUID;
|
||||||
|
|
||||||
import javax.net.ssl.HttpsURLConnection;
|
import javax.net.ssl.HttpsURLConnection;
|
||||||
|
|
||||||
|
import org.apache.commons.fileupload.util.Streams;
|
||||||
import org.apache.commons.validator.routines.UrlValidator;
|
import org.apache.commons.validator.routines.UrlValidator;
|
||||||
import org.gcube.applicationsupportlayer.social.ApplicationNotificationsManager;
|
import org.gcube.applicationsupportlayer.social.ApplicationNotificationsManager;
|
||||||
import org.gcube.applicationsupportlayer.social.NotificationsManager;
|
import org.gcube.applicationsupportlayer.social.NotificationsManager;
|
||||||
|
@ -82,15 +84,14 @@ public class ShareUpdateServiceImpl extends RemoteServiceServlet implements Shar
|
||||||
private static final String STORAGE_OWNER = "gCubeSocialFramework";
|
private static final String STORAGE_OWNER = "gCubeSocialFramework";
|
||||||
public static final String UPLOAD_DIR = "/social-framework-uploads";
|
public static final String UPLOAD_DIR = "/social-framework-uploads";
|
||||||
private static final String NEWS_FEED_PORTLET_CLASSNAME = "org.gcube.portlets.user.newsfeed.server.NewsServiceImpl";
|
private static final String NEWS_FEED_PORTLET_CLASSNAME = "org.gcube.portlets.user.newsfeed.server.NewsServiceImpl";
|
||||||
|
private static String UPLOAD_LOCATION_LOCAL = System.getProperty("java.io.tmpdir");
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
private static Logger _log = LoggerFactory.getLogger(ShareUpdateServiceImpl.class);
|
private static Logger _log = LoggerFactory.getLogger(ShareUpdateServiceImpl.class);
|
||||||
/**
|
|
||||||
* The Cassandra store interface
|
|
||||||
*/
|
|
||||||
private DatabookStore store;
|
private DatabookStore store;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* connect to cassandra at startup
|
* connect to cassandra at startup
|
||||||
*/
|
*/
|
||||||
|
@ -116,6 +117,7 @@ public class ShareUpdateServiceImpl extends RemoteServiceServlet implements Shar
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Share post that could contain a link preview.
|
* Share post that could contain a link preview.
|
||||||
*/
|
*/
|
||||||
|
@ -145,7 +147,7 @@ public class ShareUpdateServiceImpl extends RemoteServiceServlet implements Shar
|
||||||
String email = currUser.getEmail();
|
String email = currUser.getEmail();
|
||||||
String fullName = currUser.getFullname();
|
String fullName = currUser.getFullname();
|
||||||
String thumbnailAvatarURL = currUser.getUserAvatarId();
|
String thumbnailAvatarURL = currUser.getUserAvatarId();
|
||||||
|
|
||||||
// get data from the preview of the link
|
// get data from the preview of the link
|
||||||
String linkTitle = preview.getTitle();
|
String linkTitle = preview.getTitle();
|
||||||
String linkDesc = preview.getDescription();
|
String linkDesc = preview.getDescription();
|
||||||
|
@ -153,6 +155,8 @@ public class ShareUpdateServiceImpl extends RemoteServiceServlet implements Shar
|
||||||
String url = preview.getUrl();
|
String url = preview.getUrl();
|
||||||
if (urlThumbnail == null)
|
if (urlThumbnail == null)
|
||||||
urlThumbnail = "null";
|
urlThumbnail = "null";
|
||||||
|
else
|
||||||
|
urlThumbnail = saveThumbnailOnFTPAndGetUrl(urlThumbnail);
|
||||||
|
|
||||||
Date feedDate = new Date();
|
Date feedDate = new Date();
|
||||||
|
|
||||||
|
@ -224,6 +228,77 @@ public class ShareUpdateServiceImpl extends RemoteServiceServlet implements Shar
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if a resource at a given url exists or not. If it exists, create a file in the temp dir
|
||||||
|
* of the tomcat
|
||||||
|
* @param urlThumbnail
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
private File storeAndGetFile(String urlThumbnail){
|
||||||
|
try {
|
||||||
|
HttpURLConnection.setFollowRedirects(true);
|
||||||
|
HttpURLConnection con =
|
||||||
|
(HttpURLConnection) new URL(urlThumbnail).openConnection();
|
||||||
|
con.setRequestMethod("HEAD"); // body is not needed yet
|
||||||
|
if(con.getResponseCode() == HttpURLConnection.HTTP_OK){
|
||||||
|
|
||||||
|
//generate the random dir
|
||||||
|
File theRandomDir = new File(UPLOAD_LOCATION_LOCAL + File.separator + UUID.randomUUID().toString());
|
||||||
|
theRandomDir.mkdir();
|
||||||
|
_log.debug("Created temp upload directory in: " + theRandomDir);
|
||||||
|
|
||||||
|
// generate a random file name and create it under the randomDir
|
||||||
|
File file = new File(theRandomDir, UUID.randomUUID().toString().substring(0, 10));
|
||||||
|
|
||||||
|
//Get the inputstream and copy there
|
||||||
|
URL url = new URL(urlThumbnail);
|
||||||
|
Streams.copy(url.openStream(), new FileOutputStream(file), true);
|
||||||
|
_log.debug("File is at " + file.getAbsolutePath());
|
||||||
|
return file;
|
||||||
|
}else
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
catch (Exception e) {
|
||||||
|
_log.error("The resource at url " + urlThumbnail + " doesn't exist");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* When a link preview needs to be saved, the method save the thumbnail on the storage no longer using the external url
|
||||||
|
* @param urlThumbnail
|
||||||
|
* @return the url of the thumbnail saved on the storage or null in case of error
|
||||||
|
*/
|
||||||
|
private String saveThumbnailOnFTPAndGetUrl(String urlThumbnail) {
|
||||||
|
File localFile;
|
||||||
|
if((localFile = storeAndGetFile(urlThumbnail)) != null){
|
||||||
|
String mimeType = null;
|
||||||
|
if (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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}else
|
||||||
|
_log.warn("the file at url " + urlThumbnail + " doesn't exist");
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Share a post with at least one attachment.
|
* Share a post with at least one attachment.
|
||||||
*/
|
*/
|
||||||
|
@ -585,9 +660,9 @@ public class ShareUpdateServiceImpl extends RemoteServiceServlet implements Shar
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public LinkPreview checkUploadedFile(String fileName, String fileabsolutePathOnServer) {
|
public LinkPreview checkUploadedFile(String fileName, String fileabsolutePathOnServer) {
|
||||||
|
|
||||||
PortalContext context = PortalContext.getConfiguration();
|
PortalContext context = PortalContext.getConfiguration();
|
||||||
|
|
||||||
LinkPreview toReturn = null;
|
LinkPreview toReturn = null;
|
||||||
|
|
||||||
String randomUploadFolderName = UUID.randomUUID().toString();
|
String randomUploadFolderName = UUID.randomUUID().toString();
|
||||||
|
@ -606,7 +681,7 @@ public class ShareUpdateServiceImpl extends RemoteServiceServlet implements Shar
|
||||||
//smpURI = storageClient.getUrl(true).RFile(remoteFilePath); //"http://ciccio.com";
|
//smpURI = storageClient.getUrl(true).RFile(remoteFilePath); //"http://ciccio.com";
|
||||||
smpURI = storageClient.getHttpUrl(true).RFile(remoteFilePath);
|
smpURI = storageClient.getHttpUrl(true).RFile(remoteFilePath);
|
||||||
|
|
||||||
//The storage uploader Thread starts here asyncronouslyù
|
//The storage uploader Thread starts here asyncronously
|
||||||
try {
|
try {
|
||||||
mimeType = FilePreviewer.getMimeType(new File(fileabsolutePathOnServer), fileName);
|
mimeType = FilePreviewer.getMimeType(new File(fileabsolutePathOnServer), fileName);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
|
|
Loading…
Reference in New Issue