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