001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.util.servlet.fileupload;
022:
023: import com.liferay.portal.kernel.util.GetterUtil;
024: import com.liferay.portal.kernel.util.StringPool;
025: import com.liferay.util.FileUtil;
026: import com.liferay.util.SystemProperties;
027:
028: import java.io.File;
029:
030: import org.apache.commons.fileupload.disk.DiskFileItem;
031:
032: /**
033: * <a href="LiferayFileItem.java.html"><b><i>View Source</i></b></a>
034: *
035: * @author Brian Wing Shun Chan
036: * @author Zongliang Li
037: * @author Harry Mark
038: *
039: */
040: public class LiferayFileItem extends DiskFileItem {
041:
042: public static final int THRESHOLD_SIZE = GetterUtil
043: .getInteger(SystemProperties.get(LiferayFileItem.class
044: .getName()
045: + ".threshold.size"));
046:
047: public LiferayFileItem(String fieldName, String contentType,
048: boolean isFormField, String fileName, int sizeThreshold,
049: File repository) {
050:
051: super (fieldName, contentType, isFormField, fileName,
052: sizeThreshold, repository);
053:
054: _fileName = fileName;
055: _repository = repository;
056: }
057:
058: public String getFileName() {
059: if (_fileName == null) {
060: return null;
061: }
062:
063: int pos = _fileName.lastIndexOf("/");
064:
065: if (pos == -1) {
066: pos = _fileName.lastIndexOf("\\");
067: }
068:
069: if (pos == -1) {
070: return _fileName;
071: } else {
072: return _fileName.substring(pos + 1, _fileName.length());
073: }
074: }
075:
076: public String getFullFileName() {
077: return _fileName;
078: }
079:
080: public String getFileNameExtension() {
081: return FileUtil.getExtension(_fileName);
082: }
083:
084: public String getString() {
085:
086: // Prevent serialization of uploaded content
087:
088: if (getSize() > THRESHOLD_SIZE) {
089: return StringPool.BLANK;
090: }
091:
092: if (_encodedString == null) {
093: return super .getString();
094: } else {
095: return _encodedString;
096: }
097: }
098:
099: public void setString(String encode) {
100: try {
101: _encodedString = getString(encode);
102: } catch (Exception e) {
103: }
104: }
105:
106: protected File getTempFile() {
107: String tempFileName = "upload_" + _getUniqueId();
108:
109: String extension = getFileNameExtension();
110:
111: if (extension != null) {
112: tempFileName += "." + extension;
113: }
114:
115: File tempFile = new File(_repository, tempFileName);
116:
117: tempFile.deleteOnExit();
118:
119: return tempFile;
120: }
121:
122: private static String _getUniqueId() {
123: int current;
124:
125: synchronized (LiferayFileItem.class) {
126: current = _counter++;
127: }
128:
129: String id = Integer.toString(current);
130:
131: if (current < 100000000) {
132: id = ("00000000" + id).substring(id.length());
133: }
134:
135: return id;
136: }
137:
138: private static int _counter = 0;
139:
140: private String _fileName;
141: private File _repository;
142: private String _encodedString;
143:
144: }
|