001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.service.impl;
016    
017    import com.liferay.portal.exception.ImageTypeException;
018    import com.liferay.portal.exception.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.kernel.webserver.WebServerServletTokenUtil;
028    import com.liferay.portal.model.Image;
029    import com.liferay.portal.service.base.ImageLocalServiceBaseImpl;
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    /**
039     * @author Brian Wing Shun Chan
040     * @author Julio Camarero
041     * @author Shuyang Zhou
042     */
043    public class ImageLocalServiceImpl extends ImageLocalServiceBaseImpl {
044    
045            @Override
046            public Image deleteImage(long imageId) throws PortalException {
047                    if (imageId <= 0) {
048                            return null;
049                    }
050    
051                    /*
052                    if (PropsValues.IMAGE_HOOK_IMPL.equals(
053                                    DatabaseHook.class.getName()) &&
054                            (imagePersistence.getListeners().length == 0)) {
055    
056                            runSQL("delete from Image where imageId = " + imageId);
057    
058                            imagePersistence.clearCache();
059                    }
060                    else {
061                    */
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                                    // DLHook throws NoSuchImageException if the file no longer
075                                    // exists. See LPS-30430. This exception can be ignored.
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() {
129                    return imagePersistence.findAll();
130            }
131    
132            @Override
133            public List<Image> getImagesBySize(int size) {
134                    return imagePersistence.findByLtSize(size);
135            }
136    
137            @Override
138            public Image moveImage(long imageId, byte[] bytes) throws PortalException {
139                    Image image = updateImage(counterLocalService.increment(), bytes);
140    
141                    if (imageId > 0) {
142                            deleteImage(imageId);
143                    }
144    
145                    return image;
146            }
147    
148            @Override
149            public Image updateImage(long imageId, byte[] bytes)
150                    throws PortalException {
151    
152                    Image image = null;
153    
154                    try {
155                            image = ImageToolUtil.getImage(bytes);
156                    }
157                    catch (IOException ioe) {
158                            throw new SystemException(ioe);
159                    }
160    
161                    return updateImage(
162                            imageId, image.getTextObj(), image.getType(), image.getHeight(),
163                            image.getWidth(), image.getSize());
164            }
165    
166            @Override
167            public Image updateImage(
168                            long imageId, byte[] bytes, String type, int height, int width,
169                            int size)
170                    throws PortalException {
171    
172                    validate(type);
173    
174                    Image image = imagePersistence.fetchByPrimaryKey(imageId);
175    
176                    if (image == null) {
177                            image = imagePersistence.create(imageId);
178                    }
179    
180                    image.setModifiedDate(new Date());
181                    image.setType(type);
182                    image.setHeight(height);
183                    image.setWidth(width);
184                    image.setSize(size);
185    
186                    Hook hook = HookFactory.getInstance();
187    
188                    hook.updateImage(image, type, bytes);
189    
190                    imagePersistence.update(image);
191    
192                    WebServerServletTokenUtil.resetToken(imageId);
193    
194                    return image;
195            }
196    
197            @Override
198            public Image updateImage(long imageId, File file) throws PortalException {
199                    Image image = null;
200    
201                    try {
202                            image = ImageToolUtil.getImage(file);
203                    }
204                    catch (IOException ioe) {
205                            throw new SystemException(ioe);
206                    }
207    
208                    return updateImage(
209                            imageId, image.getTextObj(), image.getType(), image.getHeight(),
210                            image.getWidth(), image.getSize());
211            }
212    
213            @Override
214            public Image updateImage(long imageId, InputStream is)
215                    throws PortalException {
216    
217                    Image image = null;
218    
219                    try {
220                            image = ImageToolUtil.getImage(is);
221                    }
222                    catch (IOException ioe) {
223                            throw new SystemException(ioe);
224                    }
225    
226                    return updateImage(
227                            imageId, image.getTextObj(), image.getType(), image.getHeight(),
228                            image.getWidth(), image.getSize());
229            }
230    
231            @Override
232            public Image updateImage(
233                            long imageId, InputStream is, boolean cleanUpStream)
234                    throws PortalException {
235    
236                    Image image = null;
237    
238                    try {
239                            image = ImageToolUtil.getImage(is, cleanUpStream);
240                    }
241                    catch (IOException ioe) {
242                            throw new SystemException(ioe);
243                    }
244    
245                    return updateImage(
246                            imageId, image.getTextObj(), image.getType(), image.getHeight(),
247                            image.getWidth(), image.getSize());
248            }
249    
250            protected void validate(String type) throws PortalException {
251                    if ((type == null) || type.contains(StringPool.BACK_SLASH) ||
252                            type.contains(StringPool.COLON) ||
253                            type.contains(StringPool.GREATER_THAN) ||
254                            type.contains(StringPool.LESS_THAN) ||
255                            type.contains(StringPool.PERCENT) ||
256                            type.contains(StringPool.PERIOD) ||
257                            type.contains(StringPool.PIPE) ||
258                            type.contains(StringPool.QUESTION) ||
259                            type.contains(StringPool.QUOTE) ||
260                            type.contains(StringPool.SLASH) ||
261                            type.contains(StringPool.SPACE) || type.contains(StringPool.STAR)) {
262    
263                            throw new ImageTypeException();
264                    }
265            }
266    
267            private static final Log _log = LogFactoryUtil.getLog(
268                    ImageLocalServiceImpl.class);
269    
270    }