share-updates/src/main/java/org/gcube/portlets/user/shareupdates/server/FilePreviewer.java

130 lines
4.0 KiB
Java

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.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.util.ArrayList;
import javax.imageio.ImageIO;
import org.apache.pdfbox.pdmodel.PDDocument;
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.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.sun.pdfview.PDFFile;
import com.sun.pdfview.PDFPage;
import com.sun.pdfview.PDFParseException;
/**
*
* @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 String PDF_DEFAULT_IMAGE = "default_pdf.png";
private static FTPManager getFTPManager() {
return FTPManager.getInstance();
}
/**
*
* @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 getPdfPreview(String fileName, String path2Pdf, String httpUrl) throws Exception {
ArrayList<String> imagesUrl = new ArrayList<String>();
//description
String desc = null;
try {
desc = getPDFDescription(path2Pdf);
}
catch (Exception ex) {
_log.warn("PDF Parse exception, returning no description");
desc = "";
}
//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());
PDFFile pdf = null;
try {
pdf = new PDFFile(buf);
} catch (PDFParseException ex) {
raf.close();
_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);
}
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, rect, null, true, true);
Graphics2D bufImageGraphics = bufferedImage.createGraphics();
bufImageGraphics.drawImage(image, 0, 0, scaledWidth, scaledHeight, null);
//thumbnail previes 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 = 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);
}
else
throw new IOException("Could not process pdf file");
}
/**
*
* @param path2File
* @return
* @throws Exception
*/
private static String getPDFDescription(String path2File) throws Exception {
PDDocument doc = PDDocument.load(path2File);
PDFTextStripper stripper = new PDFTextStripper();
//only first page text
stripper.setStartPage(1);
stripper.setEndPage(1);
String text = stripper.getText(doc);
String toReturn = (text.length() > 300) ? text.substring(0, 295) + " ... " : text;
doc.close();
return toReturn;
}
}