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
42 public class LiferayFileItem extends DiskFileItem {
43
44 public static final int THRESHOLD_SIZE = GetterUtil.getInteger(
45 PropsUtil.get(LiferayFileItem.class.getName() + ".threshold.size"));
46
47 public LiferayFileItem(
48 String fieldName, String contentType, boolean isFormField,
49 String fileName, int sizeThreshold, File repository) {
50
51 super(
52 fieldName, contentType, isFormField, fileName, sizeThreshold,
53 repository);
54
55 _fileName = fileName;
56 _repository = repository;
57 }
58
59 public String getFileName() {
60 if (_fileName == null) {
61 return null;
62 }
63
64 int pos = _fileName.lastIndexOf("/");
65
66 if (pos == -1) {
67 pos = _fileName.lastIndexOf("\\");
68 }
69
70 if (pos == -1) {
71 return _fileName;
72 }
73 else {
74 return _fileName.substring(pos + 1, _fileName.length());
75 }
76 }
77
78 public String getFullFileName() {
79 return _fileName;
80 }
81
82 public String getFileNameExtension() {
83 return FileUtil.getExtension(_fileName);
84 }
85
86 public String getString() {
87
88
90 if (getSize() > THRESHOLD_SIZE) {
91 return StringPool.BLANK;
92 }
93
94 if (_encodedString == null) {
95 return super.getString();
96 }
97 else {
98 return _encodedString;
99 }
100 }
101
102 public void setString(String encode) {
103 try {
104 _encodedString = getString(encode);
105 }
106 catch (Exception e) {
107 }
108 }
109
110 protected File getTempFile() {
111 String tempFileName = "upload_" + _getUniqueId();
112
113 String extension = getFileNameExtension();
114
115 if (extension != null) {
116 tempFileName += "." + extension;
117 }
118
119 File tempFile = new File(_repository, tempFileName);
120
121 tempFile.deleteOnExit();
122
123 return tempFile;
124 }
125
126 private static String _getUniqueId() {
127 int current;
128
129 synchronized (LiferayFileItem.class) {
130 current = _counter++;
131 }
132
133 String id = String.valueOf(current);
134
135 if (current < 100000000) {
136 id = ("00000000" + id).substring(id.length());
137 }
138
139 return id;
140 }
141
142 private static int _counter = 0;
143
144 private String _fileName;
145 private File _repository;
146 private String _encodedString;
147
148 }