001    /**
002     * Copyright (c) 2000-2011 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.util.FileUtil;
020    import com.liferay.portal.kernel.util.GetterUtil;
021    import com.liferay.portal.kernel.util.StringPool;
022    import com.liferay.portal.util.PropsUtil;
023    
024    import java.io.File;
025    
026    import org.apache.commons.fileupload.disk.DiskFileItem;
027    
028    /**
029     * @author Brian Wing Shun Chan
030     * @author Zongliang Li
031     * @author Harry Mark
032     */
033    public class LiferayFileItem extends DiskFileItem {
034    
035            public static final int THRESHOLD_SIZE = GetterUtil.getInteger(
036                    PropsUtil.get(LiferayFileItem.class.getName() + ".threshold.size"));
037    
038            public LiferayFileItem(
039                    String fieldName, String contentType, boolean isFormField,
040                    String fileName, int sizeThreshold, File repository) {
041    
042                    super(
043                            fieldName, contentType, isFormField, fileName, sizeThreshold,
044                            repository);
045    
046                    _fileName = fileName;
047                    _sizeThreshold = sizeThreshold;
048                    _repository = repository;
049            }
050    
051            public String getEncodedString() {
052                    return _encodedString;
053            }
054    
055            public String getFileName() {
056                    if (_fileName == null) {
057                            return null;
058                    }
059    
060                    int pos = _fileName.lastIndexOf("/");
061    
062                    if (pos == -1) {
063                            pos = _fileName.lastIndexOf("\\");
064                    }
065    
066                    if (pos == -1) {
067                            return _fileName;
068                    }
069                    else {
070                            return _fileName.substring(pos + 1, _fileName.length());
071                    }
072            }
073    
074            public String getFileNameExtension() {
075                    return FileUtil.getExtension(_fileName);
076            }
077    
078            public String getFullFileName() {
079                    return _fileName;
080            }
081    
082            public int getSizeThreshold() {
083                    return _sizeThreshold;
084            }
085    
086            @Override
087            public String getString() {
088    
089                    // Prevent serialization of uploaded content
090    
091                    if (getSize() > THRESHOLD_SIZE) {
092                            return StringPool.BLANK;
093                    }
094    
095                    if (_encodedString == null) {
096                            return super.getString();
097                    }
098                    else {
099                            return _encodedString;
100                    }
101            }
102    
103            public void setString(String encode) {
104                    try {
105                            _encodedString = getString(encode);
106                    }
107                    catch (Exception e) {
108                    }
109            }
110    
111            @Override
112            protected File getTempFile() {
113                    String tempFileName = "upload_" + _getUniqueId();
114    
115                    String extension = getFileNameExtension();
116    
117                    if (extension != null) {
118                            tempFileName += "." + extension;
119                    }
120    
121                    File tempFile = new File(_repository, tempFileName);
122    
123                    FinalizeManager.register(
124                            tempFile, new DeleteFileFinalizeAction(tempFile.getAbsolutePath()));
125    
126                    return tempFile;
127            }
128    
129            private static String _getUniqueId() {
130                    int current;
131    
132                    synchronized (LiferayFileItem.class) {
133                            current = _counter++;
134                    }
135    
136                    String id = String.valueOf(current);
137    
138                    if (current < 100000000) {
139                            id = ("00000000" + id).substring(id.length());
140                    }
141    
142                    return id;
143            }
144    
145            private static int _counter;
146    
147            private String _encodedString;
148            private String _fileName;
149            private File _repository;
150            private int _sizeThreshold;
151    
152    }