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