1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portal.image;
16  
17  import com.liferay.documentlibrary.NoSuchFileException;
18  import com.liferay.portal.PortalException;
19  import com.liferay.portal.SystemException;
20  import com.liferay.portal.kernel.util.FileUtil;
21  import com.liferay.portal.kernel.util.StringBundler;
22  import com.liferay.portal.kernel.util.StringPool;
23  import com.liferay.portal.model.Image;
24  import com.liferay.portal.util.PropsValues;
25  
26  import java.io.File;
27  import java.io.FileInputStream;
28  import java.io.IOException;
29  import java.io.InputStream;
30  
31  /**
32   * <a href="FileSystemHook.java.html"><b><i>View Source</i></b></a>
33   *
34   * @author Jorge Ferrer
35   */
36  public class FileSystemHook extends BaseHook {
37  
38      public FileSystemHook() {
39          _rootDir = new File(PropsValues.IMAGE_HOOK_FILE_SYSTEM_ROOT_DIR);
40  
41          if (!_rootDir.exists()) {
42              _rootDir.mkdirs();
43          }
44      }
45  
46      public void deleteImage(Image image) {
47          File file = getFile(image.getImageId(), image.getType());
48  
49          FileUtil.delete(file);
50      }
51  
52      public byte[] getImageAsBytes(Image image)
53          throws PortalException, SystemException {
54  
55          try {
56              File file = getFile(image.getImageId(), image.getType());
57  
58              if (!file.exists()) {
59                  throw new NoSuchFileException(file.getPath());
60              }
61  
62              return FileUtil.getBytes(file);
63          }
64          catch (IOException ioe) {
65              throw new SystemException(ioe);
66          }
67      }
68  
69      public InputStream getImageAsStream(Image image)
70          throws PortalException, SystemException {
71  
72          try {
73              File file = getFile(image.getImageId(), image.getType());
74  
75              if (!file.exists()) {
76                  throw new NoSuchFileException(file.getPath());
77              }
78  
79              return new FileInputStream(file);
80          }
81          catch (IOException ioe) {
82              throw new SystemException(ioe);
83          }
84      }
85  
86      public void updateImage(Image image, String type, byte[] bytes)
87          throws SystemException {
88  
89          try {
90              File file = getFile(image.getImageId(), type);
91  
92              FileUtil.write(file, bytes);
93          }
94          catch (IOException ioe) {
95              throw new SystemException(ioe);
96          }
97      }
98  
99      protected String buildPath(String fileNameFragment) {
100         int fileNameFragmentLength = fileNameFragment.length();
101 
102         if (fileNameFragmentLength <= 2) {
103             return StringPool.BLANK;
104         }
105 
106         StringBundler sb = new StringBundler(
107             fileNameFragmentLength / 2 + fileNameFragmentLength);
108 
109         for (int i = 0; i < fileNameFragmentLength; i += 2) {
110             if ((i + 2) < fileNameFragmentLength) {
111                 sb.append(StringPool.SLASH);
112                 sb.append(fileNameFragment.substring(i, i + 2));
113             }
114         }
115 
116         return sb.toString();
117     }
118 
119     protected File getFile(long imageId, String type) {
120         String path = buildPath(String.valueOf(imageId));
121 
122         return new File(
123             _rootDir + StringPool.SLASH + path + StringPool.SLASH +
124                 imageId + StringPool.PERIOD + type);
125     }
126 
127     private File _rootDir;
128 
129 }