1
14
15 package com.liferay.portal.upload;
16
17 import com.liferay.portal.kernel.util.FileUtil;
18 import com.liferay.portal.kernel.util.GetterUtil;
19 import com.liferay.portal.kernel.util.StringPool;
20 import com.liferay.portal.util.PropsUtil;
21
22 import java.io.File;
23
24 import org.apache.commons.fileupload.disk.DiskFileItem;
25
26
33 public class LiferayFileItem extends DiskFileItem {
34
35 public static final int THRESHOLD_SIZE = GetterUtil.getInteger(
36 PropsUtil.get(LiferayFileItem.class.getName() + ".threshold.size"));
37
38 public LiferayFileItem(
39 String fieldName, String contentType, boolean isFormField,
40 String fileName, int sizeThreshold, File repository) {
41
42 super(
43 fieldName, contentType, isFormField, fileName, sizeThreshold,
44 repository);
45
46 _fileName = fileName;
47 _repository = repository;
48 }
49
50 public String getFileName() {
51 if (_fileName == null) {
52 return null;
53 }
54
55 int pos = _fileName.lastIndexOf("/");
56
57 if (pos == -1) {
58 pos = _fileName.lastIndexOf("\\");
59 }
60
61 if (pos == -1) {
62 return _fileName;
63 }
64 else {
65 return _fileName.substring(pos + 1, _fileName.length());
66 }
67 }
68
69 public String getFullFileName() {
70 return _fileName;
71 }
72
73 public String getFileNameExtension() {
74 return FileUtil.getExtension(_fileName);
75 }
76
77 public String getString() {
78
79
81 if (getSize() > THRESHOLD_SIZE) {
82 return StringPool.BLANK;
83 }
84
85 if (_encodedString == null) {
86 return super.getString();
87 }
88 else {
89 return _encodedString;
90 }
91 }
92
93 public void setString(String encode) {
94 try {
95 _encodedString = getString(encode);
96 }
97 catch (Exception e) {
98 }
99 }
100
101 protected File getTempFile() {
102 String tempFileName = "upload_" + _getUniqueId();
103
104 String extension = getFileNameExtension();
105
106 if (extension != null) {
107 tempFileName += "." + extension;
108 }
109
110 File tempFile = new File(_repository, tempFileName);
111
112 tempFile.deleteOnExit();
113
114 return tempFile;
115 }
116
117 private static String _getUniqueId() {
118 int current;
119
120 synchronized (LiferayFileItem.class) {
121 current = _counter++;
122 }
123
124 String id = String.valueOf(current);
125
126 if (current < 100000000) {
127 id = ("00000000" + id).substring(id.length());
128 }
129
130 return id;
131 }
132
133 private static int _counter = 0;
134
135 private String _fileName;
136 private File _repository;
137 private String _encodedString;
138
139 }