1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.image;
24  
25  import com.liferay.documentlibrary.NoSuchFileException;
26  import com.liferay.portal.PortalException;
27  import com.liferay.portal.SystemException;
28  import com.liferay.portal.kernel.util.FileUtil;
29  import com.liferay.portal.kernel.util.StringPool;
30  import com.liferay.portal.model.Image;
31  import com.liferay.portal.util.PropsValues;
32  
33  import java.io.File;
34  import java.io.FileInputStream;
35  import java.io.IOException;
36  import java.io.InputStream;
37  
38  /**
39   * <a href="FileSystemHook.java.html"><b><i>View Source</i></b></a>
40   *
41   * @author Jorge Ferrer
42   *
43   */
44  public class FileSystemHook extends BaseHook {
45  
46      public FileSystemHook() {
47          _rootDir = new File(PropsValues.IMAGE_HOOK_FILE_SYSTEM_ROOT_DIR);
48  
49          if (!_rootDir.exists()) {
50              _rootDir.mkdirs();
51          }
52      }
53  
54      public void deleteImage(Image image) {
55          File file = getFile(image.getImageId(), image.getType());
56  
57          FileUtil.delete(file);
58      }
59  
60      public byte[] getImageAsBytes(Image image)
61          throws PortalException, SystemException {
62  
63          try {
64              File file = getFile(image.getImageId(), image.getType());
65  
66              if (!file.exists()) {
67                  throw new NoSuchFileException(file.getPath());
68              }
69  
70              return FileUtil.getBytes(file);
71          }
72          catch (IOException ioe) {
73              throw new SystemException(ioe);
74          }
75      }
76  
77      public InputStream getImageAsStream(Image image)
78          throws PortalException, SystemException {
79  
80          try {
81              File file = getFile(image.getImageId(), image.getType());
82  
83              if (!file.exists()) {
84                  throw new NoSuchFileException(file.getPath());
85              }
86  
87              return new FileInputStream(file);
88          }
89          catch (IOException ioe) {
90              throw new SystemException(ioe);
91          }
92      }
93  
94      public void updateImage(Image image, String type, byte[] bytes)
95          throws SystemException {
96  
97          try {
98              File file = getFile(image.getImageId(), type);
99  
100             FileUtil.write(file, bytes);
101         }
102         catch (IOException ioe) {
103             throw new SystemException(ioe);
104         }
105     }
106 
107     protected void buildPath(StringBuilder sb, String fileNameFragment) {
108         if (fileNameFragment.length() <= 2) {
109             return;
110         }
111 
112         sb.append(StringPool.SLASH);
113         sb.append(fileNameFragment.substring(0, 2));
114 
115         buildPath(sb, fileNameFragment.substring(2));
116     }
117 
118     protected File getFile(long imageId, String type) {
119         StringBuilder sb = new StringBuilder();
120 
121         buildPath(sb, String.valueOf(imageId));
122 
123         return new File(
124             _rootDir + StringPool.SLASH + sb.toString() + StringPool.SLASH +
125                 imageId + StringPool.PERIOD + type);
126     }
127 
128     private File _rootDir;
129 
130 }