001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.upload;
016    
017    import com.liferay.portal.kernel.memory.DeleteFileFinalizeAction;
018    import com.liferay.portal.kernel.memory.FinalizeManager;
019    import com.liferay.portal.kernel.upload.FileItem;
020    import com.liferay.portal.kernel.util.FileUtil;
021    import com.liferay.portal.kernel.util.GetterUtil;
022    import com.liferay.portal.kernel.util.StringPool;
023    import com.liferay.portal.util.PropsUtil;
024    
025    import java.io.File;
026    
027    import org.apache.commons.fileupload.disk.DiskFileItem;
028    
029    /**
030     * @author Brian Wing Shun Chan
031     * @author Zongliang Li
032     * @author Harry Mark
033     */
034    public class LiferayFileItem extends DiskFileItem implements FileItem {
035    
036            public static final int THRESHOLD_SIZE = GetterUtil.getInteger(
037                    PropsUtil.get(LiferayFileItem.class.getName() + ".threshold.size"));
038    
039            public LiferayFileItem(
040                    String fieldName, String contentType, boolean isFormField,
041                    String fileName, int sizeThreshold, File repository) {
042    
043                    super(
044                            fieldName, contentType, isFormField, fileName, sizeThreshold,
045                            repository);
046    
047                    _fileName = fileName;
048                    _sizeThreshold = sizeThreshold;
049                    _repository = repository;
050            }
051    
052            public String getEncodedString() {
053                    return _encodedString;
054            }
055    
056            public String getFileName() {
057                    if (_fileName == null) {
058                            return null;
059                    }
060    
061                    int pos = _fileName.lastIndexOf("/");
062    
063                    if (pos == -1) {
064                            pos = _fileName.lastIndexOf("\\");
065                    }
066    
067                    if (pos == -1) {
068                            return _fileName;
069                    }
070                    else {
071                            return _fileName.substring(pos + 1, _fileName.length());
072                    }
073            }
074    
075            public String getFileNameExtension() {
076                    return FileUtil.getExtension(_fileName);
077            }
078    
079            public String getFullFileName() {
080                    return _fileName;
081            }
082    
083            public int getSizeThreshold() {
084                    return _sizeThreshold;
085            }
086    
087            @Override
088            public String getString() {
089    
090                    // Prevent serialization of uploaded content
091    
092                    if (getSize() > THRESHOLD_SIZE) {
093                            return StringPool.BLANK;
094                    }
095    
096                    if (_encodedString == null) {
097                            return super.getString();
098                    }
099                    else {
100                            return _encodedString;
101                    }
102            }
103    
104            public void setString(String encode) {
105                    try {
106                            _encodedString = getString(encode);
107                    }
108                    catch (Exception e) {
109                    }
110            }
111    
112            @Override
113            protected File getTempFile() {
114                    String tempFileName = "upload_" + _getUniqueId();
115    
116                    String extension = getFileNameExtension();
117    
118                    if (extension != null) {
119                            tempFileName += "." + extension;
120                    }
121    
122                    File tempFile = new File(_repository, tempFileName);
123    
124                    FinalizeManager.register(
125                            tempFile, new DeleteFileFinalizeAction(tempFile.getAbsolutePath()));
126    
127                    return tempFile;
128            }
129    
130            private static String _getUniqueId() {
131                    int current;
132    
133                    synchronized (LiferayFileItem.class) {
134                            current = _counter++;
135                    }
136    
137                    String id = String.valueOf(current);
138    
139                    if (current < 100000000) {
140                            id = ("00000000" + id).substring(id.length());
141                    }
142    
143                    return id;
144            }
145    
146            private static int _counter;
147    
148            private String _encodedString;
149            private String _fileName;
150            private File _repository;
151            private int _sizeThreshold;
152    
153    }