1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.documentlibrary.util;
16  
17  import com.liferay.portal.kernel.exception.PortalException;
18  import com.liferay.portal.kernel.util.FileUtil;
19  import com.liferay.portal.kernel.util.StringBundler;
20  import com.liferay.portal.kernel.util.StringPool;
21  import com.liferay.portal.kernel.util.StringUtil;
22  import com.liferay.portlet.documentlibrary.util.DLUtil;
23  
24  import java.io.File;
25  
26  /**
27   * <a href="AdvancedFileSystemHook.java.html"><b><i>View Source</i></b></a>
28   *
29   * <p>
30   * See http://issues.liferay.com/browse/LPS-1976.
31   * </p>
32   *
33   * @author Jorge Ferrer
34   * @author Ryan Park
35   * @author Brian Wing Shun Chan
36   */
37  public class AdvancedFileSystemHook extends FileSystemHook {
38  
39      public void updateFile(
40              long companyId, String portletId, long groupId, long repositoryId,
41              String fileName, String newFileName, boolean reindex)
42          throws PortalException {
43  
44          super.updateFile(
45              companyId, portletId, groupId, repositoryId, fileName, newFileName,
46              reindex);
47  
48          File newFileNameDir = getFileNameDir(
49              companyId, repositoryId, newFileName);
50  
51          String[] fileNameVersions = FileUtil.listFiles(newFileNameDir);
52  
53          for (String fileNameVersion : fileNameVersions) {
54              String ext = FileUtil.getExtension(fileNameVersion);
55  
56              if (ext.equals(_HOOK_EXTENSION)) {
57                  continue;
58              }
59  
60              File fileNameVersionFile = new File(
61                  newFileNameDir + StringPool.SLASH + fileNameVersion);
62              File newFileNameVersionFile = new File(
63                  newFileNameDir + StringPool.SLASH +
64                      FileUtil.stripExtension(fileNameVersion) +
65                          StringPool.PERIOD + _HOOK_EXTENSION);
66  
67              fileNameVersionFile.renameTo(newFileNameVersionFile);
68          }
69      }
70  
71      protected void buildPath(StringBundler sb, String fileNameFragment) {
72          int fileNameFragmentLength = fileNameFragment.length();
73  
74          if ((fileNameFragmentLength <= 2) || (getDepth(sb.toString()) > 3)) {
75              return;
76          }
77  
78          for (int i = 0;i < fileNameFragmentLength;i += 2) {
79              if ((i + 2) < fileNameFragmentLength) {
80                  sb.append(fileNameFragment.substring(i, i + 2));
81                  sb.append(StringPool.SLASH);
82  
83                  if (getDepth(sb.toString()) > 3) {
84                      return;
85                  }
86              }
87          }
88  
89          return;
90      }
91  
92      protected int getDepth(String path) {
93          String[] fragments = StringUtil.split(path, StringPool.SLASH);
94  
95          return fragments.length;
96      }
97  
98      protected File getDirNameDir(
99          long companyId, long repositoryId, String dirName) {
100 
101         File repositoryDir = getRepositoryDir(companyId, repositoryId);
102 
103         return new File(repositoryDir + StringPool.SLASH + dirName);
104     }
105 
106     protected File getFileNameDir(
107         long companyId, long repositoryId, String fileName) {
108 
109         String ext = StringPool.PERIOD + FileUtil.getExtension(fileName);
110 
111         if (ext.equals(StringPool.PERIOD)) {
112             ext += _HOOK_EXTENSION;
113         }
114 
115         StringBundler sb = new StringBundler();
116 
117         String fileNameFragment = FileUtil.stripExtension(fileName);
118 
119         if (fileNameFragment.startsWith("DLFE-")) {
120             fileNameFragment = fileNameFragment.substring(5);
121 
122             sb.append("DLFE" + StringPool.SLASH);
123         }
124 
125         buildPath(sb, fileNameFragment);
126 
127         File repositoryDir = getRepositoryDir(companyId, repositoryId);
128 
129         File fileNameDir = new File(
130             repositoryDir + StringPool.SLASH + sb.toString() +
131                 StringPool.SLASH + fileNameFragment + ext);
132 
133         return fileNameDir;
134     }
135 
136     protected File getFileNameVersionFile(
137         long companyId, long repositoryId, String fileName, String version) {
138 
139         String ext = StringPool.PERIOD + FileUtil.getExtension(fileName);
140 
141         if (ext.equals(StringPool.PERIOD)) {
142             ext += _HOOK_EXTENSION;
143         }
144 
145         int pos = fileName.lastIndexOf(StringPool.SLASH);
146 
147         if (pos == -1) {
148             StringBundler sb = new StringBundler();
149 
150             String fileNameFragment = FileUtil.stripExtension(fileName);
151 
152             if (fileNameFragment.startsWith("DLFE-")) {
153                 fileNameFragment = fileNameFragment.substring(5);
154 
155                 sb.append("DLFE" + StringPool.SLASH);
156             }
157 
158             buildPath(sb, fileNameFragment);
159 
160             File repositoryDir = getRepositoryDir(companyId, repositoryId);
161 
162             return new File(
163                 repositoryDir + StringPool.SLASH + sb.toString() +
164                     StringPool.SLASH + fileNameFragment + ext +
165                         StringPool.SLASH + fileNameFragment +
166                             StringPool.UNDERLINE + version + ext);
167         }
168         else {
169             File fileNameDir = getDirNameDir(companyId, repositoryId, fileName);
170 
171             String fileNameFragment = FileUtil.stripExtension(
172                 fileName.substring(pos + 1));
173 
174             return new File(
175                 fileNameDir + StringPool.SLASH + fileNameFragment +
176                     StringPool.UNDERLINE + version + ext);
177         }
178     }
179 
180     protected String getHeadVersionNumber(
181         long companyId, long repositoryId, String fileName) {
182 
183         File fileNameDir = getFileNameDir(companyId, repositoryId, fileName);
184 
185         if (!fileNameDir.exists()) {
186             return DEFAULT_VERSION;
187         }
188 
189         String[] versionNumbers = FileUtil.listFiles(fileNameDir);
190 
191         String headVersionNumber = DEFAULT_VERSION;
192 
193         for (int i = 0; i < versionNumbers.length; i++) {
194             String versionNumberFragment = versionNumbers[i];
195 
196             int pos = versionNumberFragment.lastIndexOf(StringPool.UNDERLINE);
197 
198             if (pos > -1) {
199                 versionNumberFragment = versionNumberFragment.substring(
200                     pos + 1);
201             }
202 
203             String versionNumber = versionNumberFragment;
204 
205             if (DLUtil.compareVersions(versionNumber, headVersionNumber) > 0) {
206                 headVersionNumber = versionNumber;
207             }
208         }
209 
210         return headVersionNumber;
211     }
212 
213     private static final String _HOOK_EXTENSION = "afsh";
214 
215 }