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