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