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.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) throws PortalException {
053                    try {
054                            File file = getFile(image.getImageId(), image.getType());
055    
056                            if (!file.exists()) {
057                                    throw new NoSuchFileException(file.getPath());
058                            }
059    
060                            return FileUtil.getBytes(file);
061                    }
062                    catch (IOException ioe) {
063                            throw new SystemException(ioe);
064                    }
065            }
066    
067            @Override
068            public InputStream getImageAsStream(Image image) throws PortalException {
069                    try {
070                            File file = getFile(image.getImageId(), image.getType());
071    
072                            if (!file.exists()) {
073                                    throw new NoSuchFileException(file.getPath());
074                            }
075    
076                            return new FileInputStream(file);
077                    }
078                    catch (IOException ioe) {
079                            throw new SystemException(ioe);
080                    }
081            }
082    
083            @Override
084            public void updateImage(Image image, String type, byte[] bytes) {
085                    try {
086                            File file = getFile(image.getImageId(), type);
087    
088                            FileUtil.write(file, bytes);
089                    }
090                    catch (IOException ioe) {
091                            throw new SystemException(ioe);
092                    }
093            }
094    
095            protected String buildPath(String fileNameFragment) {
096                    int fileNameFragmentLength = fileNameFragment.length();
097    
098                    if (fileNameFragmentLength <= 2) {
099                            return StringPool.BLANK;
100                    }
101    
102                    StringBundler sb = new StringBundler(
103                            fileNameFragmentLength / 2 + fileNameFragmentLength);
104    
105                    for (int i = 0; i < fileNameFragmentLength; i += 2) {
106                            if ((i + 2) < fileNameFragmentLength) {
107                                    sb.append(StringPool.SLASH);
108                                    sb.append(fileNameFragment.substring(i, i + 2));
109                            }
110                    }
111    
112                    return sb.toString();
113            }
114    
115            protected File getFile(long imageId, String type) {
116                    String path = buildPath(String.valueOf(imageId));
117    
118                    return new File(
119                            _rootDir + StringPool.SLASH + path + StringPool.SLASH +
120                                    imageId + StringPool.PERIOD + type);
121            }
122    
123            private File _rootDir;
124    
125    }