001
014
015 package com.liferay.portal.service.impl;
016
017 import com.liferay.portal.ImageTypeException;
018 import com.liferay.portal.NoSuchImageException;
019 import com.liferay.portal.image.HookFactory;
020 import com.liferay.portal.kernel.exception.PortalException;
021 import com.liferay.portal.kernel.exception.SystemException;
022 import com.liferay.portal.kernel.image.Hook;
023 import com.liferay.portal.kernel.image.ImageBag;
024 import com.liferay.portal.kernel.image.ImageProcessorUtil;
025 import com.liferay.portal.kernel.log.Log;
026 import com.liferay.portal.kernel.log.LogFactoryUtil;
027 import com.liferay.portal.kernel.util.FileUtil;
028 import com.liferay.portal.kernel.util.PropsKeys;
029 import com.liferay.portal.model.Image;
030 import com.liferay.portal.model.impl.ImageImpl;
031 import com.liferay.portal.service.base.ImageLocalServiceBaseImpl;
032 import com.liferay.portal.util.PropsUtil;
033 import com.liferay.portal.webserver.WebServerServletTokenUtil;
034
035 import java.awt.image.RenderedImage;
036
037 import java.io.File;
038 import java.io.FileInputStream;
039 import java.io.IOException;
040 import java.io.InputStream;
041
042 import java.util.Arrays;
043 import java.util.Date;
044 import java.util.List;
045
046
050 public class ImageLocalServiceImpl extends ImageLocalServiceBaseImpl {
051
052 @Override
053 public void afterPropertiesSet() {
054 ClassLoader classLoader = getClass().getClassLoader();
055
056 try {
057 InputStream is = classLoader.getResourceAsStream(
058 PropsUtil.get(PropsKeys.IMAGE_DEFAULT_SPACER));
059
060 if (is == null) {
061 _log.error("Default spacer is not available");
062 }
063
064 _defaultSpacer = getImage(is);
065 }
066 catch (Exception ioe) {
067 _log.error(
068 "Unable to configure the default spacer: " + ioe.getMessage());
069 }
070
071 try {
072 InputStream is = classLoader.getResourceAsStream(
073 PropsUtil.get(PropsKeys.IMAGE_DEFAULT_COMPANY_LOGO));
074
075 if (is == null) {
076 _log.error("Default company logo is not available");
077 }
078
079 _defaultCompanyLogo = getImage(is);
080 }
081 catch (Exception ioe) {
082 _log.error(
083 "Unable to configure the default company logo: " +
084 ioe.getMessage());
085 }
086
087 try {
088 InputStream is = classLoader.getResourceAsStream(
089 PropsUtil.get(PropsKeys.IMAGE_DEFAULT_ORGANIZATION_LOGO));
090
091 if (is == null) {
092 _log.error("Default organization logo is not available");
093 }
094
095 _defaultOrganizationLogo = getImage(is);
096 }
097 catch (Exception ioe) {
098 _log.error(
099 "Unable to configure the default organization logo: " +
100 ioe.getMessage());
101 }
102
103 try {
104 InputStream is = classLoader.getResourceAsStream(
105 PropsUtil.get(PropsKeys.IMAGE_DEFAULT_USER_FEMALE_PORTRAIT));
106
107 if (is == null) {
108 _log.error("Default user female portrait is not available");
109 }
110
111 _defaultUserFemalePortrait = getImage(is);
112 }
113 catch (Exception ioe) {
114 _log.error(
115 "Unable to configure the default user female portrait: " +
116 ioe.getMessage());
117 }
118
119 try {
120 InputStream is = classLoader.getResourceAsStream(
121 PropsUtil.get(PropsKeys.IMAGE_DEFAULT_USER_MALE_PORTRAIT));
122
123 if (is == null) {
124 _log.error("Default user male portrait is not available");
125 }
126
127 _defaultUserMalePortrait = getImage(is);
128 }
129 catch (Exception ioe) {
130 _log.error(
131 "Unable to configure the default user male portrait: " +
132 ioe.getMessage());
133 }
134 }
135
136 @Override
137 public void deleteImage(long imageId)
138 throws PortalException, SystemException {
139
140 if (imageId <= 0) {
141 return;
142 }
143
144
153 try {
154 Image image = getImage(imageId);
155
156 imagePersistence.remove(imageId);
157
158 Hook hook = HookFactory.getInstance();
159
160 hook.deleteImage(image);
161 }
162 catch (NoSuchImageException nsie) {
163 }
164
165 }
166
167 public Image getCompanyLogo(long imageId) {
168 Image image = getImage(imageId);
169
170 if (image == null) {
171 image = getDefaultCompanyLogo();
172 }
173
174 return image;
175 }
176
177 public Image getDefaultCompanyLogo() {
178 return _defaultCompanyLogo;
179 }
180
181 public Image getDefaultOrganizationLogo() {
182 return _defaultOrganizationLogo;
183 }
184
185 public Image getDefaultSpacer() {
186 return _defaultSpacer;
187 }
188
189 public Image getDefaultUserFemalePortrait() {
190 return _defaultUserFemalePortrait;
191 }
192
193 public Image getDefaultUserMalePortrait() {
194 return _defaultUserMalePortrait;
195 }
196
197 @Override
198 public Image getImage(long imageId) {
199 try {
200 if (imageId > 0) {
201 return imagePersistence.findByPrimaryKey(imageId);
202 }
203 }
204 catch (Exception e) {
205 if (_log.isWarnEnabled()) {
206 _log.warn(
207 "Unable to get image " + imageId + ": " + e.getMessage());
208 }
209 }
210
211 return null;
212 }
213
214 public Image getImage(byte[] bytes)
215 throws PortalException, SystemException {
216
217 return getImage(null, bytes);
218 }
219
220 public Image getImage(File file) throws PortalException, SystemException {
221 try {
222 return getImage(new FileInputStream(file));
223 }
224 catch (IOException ioe) {
225 throw new SystemException(ioe);
226 }
227 }
228
229 public Image getImage(InputStream is)
230 throws PortalException, SystemException {
231
232 return getImage(is, null);
233 }
234
235 public Image getImage(InputStream is, boolean cleanUpStream)
236 throws PortalException, SystemException {
237
238 return getImage(is, null, cleanUpStream);
239 }
240
241 public Image getImageOrDefault(long imageId) {
242 Image image = getImage(imageId);
243
244 if (image == null) {
245 image = getDefaultSpacer();
246 }
247
248 return image;
249 }
250
251 public List<Image> getImages() throws SystemException {
252 return imagePersistence.findAll();
253 }
254
255 @Override
256 public List<Image> getImages(int start, int end) throws SystemException {
257 return imagePersistence.findAll(start, end);
258 }
259
260 public List<Image> getImagesBySize(int size) throws SystemException {
261 return imagePersistence.findByLtSize(size);
262 }
263
264 public boolean isNullOrDefaultSpacer(byte[] bytes) {
265 if ((bytes == null) || (bytes.length == 0) ||
266 (Arrays.equals(bytes, getDefaultSpacer().getTextObj()))) {
267
268 return true;
269 }
270 else {
271 return false;
272 }
273 }
274
275 public Image updateImage(long imageId, byte[] bytes)
276 throws PortalException, SystemException {
277
278 Image image = getImage(bytes);
279
280 return updateImage(
281 imageId, image.getTextObj(), image.getType(), image.getHeight(),
282 image.getWidth(), image.getSize());
283 }
284
285 public Image updateImage(long imageId, File file)
286 throws PortalException, SystemException {
287
288 Image image = getImage(file);
289
290 return updateImage(
291 imageId, image.getTextObj(), image.getType(), image.getHeight(),
292 image.getWidth(), image.getSize());
293 }
294
295 public Image updateImage(long imageId, InputStream is)
296 throws PortalException, SystemException {
297
298 Image image = getImage(is);
299
300 return updateImage(
301 imageId, image.getTextObj(), image.getType(), image.getHeight(),
302 image.getWidth(), image.getSize());
303 }
304
305 public Image updateImage(
306 long imageId, InputStream is, boolean cleanUpStream)
307 throws PortalException, SystemException {
308
309 Image image = getImage(is, cleanUpStream);
310
311 return updateImage(
312 imageId, image.getTextObj(), image.getType(), image.getHeight(),
313 image.getWidth(), image.getSize());
314 }
315
316 public Image updateImage(
317 long imageId, byte[] bytes, String type, int height, int width,
318 int size)
319 throws PortalException, SystemException {
320
321 Image image = imagePersistence.fetchByPrimaryKey(imageId);
322
323 if (image == null) {
324 image = imagePersistence.create(imageId);
325 }
326
327 image.setModifiedDate(new Date());
328 image.setType(type);
329 image.setHeight(height);
330 image.setWidth(width);
331 image.setSize(size);
332
333 Hook hook = HookFactory.getInstance();
334
335 hook.updateImage(image, type, bytes);
336
337 imagePersistence.update(image, false);
338
339 WebServerServletTokenUtil.resetToken(imageId);
340
341 return image;
342 }
343
344 protected Image getImage(InputStream is, byte[] bytes)
345 throws PortalException, SystemException {
346
347 return getImage(is, bytes, true);
348 }
349
350 protected Image getImage(
351 InputStream is, byte[] bytes, boolean cleanUpStream)
352 throws PortalException, SystemException {
353
354 try {
355 if (is != null) {
356 bytes = FileUtil.getBytes(is, -1, cleanUpStream);
357 }
358
359 if (bytes == null) {
360 return null;
361 }
362
363 ImageBag imageBag = ImageProcessorUtil.read(bytes);
364
365 RenderedImage renderedImage = imageBag.getRenderedImage();
366 String type = imageBag.getType();
367
368 if (renderedImage == null) {
369 throw new ImageTypeException();
370 }
371
372 int height = renderedImage.getHeight();
373 int width = renderedImage.getWidth();
374 int size = bytes.length;
375
376 Image image = new ImageImpl();
377
378 image.setTextObj(bytes);
379 image.setType(type);
380 image.setHeight(height);
381 image.setWidth(width);
382 image.setSize(size);
383
384 return image;
385 }
386 catch (IOException ioe) {
387 throw new SystemException(ioe);
388 }
389 }
390
391 private static Log _log = LogFactoryUtil.getLog(
392 ImageLocalServiceImpl.class);
393
394 private Image _defaultSpacer;
395 private Image _defaultCompanyLogo;
396 private Image _defaultOrganizationLogo;
397 private Image _defaultUserFemalePortrait;
398 private Image _defaultUserMalePortrait;
399
400 }