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.ImageToolUtil;
024 import com.liferay.portal.kernel.log.Log;
025 import com.liferay.portal.kernel.log.LogFactoryUtil;
026 import com.liferay.portal.kernel.util.StringPool;
027 import com.liferay.portal.model.Image;
028 import com.liferay.portal.service.base.ImageLocalServiceBaseImpl;
029 import com.liferay.portal.webserver.WebServerServletTokenUtil;
030
031 import java.io.File;
032 import java.io.IOException;
033 import java.io.InputStream;
034
035 import java.util.Date;
036 import java.util.List;
037
038
043 public class ImageLocalServiceImpl extends ImageLocalServiceBaseImpl {
044
045 @Override
046 public Image deleteImage(long imageId)
047 throws PortalException, SystemException {
048
049 if (imageId <= 0) {
050 return null;
051 }
052
053
062 Image image = getImage(imageId);
063
064 if (image != null) {
065 imagePersistence.remove(image);
066
067 Hook hook = HookFactory.getInstance();
068
069 try {
070 hook.deleteImage(image);
071 }
072 catch (NoSuchImageException nsie) {
073
074
075
076
077 if (_log.isWarnEnabled()) {
078 _log.warn(nsie, nsie);
079 }
080 }
081 }
082
083 return image;
084
085 }
086
087 @Override
088 public Image getCompanyLogo(long imageId) {
089 Image image = getImage(imageId);
090
091 if (image == null) {
092 image = ImageToolUtil.getDefaultCompanyLogo();
093 }
094
095 return image;
096 }
097
098 @Override
099 public Image getImage(long imageId) {
100 if (imageId > 0) {
101 try {
102 return imagePersistence.fetchByPrimaryKey(imageId);
103 }
104 catch (Exception e) {
105 if (_log.isWarnEnabled()) {
106 _log.warn(
107 "Unable to get image " + imageId + ": " +
108 e.getMessage());
109 }
110 }
111 }
112
113 return null;
114 }
115
116 @Override
117 public Image getImageOrDefault(long imageId) {
118 Image image = getImage(imageId);
119
120 if (image == null) {
121 image = ImageToolUtil.getDefaultSpacer();
122 }
123
124 return image;
125 }
126
127 @Override
128 public List<Image> getImages() throws SystemException {
129 return imagePersistence.findAll();
130 }
131
132 @Override
133 public List<Image> getImagesBySize(int size) throws SystemException {
134 return imagePersistence.findByLtSize(size);
135 }
136
137 @Override
138 public Image updateImage(long imageId, byte[] bytes)
139 throws PortalException, SystemException {
140
141 Image image = null;
142
143 try {
144 image = ImageToolUtil.getImage(bytes);
145 }
146 catch (IOException ioe) {
147 throw new SystemException(ioe);
148 }
149
150 return updateImage(
151 imageId, image.getTextObj(), image.getType(), image.getHeight(),
152 image.getWidth(), image.getSize());
153 }
154
155 @Override
156 public Image updateImage(
157 long imageId, byte[] bytes, String type, int height, int width,
158 int size)
159 throws PortalException, SystemException {
160
161 validate(type);
162
163 Image image = imagePersistence.fetchByPrimaryKey(imageId);
164
165 if (image == null) {
166 image = imagePersistence.create(imageId);
167 }
168
169 image.setModifiedDate(new Date());
170 image.setType(type);
171 image.setHeight(height);
172 image.setWidth(width);
173 image.setSize(size);
174
175 Hook hook = HookFactory.getInstance();
176
177 hook.updateImage(image, type, bytes);
178
179 imagePersistence.update(image);
180
181 WebServerServletTokenUtil.resetToken(imageId);
182
183 return image;
184 }
185
186 @Override
187 public Image updateImage(long imageId, File file)
188 throws PortalException, SystemException {
189
190 Image image = null;
191
192 try {
193 image = ImageToolUtil.getImage(file);
194 }
195 catch (IOException ioe) {
196 throw new SystemException(ioe);
197 }
198
199 return updateImage(
200 imageId, image.getTextObj(), image.getType(), image.getHeight(),
201 image.getWidth(), image.getSize());
202 }
203
204 @Override
205 public Image updateImage(long imageId, InputStream is)
206 throws PortalException, SystemException {
207
208 Image image = null;
209
210 try {
211 image = ImageToolUtil.getImage(is);
212 }
213 catch (IOException ioe) {
214 throw new SystemException(ioe);
215 }
216
217 return updateImage(
218 imageId, image.getTextObj(), image.getType(), image.getHeight(),
219 image.getWidth(), image.getSize());
220 }
221
222 @Override
223 public Image updateImage(
224 long imageId, InputStream is, boolean cleanUpStream)
225 throws PortalException, SystemException {
226
227 Image image = null;
228
229 try {
230 image = ImageToolUtil.getImage(is, cleanUpStream);
231 }
232 catch (IOException ioe) {
233 throw new SystemException(ioe);
234 }
235
236 return updateImage(
237 imageId, image.getTextObj(), image.getType(), image.getHeight(),
238 image.getWidth(), image.getSize());
239 }
240
241 protected void validate(String type) throws PortalException {
242 if ((type == null) ||
243 type.contains(StringPool.BACK_SLASH) ||
244 type.contains(StringPool.COLON) ||
245 type.contains(StringPool.GREATER_THAN) ||
246 type.contains(StringPool.LESS_THAN) ||
247 type.contains(StringPool.PERCENT) ||
248 type.contains(StringPool.PERIOD) ||
249 type.contains(StringPool.PIPE) ||
250 type.contains(StringPool.QUESTION) ||
251 type.contains(StringPool.QUOTE) ||
252 type.contains(StringPool.SLASH) ||
253 type.contains(StringPool.SPACE) ||
254 type.contains(StringPool.STAR)) {
255
256 throw new ImageTypeException();
257 }
258 }
259
260 private static Log _log = LogFactoryUtil.getLog(
261 ImageLocalServiceImpl.class);
262
263 }