/** * MailArchiver is an application that provides services for storing and managing e-mail messages through a Web Services SOAP interface. * Copyright (C) 2012 Marcio Andre Scholl Levien and Fernando Alberto Reuter Wendt and Jose Ronaldo Nogueira Fonseca Junior * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as * published by the Free Software Foundation, either version 3 of the * License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Affero General Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ /******************************************************************************\ * * This product was developed by * * SERVIÇO FEDERAL DE PROCESSAMENTO DE DADOS (SERPRO), * * a government company established under Brazilian law (5.615/70), * at Department of Development of Porto Alegre. * \******************************************************************************/ package serpro.mailarchiver.domain.metaarchive; import java.io.IOException; import java.io.InputStream; import java.nio.file.Files; import java.nio.file.Path; import javax.jdo.JDOHelper; import javax.jdo.annotations.NotPersistent; import javax.jdo.annotations.PersistenceCapable; import net.coobird.thumbnailator.Thumbnails; import net.coobird.thumbnailator.Thumbnails.Builder; import serpro.mailarchiver.util.Logger; @PersistenceCapable public class BinaryBody extends SingleBody { @NotPersistent private static final Logger log = Logger.getLocalLogger(); @Override final String toString(String pad) { return String.format( "BinaryBody%n" + "%1$sjdoState: %2$s%n" + "%1$soid: %3$s%n" + "%1$shash: %4$x%n" + "%1$soffset: %5$d%n" + "%1$slength: %6$d%n" + "%1$ssize: %7$d" , pad , JDOHelper.getObjectState(this) , getOid() , hashCode() , getOffset() , getLength() , getSize()); } //-------------------------------------------------------------------------- public Path getThumbnailPath(int size) { return userAppConfig.SERVER.getArchiveDir().resolve("temp").resolve(getOid()).resolve("thumb").resolve(String.valueOf(size)).resolve(getFileName()); } public void publishThumbnail(int size) throws IOException { if(Files.exists(getThumbnailPath(size))) { //already published return; } log.info("Publishing: %s/thumb/%d/%s", getOid(), size, getFileName()); if(Files.notExists(getThumbnailPath(size).getParent())) { Files.createDirectories(getThumbnailPath(size).getParent()); } InputStream is = getDecoderInputStream(); Builder thumbBuilder = Thumbnails .of(is) .size(size, size) .keepAspectRatio(true) .allowOverwrite(true); int n = getFileName().lastIndexOf('.'); if(n >= 0) { String format = getFileName().substring(n + 1).toLowerCase(); if(!format.isEmpty()) { thumbBuilder.outputFormat(format); } } thumbBuilder.toFile(getThumbnailPath(size).toString()); is.close(); } }