1
2
3
4
5
6
7
8 package com.buckosoft.PicMan.web;
9
10 import java.awt.Dimension;
11 import java.awt.Graphics2D;
12 import java.awt.Image;
13 import java.awt.image.BufferedImage;
14 import java.io.IOException;
15 import java.io.PrintWriter;
16 import java.util.Iterator;
17
18 import javax.imageio.ImageIO;
19 import javax.imageio.ImageWriter;
20 import javax.imageio.stream.ImageOutputStream;
21 import javax.servlet.ServletException;
22 import javax.servlet.ServletOutputStream;
23 import javax.servlet.http.HttpServletRequest;
24 import javax.servlet.http.HttpServletResponse;
25
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28 import org.springframework.web.servlet.ModelAndView;
29
30 import com.buckosoft.BSAccount.web.BSAccountPageController;
31 import com.buckosoft.BSAccount.web.BSAccountUserWebSession;
32 import com.buckosoft.PicMan.business.PicManFacade;
33 import com.buckosoft.PicMan.db.DatabaseFacade;
34 import com.buckosoft.PicMan.domain.Mosaic;
35 import com.buckosoft.PicMan.domain.Pic;
36 import com.buckosoft.PicMan.domain.Thumbnail;
37 import com.buckosoft.PicMan.domain.User;
38
39
40
41
42
43
44
45 public class GetThumbNailController extends BSAccountPageController {
46
47 private static boolean DEBUG = false;
48 protected final Log logger = LogFactory.getLog(getClass());
49
50 private DatabaseFacade dbf;
51 private PicManFacade pmf;
52
53
54
55
56 public void setDatabase(DatabaseFacade dbf) { this.dbf = dbf; }
57
58
59
60
61 public void setPicMan(PicManFacade pmf) { this.pmf = pmf; }
62
63
64
65
66
67 public void setDEBUG(boolean debugFlag) {
68 DEBUG = debugFlag;
69 }
70
71
72
73
74
75
76 public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
77 throws ServletException, IOException {
78 String s;
79 Pic pic = null;
80 String label = null;
81
82 Thumbnail tn = null;
83
84 BSAccountUserWebSession userWebSession = this.bsAccountMan.getUserWebSession(request);
85 User user;
86
87
88
89
90
91
92 user = (User)userWebSession.getUser();
93 s = request.getParameter("label");
94 if (s != null) {
95 label = s;
96 }
97
98 s = request.getParameter("moswork");
99 if (s != null) {
100 outMosaicPic(request, response);
101 return(null);
102 }
103
104 s = request.getParameter("posterMid");
105 if (s != null) {
106 outPosterMid(request, response);
107 return(null);
108 }
109
110 s = request.getParameter("importPic");
111 if (s != null) {
112 pic = new Pic(true, s);
113 }
114
115 if (pic == null) {
116 s = request.getParameter("pic");
117 if (s == null) {
118 PrintWriter out = response.getWriter();
119 out.print("No pic in request");
120 return(null);
121 }
122 }
123 if (pic == null)
124 pic = dbf.getPic(s);
125 if (DEBUG) {
126 if (pic != null)
127 logger.info("Getting thumb '" + pic.getName() + "'");
128 else
129 logger.info("Getting Xthumb for '" + s + "'");
130 }
131 int height = dbf.getSystem().getThumbHeight();
132 s = request.getParameter("mosthumb");
133 if (s != null)
134 height = dbf.getSystem().getMosaicThumbHeight();
135 s = request.getParameter("home");
136 if (s != null) {
137 int width = pic.getWidth();
138 if (width == 0) {
139 Dimension d = pmf.determinePicSize(pic);
140 if (d.height == -1)
141 width = height;
142 else {
143 logger.warn("Updating size for pic " + pic.getName());
144 pic.setHeight(d.height);
145 pic.setWidth(d.width);
146 pmf.getDB().updatePic(pic);
147 }
148 } else {
149 height = user.getHomePageThumbWidth() * pic.getHeight() / width;
150 }
151 }
152 s = request.getParameter("mos");
153 if (s != null)
154 tn = pmf.getTestMosaicThumbNail(pic, height, Integer.parseInt(s));
155 else
156 tn = pmf.getThumbNail(pic, height, label);
157 response.setContentType("image/jpeg");
158 ServletOutputStream sos = response.getOutputStream();
159 ImageOutputStream ios = ImageIO.createImageOutputStream(sos);
160 Iterator<ImageWriter> writers = ImageIO.getImageWritersByFormatName("jpg");
161 ImageWriter writer = (ImageWriter)writers.next();
162 writer.setOutput(ios);
163 writer.write(tn.getImage());
164 return(null);
165 }
166
167 private void outMosaicPic(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
168 BSAccountUserWebSession userWebSession = this.bsAccountMan.getUserWebSession(request);
169 User user = (User)userWebSession.getUser();
170 response.setContentType("image/jpeg");
171 response.addHeader("Cache-Control", "max-age=0");
172 response.addHeader("Cache-Control", "no-cache");
173 response.addHeader("expires", "0");
174 response.addHeader("Expires", "Tue, 01 Jan 1980 1:00:00 GMT");
175 response.addHeader("Pragma", "no-cache");
176 ServletOutputStream sos = response.getOutputStream();
177 ImageOutputStream ios = ImageIO.createImageOutputStream(sos);
178 Iterator<ImageWriter> writers = ImageIO.getImageWritersByFormatName("jpg");
179 ImageWriter writer = (ImageWriter)writers.next();
180 writer.setOutput(ios);
181 BufferedImage small;
182 if (pmf.getMosaicMan() == null || pmf.getMosaicMan().getMosaicEngine() == null) {
183 small = pmf.getPicReader().getXThumb(dbf.getSystem().getThumbHeight()).getImage();
184 } else {
185 BufferedImage bi;
186 bi = pmf.getMosaicMan().getMosaicEngine().getImage();
187 int width = bi.getWidth();
188 int height = bi.getHeight();
189 int picHeight = user.getMosaicPreviewHeight();
190
191 double dW = ((double)picHeight/(double)height) * width;
192 int newW = (int)dW;
193 int newH = picHeight;
194
195 small = new BufferedImage(newW, newH, BufferedImage.TYPE_INT_BGR);
196 Graphics2D g;
197 g = small.createGraphics();
198 g.drawImage(bi.getScaledInstance(-1, newH, Image.SCALE_SMOOTH), null, null);
199 }
200 writer.write(small);
201 }
202
203 private void outPosterMid(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
204
205
206 response.setContentType("image/jpeg");
207 response.addHeader("Cache-Control", "max-age=0");
208 response.addHeader("Cache-Control", "no-cache");
209 response.addHeader("expires", "0");
210 response.addHeader("Expires", "Tue, 01 Jan 1980 1:00:00 GMT");
211 response.addHeader("Pragma", "no-cache");
212 ServletOutputStream sos = response.getOutputStream();
213 ImageOutputStream ios = ImageIO.createImageOutputStream(sos);
214 Iterator<ImageWriter> writers = ImageIO.getImageWritersByFormatName("jpg");
215 ImageWriter writer = (ImageWriter)writers.next();
216 writer.setOutput(ios);
217 BufferedImage small;
218 String s = request.getParameter("posterMid");
219 Integer mid = Integer.parseInt(s);
220 Mosaic mosaic = pmf.getDB().getMosaic(mid);
221 String fname = mosaic.getOutPic();
222 BufferedImage bi = pmf.getPicReader().readPic(fname);
223 if (bi == null) {
224 small = pmf.getPicReader().getXThumb(dbf.getSystem().getThumbHeight()).getImage();
225 } else {
226 int width = bi.getWidth();
227 int height = bi.getHeight();
228 int picHeight = pmf.getDB().getThumbHeight();
229
230 double dW = ((double)picHeight/(double)height) * width;
231 int newW = (int)dW;
232 int newH = picHeight;
233
234 small = new BufferedImage(newW, newH, BufferedImage.TYPE_INT_BGR);
235 Graphics2D g;
236 g = small.createGraphics();
237 g.drawImage(bi.getScaledInstance(-1, newH, Image.SCALE_SMOOTH), null, null);
238 }
239 writer.write(small);
240 }
241
242 }