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.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.kernel.util.FileUtil;
020    import com.liferay.portal.kernel.util.StringBundler;
021    import com.liferay.portal.kernel.util.StringPool;
022    import com.liferay.portal.model.Image;
023    import com.liferay.portal.util.PropsValues;
024    import com.liferay.portlet.documentlibrary.NoSuchFileException;
025    
026    import java.io.File;
027    import java.io.FileInputStream;
028    import java.io.IOException;
029    import java.io.InputStream;
030    
031    /**
032     * @author Jorge Ferrer
033     */
034    public class FileSystemHook extends BaseHook {
035    
036            public FileSystemHook() {
037                    _rootDir = new File(PropsValues.IMAGE_HOOK_FILE_SYSTEM_ROOT_DIR);
038    
039                    if (!_rootDir.exists()) {
040                            _rootDir.mkdirs();
041                    }
042            }
043    
044            @Override
045            public void deleteImage(Image image) {
046                    File file = getFile(image.getImageId(), image.getType());
047    
048                    FileUtil.delete(file);
049            }
050    
051            @Override
052            public byte[] getImageAsBytes(Image image)
053                    throws PortalException, SystemException {
054    
055                    try {
056                            File file = getFile(image.getImageId(), image.getType());
057    
058                            if (!file.exists()) {
059                                    throw new NoSuchFileException(file.getPath());
060                            }
061    
062                            return FileUtil.getBytes(file);
063                    }
064                    catch (IOException ioe) {
065                            throw new SystemException(ioe);
066                    }
067            }
068    
069            @Override
070            public InputStream getImageAsStream(Image image)
071                    throws PortalException, SystemException {
072    
073                    try {
074                            File file = getFile(image.getImageId(), image.getType());
075    
076                            if (!file.exists()) {
077                                    throw new NoSuchFileException(file.getPath());
078                            }
079    
080                            return new FileInputStream(file);
081                    }
082                    catch (IOException ioe) {
083                            throw new SystemException(ioe);
084                    }
085            }
086    
087            @Override
088            public void updateImage(Image image, String type, byte[] bytes)
089                    throws SystemException {
090    
091                    try {
092                            File file = getFile(image.getImageId(), type);
093    
094                            FileUtil.write(file, bytes);
095                    }
096                    catch (IOException ioe) {
097                            throw new SystemException(ioe);
098                    }
099            }
100    
101            protected String buildPath(String fileNameFragment) {
102                    int fileNameFragmentLength = fileNameFragment.length();
103    
104                    if (fileNameFragmentLength <= 2) {
105                            return StringPool.BLANK;
106                    }
107    
108                    StringBundler sb = new StringBundler(
109                            fileNameFragmentLength / 2 + fileNameFragmentLength);
110    
111                    for (int i = 0; i < fileNameFragmentLength; i += 2) {
112                            if ((i + 2) < fileNameFragmentLength) {
113                                    sb.append(StringPool.SLASH);
114                                    sb.append(fileNameFragment.substring(i, i + 2));
115                            }
116                    }
117    
118                    return sb.toString();
119            }
120    
121            protected File getFile(long imageId, String type) {
122                    String path = buildPath(String.valueOf(imageId));
123    
124                    return new File(
125                            _rootDir + StringPool.SLASH + path + StringPool.SLASH +
126                                    imageId + StringPool.PERIOD + type);
127            }
128    
129            private File _rootDir;
130    
131    }