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