001    /**
002     * Copyright (c) 2000-2013 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            @Override
053            public String getEncodedString() {
054                    return _encodedString;
055            }
056    
057            @Override
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                    }
072                    else {
073                            return _fileName.substring(pos + 1);
074                    }
075            }
076    
077            @Override
078            public String getFileNameExtension() {
079                    return FileUtil.getExtension(_fileName);
080            }
081    
082            @Override
083            public String getFullFileName() {
084                    return _fileName;
085            }
086    
087            @Override
088            public int getSizeThreshold() {
089                    return _sizeThreshold;
090            }
091    
092            @Override
093            public String getString() {
094    
095                    // Prevent serialization of uploaded content
096    
097                    if (getSize() > THRESHOLD_SIZE) {
098                            return StringPool.BLANK;
099                    }
100    
101                    if (_encodedString == null) {
102                            return super.getString();
103                    }
104                    else {
105                            return _encodedString;
106                    }
107            }
108    
109            @Override
110            public void setString(String encode) {
111                    try {
112                            _encodedString = getString(encode);
113                    }
114                    catch (Exception e) {
115                    }
116            }
117    
118            @Override
119            protected File getTempFile() {
120                    String tempFileName = "upload_" + _getUniqueId();
121    
122                    String extension = getFileNameExtension();
123    
124                    if (extension != null) {
125                            tempFileName += "." + extension;
126                    }
127    
128                    File tempFile = new File(_repository, tempFileName);
129    
130                    FinalizeManager.register(
131                            tempFile, new DeleteFileFinalizeAction(tempFile.getAbsolutePath()));
132    
133                    return tempFile;
134            }
135    
136            private static String _getUniqueId() {
137                    int current;
138    
139                    synchronized (LiferayFileItem.class) {
140                            current = _counter++;
141                    }
142    
143                    String id = String.valueOf(current);
144    
145                    if (current < 100000000) {
146                            id = ("00000000" + id).substring(id.length());
147                    }
148    
149                    return id;
150            }
151    
152            private static int _counter;
153    
154            private String _encodedString;
155            private String _fileName;
156            private File _repository;
157            private int _sizeThreshold;
158    
159    }