001    /**
002     * Copyright (c) 2000-2013 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.image;
016    
017    import com.liferay.portal.NoSuchImageException;
018    import com.liferay.portal.kernel.exception.PortalException;
019    import com.liferay.portal.kernel.exception.SystemException;
020    import com.liferay.portal.kernel.util.FileUtil;
021    import com.liferay.portal.kernel.util.StringPool;
022    import com.liferay.portal.model.Image;
023    import com.liferay.portlet.documentlibrary.NoSuchFileException;
024    import com.liferay.portlet.documentlibrary.store.DLStoreUtil;
025    
026    import java.io.IOException;
027    import java.io.InputStream;
028    
029    /**
030     * @author Jorge Ferrer
031     */
032    public class DLHook extends BaseHook {
033    
034            @Override
035            public void deleteImage(Image image)
036                    throws PortalException, SystemException {
037    
038                    String fileName = getFileName(image.getImageId(), image.getType());
039    
040                    try {
041                            DLStoreUtil.deleteFile(_COMPANY_ID, _REPOSITORY_ID, fileName);
042                    }
043                    catch (NoSuchFileException nsfe) {
044                            throw new NoSuchImageException(nsfe);
045                    }
046            }
047    
048            @Override
049            public byte[] getImageAsBytes(Image image)
050                    throws PortalException, SystemException {
051    
052                    String fileName = getFileName(image.getImageId(), image.getType());
053    
054                    InputStream is = DLStoreUtil.getFileAsStream(
055                            _COMPANY_ID, _REPOSITORY_ID, fileName);
056    
057                    byte[] bytes = null;
058    
059                    try {
060                            bytes = FileUtil.getBytes(is);
061                    }
062                    catch (IOException ioe) {
063                            throw new SystemException(ioe);
064                    }
065    
066                    return bytes;
067            }
068    
069            @Override
070            public InputStream getImageAsStream(Image image)
071                    throws PortalException, SystemException {
072    
073                    String fileName = getFileName(image.getImageId(), image.getType());
074    
075                    return DLStoreUtil.getFileAsStream(
076                            _COMPANY_ID, _REPOSITORY_ID, fileName);
077            }
078    
079            @Override
080            public void updateImage(Image image, String type, byte[] bytes)
081                    throws PortalException, SystemException {
082    
083                    String fileName = getFileName(image.getImageId(), image.getType());
084    
085                    if (DLStoreUtil.hasFile(_COMPANY_ID, _REPOSITORY_ID, fileName)) {
086                            DLStoreUtil.deleteFile(_COMPANY_ID, _REPOSITORY_ID, fileName);
087                    }
088    
089                    DLStoreUtil.addFile(_COMPANY_ID, _REPOSITORY_ID, fileName, true, bytes);
090            }
091    
092            protected String getFileName(long imageId, String type) {
093                    return imageId + StringPool.PERIOD + type;
094            }
095    
096            private static final long _COMPANY_ID = 0;
097    
098            private static final long _REPOSITORY_ID = 0;
099    
100    }