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 updateImage(long imageId, byte[] bytes)
137                    throws PortalException {
138    
139                    Image image = null;
140    
141                    try {
142                            image = ImageToolUtil.getImage(bytes);
143                    }
144                    catch (IOException ioe) {
145                            throw new SystemException(ioe);
146                    }
147    
148                    return updateImage(
149                            imageId, image.getTextObj(), image.getType(), image.getHeight(),
150                            image.getWidth(), image.getSize());
151            }
152    
153            @Override
154            public Image updateImage(
155                            long imageId, byte[] bytes, String type, int height, int width,
156                            int size)
157                    throws PortalException {
158    
159                    validate(type);
160    
161                    Image image = imagePersistence.fetchByPrimaryKey(imageId);
162    
163                    if (image == null) {
164                            image = imagePersistence.create(imageId);
165                    }
166    
167                    image.setModifiedDate(new Date());
168                    image.setType(type);
169                    image.setHeight(height);
170                    image.setWidth(width);
171                    image.setSize(size);
172    
173                    Hook hook = HookFactory.getInstance();
174    
175                    hook.updateImage(image, type, bytes);
176    
177                    imagePersistence.update(image);
178    
179                    WebServerServletTokenUtil.resetToken(imageId);
180    
181                    return image;
182            }
183    
184            @Override
185            public Image updateImage(long imageId, File file) throws PortalException {
186                    Image image = null;
187    
188                    try {
189                            image = ImageToolUtil.getImage(file);
190                    }
191                    catch (IOException ioe) {
192                            throw new SystemException(ioe);
193                    }
194    
195                    return updateImage(
196                            imageId, image.getTextObj(), image.getType(), image.getHeight(),
197                            image.getWidth(), image.getSize());
198            }
199    
200            @Override
201            public Image updateImage(long imageId, InputStream is)
202                    throws PortalException {
203    
204                    Image image = null;
205    
206                    try {
207                            image = ImageToolUtil.getImage(is);
208                    }
209                    catch (IOException ioe) {
210                            throw new SystemException(ioe);
211                    }
212    
213                    return updateImage(
214                            imageId, image.getTextObj(), image.getType(), image.getHeight(),
215                            image.getWidth(), image.getSize());
216            }
217    
218            @Override
219            public Image updateImage(
220                            long imageId, InputStream is, boolean cleanUpStream)
221                    throws PortalException {
222    
223                    Image image = null;
224    
225                    try {
226                            image = ImageToolUtil.getImage(is, cleanUpStream);
227                    }
228                    catch (IOException ioe) {
229                            throw new SystemException(ioe);
230                    }
231    
232                    return updateImage(
233                            imageId, image.getTextObj(), image.getType(), image.getHeight(),
234                            image.getWidth(), image.getSize());
235            }
236    
237            protected void validate(String type) throws PortalException {
238                    if ((type == null) ||
239                            type.contains(StringPool.BACK_SLASH) ||
240                            type.contains(StringPool.COLON) ||
241                            type.contains(StringPool.GREATER_THAN) ||
242                            type.contains(StringPool.LESS_THAN) ||
243                            type.contains(StringPool.PERCENT) ||
244                            type.contains(StringPool.PERIOD) ||
245                            type.contains(StringPool.PIPE) ||
246                            type.contains(StringPool.QUESTION) ||
247                            type.contains(StringPool.QUOTE) ||
248                            type.contains(StringPool.SLASH) ||
249                            type.contains(StringPool.SPACE) ||
250                            type.contains(StringPool.STAR)) {
251    
252                            throw new ImageTypeException();
253                    }
254            }
255    
256            private static final Log _log = LogFactoryUtil.getLog(
257                    ImageLocalServiceImpl.class);
258    
259    }