1 package de.tivsource.page.common.image;
2
3 import java.io.BufferedReader;
4 import java.io.File;
5 import java.io.FileInputStream;
6 import java.io.IOException;
7 import java.io.InputStreamReader;
8 import java.io.Serializable;
9 import java.util.UUID;
10
11 import javax.persistence.Basic;
12 import javax.persistence.MappedSuperclass;
13 import javax.persistence.Transient;
14
15 import org.apache.logging.log4j.LogManager;
16 import org.apache.logging.log4j.Logger;
17 import org.apache.tools.ant.util.FileUtils;
18 import org.hibernate.annotations.Type;
19
20 import de.tivsource.page.common.file.FileActions;
21
22
23
24
25
26
27
28 @MappedSuperclass
29 public class ImageSquare implements Comparable<ImageSquare>, Serializable {
30
31
32
33
34 private static final long serialVersionUID = 7403879687974877269L;
35
36
37
38
39
40 private static final Logger logger = LogManager.getLogger(ImageSquare.class);
41
42
43
44
45 private String orginal;
46
47
48
49
50 private String picture;
51
52
53
54
55 private String thumbnail;
56
57
58
59
60 private String micro;
61
62
63
64
65 @Basic
66 @Type(type = "yes_no")
67 private Boolean standard;
68
69
70
71
72 @Transient
73 private File uploadFile;
74
75 public ImageSquare() {
76 super();
77 }
78
79 public static Logger getLogger() {
80 return logger;
81 }
82
83
84
85
86 public String getOrginal() {
87 return orginal;
88 }
89
90
91
92
93 public void setOrginal(String orginal) {
94 this.orginal = orginal;
95 }
96
97 public String getPicture() {
98 return picture;
99 }
100
101 public void setPicture(String picture) {
102 this.picture = picture;
103 }
104
105 public String getThumbnail() {
106 return thumbnail;
107 }
108
109 public void setThumbnail(String thumbnail) {
110 this.thumbnail = thumbnail;
111 }
112
113 public String getMicro() {
114 return micro;
115 }
116
117 public void setMicro(String micro) {
118 this.micro = micro;
119 }
120
121 public Boolean isStandard() {
122 return standard;
123 }
124
125 public void setStandard(Boolean standard) {
126 this.standard = standard;
127 }
128
129 public FileInputStream getPictureFileInputStream() {
130 try {
131 return new FileInputStream(new File(this.picture));
132 } catch (Exception e) {
133 e.printStackTrace();
134 return null;
135 }
136 }
137
138 public FileInputStream getThumbnailFileInputStream() {
139 try {
140 return new FileInputStream(new File(this.thumbnail));
141 } catch (Exception e) {
142 e.printStackTrace();
143 return null;
144 }
145 }
146
147 public FileInputStream getMicroFileInputStream() {
148 try {
149 return new FileInputStream(new File(this.micro));
150 } catch (Exception e) {
151 e.printStackTrace();
152 return null;
153 }
154 }
155
156
157
158
159 public File getUploadFile() {
160 return uploadFile;
161 }
162
163
164
165
166
167 public void setUploadFile(File uploadFile) {
168 this.uploadFile = uploadFile;
169 }
170
171 public void generate() {
172 String filePath = "/srv/www/tomcat-upload/";
173
174
175 String pictureSaveName = UUID.randomUUID().toString();
176
177 try {
178
179 File fileToCreate = new File(filePath, pictureSaveName + ".png");
180 logger.debug("Absoluter Pfad der neuen Picture-Datei : "
181 + fileToCreate.getAbsolutePath());
182
183
184 if (!fileToCreate.exists()) {
185 FileActions.savePictureFile(this.getUploadFile(), fileToCreate);
186 }
187 }
188 catch (Exception e) {
189 logger.error(e.getMessage());
190 }
191
192 String s = null;
193
194 String file = filePath + pictureSaveName + ".png";
195 String newFile = filePath + pictureSaveName + ".org.png";
196 String thumbFile = filePath + pictureSaveName + ".thumb.png";
197 String microFile = filePath + pictureSaveName + ".micro.png";
198 try {
199
200 Process process = Runtime.getRuntime().exec("montage -background none -geometry 350x350 -quality 100 " + file + " "+ newFile);
201 BufferedReader stdInput = new BufferedReader(new InputStreamReader(process.getInputStream()));
202 BufferedReader stdError = new BufferedReader(new InputStreamReader(process.getErrorStream()));
203
204 while ((s = stdInput.readLine()) != null) {
205 System.out.println(s);
206 }
207
208 while ((s = stdError.readLine()) != null) {
209 System.out.println(s);
210 }
211
212 process = Runtime.getRuntime().exec("montage -background none -geometry 148x148 -quality 100 " + file + " " + thumbFile);
213 stdInput = new BufferedReader(new InputStreamReader(process.getInputStream()));
214 stdError = new BufferedReader(new InputStreamReader(process.getErrorStream()));
215
216 while ((s = stdInput.readLine()) != null) {
217 System.out.println(s);
218 }
219
220 while ((s = stdError.readLine()) != null) {
221 System.out.println(s);
222 }
223
224 process = Runtime.getRuntime().exec("montage -background none -geometry 50x50 -quality 100 " + file + " " + microFile);
225 stdInput = new BufferedReader(new InputStreamReader(process.getInputStream()));
226 stdError = new BufferedReader(new InputStreamReader(process.getErrorStream()));
227
228 while ((s = stdInput.readLine()) != null) {
229 System.out.println(s);
230 }
231
232 while ((s = stdError.readLine()) != null) {
233 System.out.println(s);
234 }
235
236 } catch (IOException e) {
237 System.out.println("exception happened - here's what I know: ");
238 e.printStackTrace();
239 System.exit(-1);
240 }
241
242 this.setOrginal(file);
243 this.setPicture(newFile);
244 this.setThumbnail(thumbFile);
245 this.setMicro(microFile);
246 this.setStandard(true);
247 }
248
249 public void delete() {
250 File microFile = new File(micro);
251 if (microFile.exists()) {
252 FileUtils.delete(microFile);
253 }
254
255 File thumbnailFile = new File(thumbnail);
256 if (thumbnailFile.exists()) {
257 FileUtils.delete(thumbnailFile);
258 }
259
260 File pictureFile = new File(picture);
261 if (pictureFile.exists()) {
262 FileUtils.delete(pictureFile);
263 }
264
265 File uploadFile = new File(orginal);
266 if (uploadFile.exists()) {
267 FileUtils.delete(uploadFile);
268 }
269 }
270
271 public int compareTo(ImageSquare image) {
272 logger.trace("compareTo() aufgerufen.");
273 return image.getMicro().compareTo(this.getMicro()) * -1;
274 }
275
276 }