1 | /** |
---|
2 | * MailArchiver is an application that provides services for storing and managing e-mail messages through a Web Services SOAP interface. |
---|
3 | * Copyright (C) 2012 Marcio Andre Scholl Levien and Fernando Alberto Reuter Wendt and Jose Ronaldo Nogueira Fonseca Junior |
---|
4 | * |
---|
5 | * This program is free software: you can redistribute it and/or modify |
---|
6 | * it under the terms of the GNU Affero General Public License as |
---|
7 | * published by the Free Software Foundation, either version 3 of the |
---|
8 | * License, or (at your option) any later version. |
---|
9 | * |
---|
10 | * This program is distributed in the hope that it will be useful, |
---|
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
13 | * GNU Affero General Public License for more details. |
---|
14 | * |
---|
15 | * You should have received a copy of the GNU Affero General Public License |
---|
16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
---|
17 | */ |
---|
18 | |
---|
19 | /******************************************************************************\ |
---|
20 | * |
---|
21 | * This product was developed by |
---|
22 | * |
---|
23 | * SERVIÃO FEDERAL DE PROCESSAMENTO DE DADOS (SERPRO), |
---|
24 | * |
---|
25 | * a government company established under Brazilian law (5.615/70), |
---|
26 | * at Department of Development of Porto Alegre. |
---|
27 | * |
---|
28 | \******************************************************************************/ |
---|
29 | |
---|
30 | package serpro.mailarchiver.domain.metaarchive; |
---|
31 | |
---|
32 | import java.io.IOException; |
---|
33 | import java.io.InputStream; |
---|
34 | import java.nio.file.Files; |
---|
35 | import java.nio.file.Path; |
---|
36 | |
---|
37 | import javax.jdo.JDOHelper; |
---|
38 | import javax.jdo.annotations.NotPersistent; |
---|
39 | import javax.jdo.annotations.PersistenceCapable; |
---|
40 | |
---|
41 | import net.coobird.thumbnailator.Thumbnails; |
---|
42 | import net.coobird.thumbnailator.Thumbnails.Builder; |
---|
43 | |
---|
44 | import serpro.mailarchiver.util.Logger; |
---|
45 | |
---|
46 | @PersistenceCapable |
---|
47 | public class BinaryBody |
---|
48 | extends SingleBody |
---|
49 | { |
---|
50 | @NotPersistent |
---|
51 | private static final Logger log = Logger.getLocalLogger(); |
---|
52 | |
---|
53 | @Override |
---|
54 | final String toString(String pad) { |
---|
55 | return String.format( |
---|
56 | "BinaryBody%n" |
---|
57 | + "%1$sjdoState: %2$s%n" |
---|
58 | + "%1$soid: %3$s%n" |
---|
59 | + "%1$shash: %4$x%n" |
---|
60 | + "%1$soffset: %5$d%n" |
---|
61 | + "%1$slength: %6$d%n" |
---|
62 | + "%1$ssize: %7$d" |
---|
63 | , pad |
---|
64 | , JDOHelper.getObjectState(this) |
---|
65 | , getOid() |
---|
66 | , hashCode() |
---|
67 | , getOffset() |
---|
68 | , getLength() |
---|
69 | , getSize()); |
---|
70 | } |
---|
71 | |
---|
72 | //-------------------------------------------------------------------------- |
---|
73 | public Path getThumbnailPath(int size) { |
---|
74 | return userAppConfig.SERVER.getArchiveDir().resolve("temp").resolve(getOid()).resolve("thumb").resolve(String.valueOf(size)).resolve(getFileName()); |
---|
75 | } |
---|
76 | |
---|
77 | public void publishThumbnail(int size) throws IOException { |
---|
78 | |
---|
79 | if(Files.exists(getThumbnailPath(size))) { |
---|
80 | //already published |
---|
81 | return; |
---|
82 | } |
---|
83 | |
---|
84 | log.info("Publishing: %s/thumb/%d/%s", getOid(), size, getFileName()); |
---|
85 | |
---|
86 | if(Files.notExists(getThumbnailPath(size).getParent())) { |
---|
87 | Files.createDirectories(getThumbnailPath(size).getParent()); |
---|
88 | } |
---|
89 | |
---|
90 | InputStream is = getDecoderInputStream(); |
---|
91 | Builder<? extends InputStream> thumbBuilder = Thumbnails |
---|
92 | .of(is) |
---|
93 | .size(size, size) |
---|
94 | .keepAspectRatio(true) |
---|
95 | .allowOverwrite(true); |
---|
96 | |
---|
97 | int n = getFileName().lastIndexOf('.'); |
---|
98 | if(n >= 0) { |
---|
99 | String format = getFileName().substring(n + 1).toLowerCase(); |
---|
100 | if(!format.isEmpty()) { |
---|
101 | thumbBuilder.outputFormat(format); |
---|
102 | } |
---|
103 | } |
---|
104 | |
---|
105 | thumbBuilder.toFile(getThumbnailPath(size).toString()); |
---|
106 | |
---|
107 | is.close(); |
---|
108 | } |
---|
109 | } |
---|