001
014
015 package com.liferay.portal.upload;
016
017 import com.liferay.portal.kernel.util.FileUtil;
018 import com.liferay.portal.kernel.util.GetterUtil;
019 import com.liferay.portal.kernel.util.StringPool;
020 import com.liferay.portal.util.PropsUtil;
021
022 import java.io.File;
023
024 import org.apache.commons.fileupload.disk.DiskFileItem;
025
026
031 public class LiferayFileItem extends DiskFileItem {
032
033 public static final int THRESHOLD_SIZE = GetterUtil.getInteger(
034 PropsUtil.get(LiferayFileItem.class.getName() + ".threshold.size"));
035
036 public LiferayFileItem(
037 String fieldName, String contentType, boolean isFormField,
038 String fileName, int sizeThreshold, File repository) {
039
040 super(
041 fieldName, contentType, isFormField, fileName, sizeThreshold,
042 repository);
043
044 _fileName = fileName;
045 _repository = repository;
046 }
047
048 public String getFileName() {
049 if (_fileName == null) {
050 return null;
051 }
052
053 int pos = _fileName.lastIndexOf("/");
054
055 if (pos == -1) {
056 pos = _fileName.lastIndexOf("\\");
057 }
058
059 if (pos == -1) {
060 return _fileName;
061 }
062 else {
063 return _fileName.substring(pos + 1, _fileName.length());
064 }
065 }
066
067 public String getFullFileName() {
068 return _fileName;
069 }
070
071 public String getFileNameExtension() {
072 return FileUtil.getExtension(_fileName);
073 }
074
075 public String getString() {
076
077
078
079 if (getSize() > THRESHOLD_SIZE) {
080 return StringPool.BLANK;
081 }
082
083 if (_encodedString == null) {
084 return super.getString();
085 }
086 else {
087 return _encodedString;
088 }
089 }
090
091 public void setString(String encode) {
092 try {
093 _encodedString = getString(encode);
094 }
095 catch (Exception e) {
096 }
097 }
098
099 protected File getTempFile() {
100 String tempFileName = "upload_" + _getUniqueId();
101
102 String extension = getFileNameExtension();
103
104 if (extension != null) {
105 tempFileName += "." + extension;
106 }
107
108 File tempFile = new File(_repository, tempFileName);
109
110 tempFile.deleteOnExit();
111
112 return tempFile;
113 }
114
115 private static String _getUniqueId() {
116 int current;
117
118 synchronized (LiferayFileItem.class) {
119 current = _counter++;
120 }
121
122 String id = String.valueOf(current);
123
124 if (current < 100000000) {
125 id = ("00000000" + id).substring(id.length());
126 }
127
128 return id;
129 }
130
131 private static int _counter = 0;
132
133 private String _fileName;
134 private File _repository;
135 private String _encodedString;
136
137 }