1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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.upload;
24  
25  import com.liferay.portal.kernel.util.FileUtil;
26  import com.liferay.portal.kernel.util.GetterUtil;
27  import com.liferay.portal.kernel.util.StringPool;
28  import com.liferay.portal.util.PropsUtil;
29  
30  import java.io.File;
31  
32  import org.apache.commons.fileupload.disk.DiskFileItem;
33  
34  /**
35   * <a href="LiferayFileItem.java.html"><b><i>View Source</i></b></a>
36   *
37   * @author Brian Wing Shun Chan
38   * @author Zongliang Li
39   * @author Harry Mark
40   */
41  public class LiferayFileItem extends DiskFileItem {
42  
43      public static final int THRESHOLD_SIZE = GetterUtil.getInteger(
44          PropsUtil.get(LiferayFileItem.class.getName() + ".threshold.size"));
45  
46      public LiferayFileItem(
47          String fieldName, String contentType, boolean isFormField,
48          String fileName, int sizeThreshold, File repository) {
49  
50          super(
51              fieldName, contentType, isFormField, fileName, sizeThreshold,
52              repository);
53  
54          _fileName = fileName;
55          _repository = repository;
56      }
57  
58      public String getFileName() {
59          if (_fileName == null) {
60              return null;
61          }
62  
63          int pos = _fileName.lastIndexOf("/");
64  
65          if (pos == -1) {
66              pos = _fileName.lastIndexOf("\\");
67          }
68  
69          if (pos == -1) {
70              return _fileName;
71          }
72          else {
73              return _fileName.substring(pos + 1, _fileName.length());
74          }
75      }
76  
77      public String getFullFileName() {
78          return _fileName;
79      }
80  
81      public String getFileNameExtension() {
82          return FileUtil.getExtension(_fileName);
83      }
84  
85      public String getString() {
86  
87          // Prevent serialization of uploaded content
88  
89          if (getSize() > THRESHOLD_SIZE) {
90              return StringPool.BLANK;
91          }
92  
93          if (_encodedString == null) {
94              return super.getString();
95          }
96          else {
97              return _encodedString;
98          }
99      }
100 
101     public void setString(String encode) {
102         try {
103             _encodedString = getString(encode);
104         }
105         catch (Exception e) {
106         }
107     }
108 
109     protected File getTempFile() {
110         String tempFileName = "upload_" + _getUniqueId();
111 
112         String extension = getFileNameExtension();
113 
114         if (extension != null) {
115             tempFileName += "." + extension;
116         }
117 
118         File tempFile = new File(_repository, tempFileName);
119 
120         tempFile.deleteOnExit();
121 
122         return tempFile;
123     }
124 
125     private static String _getUniqueId() {
126         int current;
127 
128         synchronized (LiferayFileItem.class) {
129             current = _counter++;
130         }
131 
132         String id = String.valueOf(current);
133 
134         if (current < 100000000) {
135             id = ("00000000" + id).substring(id.length());
136         }
137 
138         return id;
139     }
140 
141     private static int _counter = 0;
142 
143     private String _fileName;
144     private File _repository;
145     private String _encodedString;
146 
147 }