1
22
23 package com.liferay.portal.service.impl;
24
25 import com.liferay.portal.PortalException;
26 import com.liferay.portal.SystemException;
27 import com.liferay.portal.kernel.image.ImageBag;
28 import com.liferay.portal.kernel.image.ImageProcessorUtil;
29 import com.liferay.portal.kernel.servlet.ImageServletTokenUtil;
30 import com.liferay.portal.model.Image;
31 import com.liferay.portal.model.impl.ImageImpl;
32 import com.liferay.portal.service.ImageLocalServiceUtil;
33 import com.liferay.portal.util.PropsUtil;
34 import com.liferay.util.FileUtil;
35
36 import java.awt.image.RenderedImage;
37
38 import java.io.File;
39 import java.io.FileInputStream;
40 import java.io.IOException;
41 import java.io.InputStream;
42
43 import java.util.Arrays;
44
45 import org.apache.commons.logging.Log;
46 import org.apache.commons.logging.LogFactory;
47
48
55 public class ImageLocalUtil {
56
57 public static void deleteImage(long imageId) throws SystemException {
58 try {
59 ImageLocalServiceUtil.deleteImage(imageId);
60 }
61 catch (PortalException pe) {
62 if (_log.isWarnEnabled()) {
63 _log.warn(
64 "Unable to delete image " + imageId + ": " +
65 pe.getMessage());
66 }
67 }
68 }
69
70 public static Image getCompanyLogo(long imageId) {
71 Image image = getImage(imageId);
72
73 if (image == null) {
74 image = getDefaultCompanyLogo();
75 }
76
77 return image;
78 }
79
80 public static Image getDefaultCompanyLogo() {
81 return _instance._defaultCompanyLogo;
82 }
83
84 public static Image getDefaultSpacer() {
85 return _instance._defaultSpacer;
86 }
87
88 public static Image getDefaultUserFemalePortrait() {
89 return _instance._defaultUserFemalePortrait;
90 }
91
92 public static Image getDefaultUserMalePortrait() {
93 return _instance._defaultUserMalePortrait;
94 }
95
96 public static Image getImage(long imageId) {
97 try {
98 if (imageId > 0) {
99 return ImageLocalServiceUtil.getImage(imageId);
100 }
101 }
102 catch (Exception e) {
103 if (_log.isWarnEnabled()) {
104 _log.warn(
105 "Unable to get image " + imageId + ": " + e.getMessage());
106 }
107 }
108
109 return null;
110 }
111
112 public static Image getImage(byte[] bytes) throws IOException {
113 return _instance._getImage(bytes);
114 }
115
116 public static Image getImage(File file) throws IOException {
117 return _instance._getImage(file);
118 }
119
120 public static Image getImage(InputStream is) throws IOException {
121 return _instance._getImage(is);
122 }
123
124 public static Image getImageOrDefault(long imageId) {
125 Image image = getImage(imageId);
126
127 if (image == null) {
128 image = getDefaultSpacer();
129 }
130
131 return image;
132 }
133
134 public static boolean isNullOrDefaultSpacer(byte[] bytes) {
135 if ((bytes == null) || (bytes.length == 0) ||
136 (Arrays.equals(bytes, getDefaultSpacer().getTextObj()))) {
137
138 return true;
139 }
140 else {
141 return false;
142 }
143 }
144
145 public static Image updateImage(long imageId, byte[] bytes)
146 throws SystemException {
147
148 try {
149 Image image = getImage(bytes);
150
151 return updateImage(
152 imageId, image.getTextObj(), image.getType(), image.getHeight(),
153 image.getWidth(), image.getSize());
154 }
155 catch (IOException ioe) {
156 throw new SystemException(ioe);
157 }
158 }
159
160 public static Image updateImage(long imageId, File file)
161 throws SystemException {
162
163 try {
164 Image image = getImage(file);
165
166 return updateImage(
167 imageId, image.getTextObj(), image.getType(), image.getHeight(),
168 image.getWidth(), image.getSize());
169 }
170 catch (IOException ioe) {
171 throw new SystemException(ioe);
172 }
173 }
174
175 public static Image updateImage(long imageId, InputStream is)
176 throws SystemException {
177
178 try {
179 Image image = getImage(is);
180
181 return updateImage(
182 imageId, image.getTextObj(), image.getType(), image.getHeight(),
183 image.getWidth(), image.getSize());
184 }
185 catch (IOException ioe) {
186 throw new SystemException(ioe);
187 }
188 }
189
190 public static Image updateImage(
191 long imageId, byte[] bytes, String type, int height, int width,
192 int size)
193 throws SystemException {
194
195 Image image = ImageLocalServiceUtil.updateImage(
196 imageId, bytes, type, height, width, size);
197
198 ImageServletTokenUtil.resetToken(imageId);
199
200 return image;
201 }
202
203 private ImageLocalUtil() {
204 ClassLoader classLoader = getClass().getClassLoader();
205
206 try {
207 InputStream is = classLoader.getResourceAsStream(
208 PropsUtil.get(PropsUtil.IMAGE_DEFAULT_SPACER));
209
210 if (is == null) {
211 _log.error("Default spacer is not available");
212 }
213
214 _defaultSpacer = _getImage(is);
215 }
216 catch (IOException ioe) {
217 _log.error(
218 "Unable to configure the default spacer: " + ioe.getMessage());
219 }
220
221 try {
222 InputStream is = classLoader.getResourceAsStream(
223 PropsUtil.get(PropsUtil.IMAGE_DEFAULT_COMPANY_LOGO));
224
225 if (is == null) {
226 _log.error("Default company logo is not available");
227 }
228
229 _defaultCompanyLogo = _getImage(is);
230 }
231 catch (IOException ioe) {
232 _log.error(
233 "Unable to configure the default company logo: " +
234 ioe.getMessage());
235 }
236
237 try {
238 InputStream is = classLoader.getResourceAsStream(
239 PropsUtil.get(PropsUtil.IMAGE_DEFAULT_USER_FEMALE_PORTRAIT));
240
241 if (is == null) {
242 _log.error("Default user female portrait is not available");
243 }
244
245 _defaultUserFemalePortrait = _getImage(is);
246 }
247 catch (IOException ioe) {
248 _log.error(
249 "Unable to configure the default user female portrait: " +
250 ioe.getMessage());
251 }
252
253 try {
254 InputStream is = classLoader.getResourceAsStream(
255 PropsUtil.get(PropsUtil.IMAGE_DEFAULT_USER_MALE_PORTRAIT));
256
257 if (is == null) {
258 _log.error("Default user male portrait is not available");
259 }
260
261 _defaultUserMalePortrait = _getImage(is);
262 }
263 catch (IOException ioe) {
264 _log.error(
265 "Unable to configure the default user male portrait: " +
266 ioe.getMessage());
267 }
268 }
269
270 private Image _getImage(byte[] bytes) throws IOException {
271 return _getImage(null, bytes);
272 }
273
274 private Image _getImage(File file) throws IOException {
275 return _getImage(new FileInputStream(file));
276 }
277
278 private Image _getImage(InputStream is) throws IOException {
279 return _getImage(is, null);
280 }
281
282 private Image _getImage(InputStream is, byte[] bytes) throws IOException {
283 try {
284 if (is != null) {
285 bytes = FileUtil.getBytes(is);
286 }
287
288 ImageBag imageBag = ImageProcessorUtil.read(bytes);
289
290 RenderedImage renderedImage = imageBag.getRenderedImage();
291 String type = imageBag.getType();
292
293 if (renderedImage == null) {
294 throw new IOException(
295 "Unable to retreive rendered image from input stream " +
296 "with type " + type);
297 }
298
299 int height = renderedImage.getHeight();
300 int width = renderedImage.getWidth();
301 int size = bytes.length;
302
303 Image image = new ImageImpl();
304
305 image.setTextObj(bytes);
306 image.setType(type);
307 image.setHeight(height);
308 image.setWidth(width);
309 image.setSize(size);
310
311 return image;
312 }
313 finally {
314 if (is != null) {
315 try {
316 is.close();
317 }
318 catch (IOException ioe) {
319 if (_log.isWarnEnabled()) {
320 _log.warn(ioe);
321 }
322 }
323 }
324 }
325 }
326
327 private static Log _log = LogFactory.getLog(ImageLocalUtil.class);
328
329 private static ImageLocalUtil _instance = new ImageLocalUtil();
330
331 private Image _defaultSpacer;
332 private Image _defaultCompanyLogo;
333 private Image _defaultUserFemalePortrait;
334 private Image _defaultUserMalePortrait;
335
336 }