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.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   */
42  public class LiferayFileItem extends DiskFileItem {
43  
44      public static final int THRESHOLD_SIZE = GetterUtil.getInteger(
45          PropsUtil.get(LiferayFileItem.class.getName() + ".threshold.size"));
46  
47      public LiferayFileItem(
48          String fieldName, String contentType, boolean isFormField,
49          String fileName, int sizeThreshold, File repository) {
50  
51          super(
52              fieldName, contentType, isFormField, fileName, sizeThreshold,
53              repository);
54  
55          _fileName = fileName;
56          _repository = repository;
57      }
58  
59      public String getFileName() {
60          if (_fileName == null) {
61              return null;
62          }
63  
64          int pos = _fileName.lastIndexOf("/");
65  
66          if (pos == -1) {
67              pos = _fileName.lastIndexOf("\\");
68          }
69  
70          if (pos == -1) {
71              return _fileName;
72          }
73          else {
74              return _fileName.substring(pos + 1, _fileName.length());
75          }
76      }
77  
78      public String getFullFileName() {
79          return _fileName;
80      }
81  
82      public String getFileNameExtension() {
83          return FileUtil.getExtension(_fileName);
84      }
85  
86      public String getString() {
87  
88          // Prevent serialization of uploaded content
89  
90          if (getSize() > THRESHOLD_SIZE) {
91              return StringPool.BLANK;
92          }
93  
94          if (_encodedString == null) {
95              return super.getString();
96          }
97          else {
98              return _encodedString;
99          }
100     }
101 
102     public void setString(String encode) {
103         try {
104             _encodedString = getString(encode);
105         }
106         catch (Exception e) {
107         }
108     }
109 
110     protected File getTempFile() {
111         String tempFileName = "upload_" + _getUniqueId();
112 
113         String extension = getFileNameExtension();
114 
115         if (extension != null) {
116             tempFileName += "." + extension;
117         }
118 
119         File tempFile = new File(_repository, tempFileName);
120 
121         tempFile.deleteOnExit();
122 
123         return tempFile;
124     }
125 
126     private static String _getUniqueId() {
127         int current;
128 
129         synchronized (LiferayFileItem.class) {
130             current = _counter++;
131         }
132 
133         String id = String.valueOf(current);
134 
135         if (current < 100000000) {
136             id = ("00000000" + id).substring(id.length());
137         }
138 
139         return id;
140     }
141 
142     private static int _counter = 0;
143 
144     private String _fileName;
145     private File _repository;
146     private String _encodedString;
147 
148 }