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