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.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) throws PortalException {
036                    String fileName = getFileName(image.getImageId(), image.getType());
037    
038                    try {
039                            DLStoreUtil.deleteFile(_COMPANY_ID, _REPOSITORY_ID, fileName);
040                    }
041                    catch (NoSuchFileException nsfe) {
042                            throw new NoSuchImageException(nsfe);
043                    }
044            }
045    
046            @Override
047            public byte[] getImageAsBytes(Image image) throws PortalException {
048                    String fileName = getFileName(image.getImageId(), image.getType());
049    
050                    InputStream is = DLStoreUtil.getFileAsStream(
051                            _COMPANY_ID, _REPOSITORY_ID, fileName);
052    
053                    byte[] bytes = null;
054    
055                    try {
056                            bytes = FileUtil.getBytes(is);
057                    }
058                    catch (IOException ioe) {
059                            throw new SystemException(ioe);
060                    }
061    
062                    return bytes;
063            }
064    
065            @Override
066            public InputStream getImageAsStream(Image image) throws PortalException {
067                    String fileName = getFileName(image.getImageId(), image.getType());
068    
069                    return DLStoreUtil.getFileAsStream(
070                            _COMPANY_ID, _REPOSITORY_ID, fileName);
071            }
072    
073            @Override
074            public void updateImage(Image image, String type, byte[] bytes)
075                    throws PortalException {
076    
077                    String fileName = getFileName(image.getImageId(), image.getType());
078    
079                    if (DLStoreUtil.hasFile(_COMPANY_ID, _REPOSITORY_ID, fileName)) {
080                            DLStoreUtil.deleteFile(_COMPANY_ID, _REPOSITORY_ID, fileName);
081                    }
082    
083                    DLStoreUtil.addFile(_COMPANY_ID, _REPOSITORY_ID, fileName, true, bytes);
084            }
085    
086            protected String getFileName(long imageId, String type) {
087                    return imageId + StringPool.PERIOD + type;
088            }
089    
090            private static final long _COMPANY_ID = 0;
091    
092            private static final long _REPOSITORY_ID = 0;
093    
094    }