1
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
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
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 }