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.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    /**
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                    /*if (PropsValues.IMAGE_HOOK_IMPL.equals(
052                                    DatabaseHook.class.getName()) &&
053                            (imagePersistence.getListeners().length == 0)) {
054    
055                            runSQL("delete from Image where imageId = " + imageId);
056    
057                            imagePersistence.clearCache();
058                    }
059                    else {*/
060                            Image image = getImage(imageId);
061    
062                            if (image != null) {
063                                    imagePersistence.remove(image);
064    
065                                    Hook hook = HookFactory.getInstance();
066    
067                                    try {
068                                            hook.deleteImage(image);
069                                    }
070                                    catch (NoSuchImageException nsie) {
071    
072                                            // DLHook throws NoSuchImageException if the file no longer
073                                            // exists. See LPS-30430. This exception can be ignored.
074    
075                                            if (_log.isWarnEnabled()) {
076                                                    _log.warn(nsie, nsie);
077                                            }
078                                    }
079                            }
080    
081                            return image;
082                    //}
083            }
084    
085            @Override
086            public Image getCompanyLogo(long imageId) {
087                    Image image = getImage(imageId);
088    
089                    if (image == null) {
090                            image = ImageToolUtil.getDefaultCompanyLogo();
091                    }
092    
093                    return image;
094            }
095    
096            @Override
097            public Image getImage(long imageId) {
098                    if (imageId > 0) {
099                            try {
100                                    return imagePersistence.fetchByPrimaryKey(imageId);
101                            }
102                            catch (Exception e) {
103                                    if (_log.isWarnEnabled()) {
104                                            _log.warn(
105                                                    "Unable to get image " + imageId + ": " +
106                                                            e.getMessage());
107                                    }
108                            }
109                    }
110    
111                    return null;
112            }
113    
114            @Override
115            public Image getImageOrDefault(long imageId) {
116                    Image image = getImage(imageId);
117    
118                    if (image == null) {
119                            image = ImageToolUtil.getDefaultSpacer();
120                    }
121    
122                    return image;
123            }
124    
125            @Override
126            public List<Image> getImages() {
127                    return imagePersistence.findAll();
128            }
129    
130            @Override
131            public List<Image> getImagesBySize(int size) {
132                    return imagePersistence.findByLtSize(size);
133            }
134    
135            @Override
136            public Image moveImage(long imageId, byte[] bytes) throws PortalException {
137                    Image image = updateImage(counterLocalService.increment(), bytes);
138    
139                    if (imageId > 0) {
140                            deleteImage(imageId);
141                    }
142    
143                    return image;
144            }
145    
146            @Override
147            public Image updateImage(long imageId, byte[] bytes)
148                    throws PortalException {
149    
150                    Image image = null;
151    
152                    try {
153                            image = ImageToolUtil.getImage(bytes);
154                    }
155                    catch (IOException ioe) {
156                            throw new SystemException(ioe);
157                    }
158    
159                    return updateImage(
160                            imageId, image.getTextObj(), image.getType(), image.getHeight(),
161                            image.getWidth(), image.getSize());
162            }
163    
164            @Override
165            public Image updateImage(
166                            long imageId, byte[] bytes, String type, int height, int width,
167                            int size)
168                    throws PortalException {
169    
170                    validate(type);
171    
172                    Image image = imagePersistence.fetchByPrimaryKey(imageId);
173    
174                    if (image == null) {
175                            image = imagePersistence.create(imageId);
176                    }
177    
178                    image.setModifiedDate(new Date());
179                    image.setType(type);
180                    image.setHeight(height);
181                    image.setWidth(width);
182                    image.setSize(size);
183    
184                    Hook hook = HookFactory.getInstance();
185    
186                    hook.updateImage(image, type, bytes);
187    
188                    imagePersistence.update(image);
189    
190                    WebServerServletTokenUtil.resetToken(imageId);
191    
192                    return image;
193            }
194    
195            @Override
196            public Image updateImage(long imageId, File file) throws PortalException {
197                    Image image = null;
198    
199                    try {
200                            image = ImageToolUtil.getImage(file);
201                    }
202                    catch (IOException ioe) {
203                            throw new SystemException(ioe);
204                    }
205    
206                    return updateImage(
207                            imageId, image.getTextObj(), image.getType(), image.getHeight(),
208                            image.getWidth(), image.getSize());
209            }
210    
211            @Override
212            public Image updateImage(long imageId, InputStream is)
213                    throws PortalException {
214    
215                    Image image = null;
216    
217                    try {
218                            image = ImageToolUtil.getImage(is);
219                    }
220                    catch (IOException ioe) {
221                            throw new SystemException(ioe);
222                    }
223    
224                    return updateImage(
225                            imageId, image.getTextObj(), image.getType(), image.getHeight(),
226                            image.getWidth(), image.getSize());
227            }
228    
229            @Override
230            public Image updateImage(
231                            long imageId, InputStream is, boolean cleanUpStream)
232                    throws PortalException {
233    
234                    Image image = null;
235    
236                    try {
237                            image = ImageToolUtil.getImage(is, cleanUpStream);
238                    }
239                    catch (IOException ioe) {
240                            throw new SystemException(ioe);
241                    }
242    
243                    return updateImage(
244                            imageId, image.getTextObj(), image.getType(), image.getHeight(),
245                            image.getWidth(), image.getSize());
246            }
247    
248            protected void validate(String type) throws PortalException {
249                    if ((type == null) || type.contains(StringPool.BACK_SLASH) ||
250                            type.contains(StringPool.COLON) ||
251                            type.contains(StringPool.GREATER_THAN) ||
252                            type.contains(StringPool.LESS_THAN) ||
253                            type.contains(StringPool.PERCENT) ||
254                            type.contains(StringPool.PERIOD) ||
255                            type.contains(StringPool.PIPE) ||
256                            type.contains(StringPool.QUESTION) ||
257                            type.contains(StringPool.QUOTE) ||
258                            type.contains(StringPool.SLASH) ||
259                            type.contains(StringPool.SPACE) || type.contains(StringPool.STAR)) {
260    
261                            throw new ImageTypeException();
262                    }
263            }
264    
265            private static final Log _log = LogFactoryUtil.getLog(
266                    ImageLocalServiceImpl.class);
267    
268    }