001
014
015 package com.liferay.portal.upload;
016
017 import com.liferay.portal.kernel.log.Log;
018 import com.liferay.portal.kernel.log.LogFactoryUtil;
019 import com.liferay.portal.kernel.memory.DeleteFileFinalizeAction;
020 import com.liferay.portal.kernel.memory.FinalizeManager;
021 import com.liferay.portal.kernel.upload.FileItem;
022 import com.liferay.portal.kernel.util.ContentTypes;
023 import com.liferay.portal.kernel.util.FileUtil;
024 import com.liferay.portal.kernel.util.GetterUtil;
025 import com.liferay.portal.kernel.util.MimeTypesUtil;
026 import com.liferay.portal.kernel.util.StringPool;
027 import com.liferay.portal.util.PropsUtil;
028
029 import java.io.File;
030 import java.io.IOException;
031 import java.io.UnsupportedEncodingException;
032
033 import org.apache.commons.fileupload.disk.DiskFileItem;
034
035
040 public class LiferayFileItem extends DiskFileItem implements FileItem {
041
042 public static final long THRESHOLD_SIZE = GetterUtil.getLong(
043 PropsUtil.get(LiferayFileItem.class.getName() + ".threshold.size"));
044
045 public LiferayFileItem(
046 String fieldName, String contentType, boolean isFormField,
047 String fileName, int sizeThreshold, File repository) {
048
049 super(
050 fieldName, contentType, isFormField, fileName, sizeThreshold,
051 repository);
052
053 _fileName = fileName;
054 _sizeThreshold = sizeThreshold;
055 _repository = repository;
056 }
057
058 @Override
059 public String getContentType() {
060 try {
061 return MimeTypesUtil.getContentType(
062 getInputStream(), getFileName());
063 }
064 catch (IOException ioe) {
065 return ContentTypes.APPLICATION_OCTET_STREAM;
066 }
067 }
068
069 @Override
070 public String getEncodedString() {
071 return _encodedString;
072 }
073
074 @Override
075 public String getFileName() {
076 if (_fileName == null) {
077 return null;
078 }
079
080 int pos = _fileName.lastIndexOf("/");
081
082 if (pos == -1) {
083 pos = _fileName.lastIndexOf("\\");
084 }
085
086 if (pos == -1) {
087 return _fileName;
088 }
089 else {
090 return _fileName.substring(pos + 1);
091 }
092 }
093
094 @Override
095 public String getFileNameExtension() {
096 return FileUtil.getExtension(_fileName);
097 }
098
099 @Override
100 public String getFullFileName() {
101 return _fileName;
102 }
103
104 public long getItemSize() {
105 long size = getSize();
106
107 String contentType = getContentType();
108
109 if (contentType != null) {
110 byte[] bytes = contentType.getBytes();
111
112 size += bytes.length;
113 }
114
115 String fieldName = getFieldName();
116
117 if (fieldName != null) {
118 byte[] bytes = fieldName.getBytes();
119
120 size += bytes.length;
121 }
122
123 String fileName = getFileName();
124
125 if (fileName != null) {
126 byte[] bytes = fileName.getBytes();
127
128 size += bytes.length;
129 }
130
131 return size;
132 }
133
134 @Override
135 public int getSizeThreshold() {
136 return _sizeThreshold;
137 }
138
139 @Override
140 public String getString() {
141
142
143
144 if (getSize() > THRESHOLD_SIZE) {
145 return StringPool.BLANK;
146 }
147
148 if (_encodedString == null) {
149 return super.getString();
150 }
151 else {
152 return _encodedString;
153 }
154 }
155
156 @Override
157 public void setString(String encode) {
158 try {
159 _encodedString = getString(encode);
160 }
161 catch (UnsupportedEncodingException uee) {
162 _log.error(uee, uee);
163 }
164 }
165
166 @Override
167 protected File getTempFile() {
168 String tempFileName = "upload_" + _getUniqueId();
169
170 String extension = getFileNameExtension();
171
172 if (extension != null) {
173 tempFileName += "." + extension;
174 }
175
176 File tempFile = new File(_repository, tempFileName);
177
178 FinalizeManager.register(
179 tempFile, new DeleteFileFinalizeAction(tempFile.getAbsolutePath()),
180 FinalizeManager.PHANTOM_REFERENCE_FACTORY);
181
182 return tempFile;
183 }
184
185 private static String _getUniqueId() {
186 int current = 0;
187
188 synchronized (LiferayFileItem.class) {
189 current = _counter++;
190 }
191
192 String id = String.valueOf(current);
193
194 if (current < 100000000) {
195 id = ("00000000" + id).substring(id.length());
196 }
197
198 return id;
199 }
200
201 private static final Log _log = LogFactoryUtil.getLog(
202 LiferayFileItem.class);
203
204 private static int _counter;
205
206 private String _encodedString;
207 private String _fileName;
208 private final File _repository;
209 private final int _sizeThreshold;
210
211 }