1   /**
2    * Copyright (c) 2000-2009 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   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.upload;
21  
22  import com.liferay.portal.kernel.util.FileUtil;
23  import com.liferay.portal.kernel.util.GetterUtil;
24  import com.liferay.portal.kernel.util.StringPool;
25  import com.liferay.portal.util.PropsUtil;
26  
27  import java.io.File;
28  
29  import org.apache.commons.fileupload.disk.DiskFileItem;
30  
31  /**
32   * <a href="LiferayFileItem.java.html"><b><i>View Source</i></b></a>
33   *
34   * @author Brian Wing Shun Chan
35   * @author Zongliang Li
36   * @author Harry Mark
37   *
38   */
39  public class LiferayFileItem extends DiskFileItem {
40  
41      public static final int THRESHOLD_SIZE = GetterUtil.getInteger(
42          PropsUtil.get(LiferayFileItem.class.getName() + ".threshold.size"));
43  
44      public LiferayFileItem(
45          String fieldName, String contentType, boolean isFormField,
46          String fileName, int sizeThreshold, File repository) {
47  
48          super(
49              fieldName, contentType, isFormField, fileName, sizeThreshold,
50              repository);
51  
52          _fileName = fileName;
53          _repository = repository;
54      }
55  
56      public String getFileName() {
57          if (_fileName == null) {
58              return null;
59          }
60  
61          int pos = _fileName.lastIndexOf("/");
62  
63          if (pos == -1) {
64              pos = _fileName.lastIndexOf("\\");
65          }
66  
67          if (pos == -1) {
68              return _fileName;
69          }
70          else {
71              return _fileName.substring(pos + 1, _fileName.length());
72          }
73      }
74  
75      public String getFullFileName() {
76          return _fileName;
77      }
78  
79      public String getFileNameExtension() {
80          return FileUtil.getExtension(_fileName);
81      }
82  
83      public String getString() {
84  
85          // Prevent serialization of uploaded content
86  
87          if (getSize() > THRESHOLD_SIZE) {
88              return StringPool.BLANK;
89          }
90  
91          if (_encodedString == null) {
92              return super.getString();
93          }
94          else {
95              return _encodedString;
96          }
97      }
98  
99      public void setString(String encode) {
100         try {
101             _encodedString = getString(encode);
102         }
103         catch (Exception e) {
104         }
105     }
106 
107     protected File getTempFile() {
108         String tempFileName = "upload_" + _getUniqueId();
109 
110         String extension = getFileNameExtension();
111 
112         if (extension != null) {
113             tempFileName += "." + extension;
114         }
115 
116         File tempFile = new File(_repository, tempFileName);
117 
118         tempFile.deleteOnExit();
119 
120         return tempFile;
121     }
122 
123     private static String _getUniqueId() {
124         int current;
125 
126         synchronized (LiferayFileItem.class) {
127             current = _counter++;
128         }
129 
130         String id = String.valueOf(current);
131 
132         if (current < 100000000) {
133             id = ("00000000" + id).substring(id.length());
134         }
135 
136         return id;
137     }
138 
139     private static int _counter = 0;
140 
141     private String _fileName;
142     private File _repository;
143     private String _encodedString;
144 
145 }