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.kernel.util;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.kernel.io.ByteArrayFileInputStream;
020    import com.liferay.portal.kernel.io.FileFilter;
021    import com.liferay.portlet.documentlibrary.store.DLStoreUtil;
022    
023    import java.io.File;
024    import java.io.IOException;
025    import java.io.InputStream;
026    
027    /**
028     * @author Sergio González
029     */
030    public class TempFileUtil {
031    
032            public static String addTempFile(
033                            long userId, String tempPathName, File file)
034                    throws IOException, PortalException, SystemException {
035    
036                    String tempFileName = FileUtil.createTempFileName();
037    
038                    DLStoreUtil.validate(tempFileName, true, file);
039    
040                    File tempFile = getTempFile(tempFileName, tempPathName);
041    
042                    FileUtil.copyFile(file, tempFile);
043    
044                    return tempFileName;
045            }
046    
047            public static String addTempFile(
048                            long userId, String fileName, String tempPathName, File file)
049                    throws IOException, PortalException, SystemException {
050    
051                    DLStoreUtil.validate(fileName, true, file);
052    
053                    File tempFile = getTempFile(userId, fileName, tempPathName);
054    
055                    FileUtil.copyFile(file, tempFile);
056    
057                    return fileName;
058            }
059    
060            public static String addTempFile(
061                            long userId, String fileName, String tempPathName,
062                            InputStream inputStream)
063                    throws IOException, PortalException, SystemException {
064    
065                    File file = null;
066    
067                    if (inputStream instanceof ByteArrayFileInputStream) {
068                            ByteArrayFileInputStream byteArrayFileInputStream =
069                                    (ByteArrayFileInputStream)inputStream;
070    
071                            file = byteArrayFileInputStream.getFile();
072    
073                            DLStoreUtil.validate(fileName, true, file);
074                    }
075                    else {
076                            DLStoreUtil.validate(fileName, true, inputStream);
077                    }
078    
079                    File tempFile = getTempFile(userId, fileName, tempPathName);
080    
081                    if (file != null) {
082                            FileUtil.copyFile(file, tempFile);
083                    }
084                    else {
085                            FileUtil.write(tempFile, inputStream);
086                    }
087    
088                    return fileName;
089            }
090    
091            public static String addTempFile(String tempPathName, File file)
092                    throws IOException, PortalException, SystemException {
093    
094                    String tempFileName = FileUtil.createTempFileName();
095    
096                    DLStoreUtil.validate(tempFileName, false, file);
097    
098                    File tempFile = getTempFile(tempFileName, tempPathName);
099    
100                    FileUtil.copyFile(file, tempFile);
101    
102                    return tempFileName;
103            }
104    
105            public static String addTempFile(
106                            String fileName, String tempPathName, File file)
107                    throws IOException, PortalException, SystemException {
108    
109                    DLStoreUtil.validate(fileName, true, file);
110    
111                    File tempFile = getTempFile(fileName, tempPathName);
112    
113                    FileUtil.copyFile(file, tempFile);
114    
115                    return fileName;
116            }
117    
118            public static void deleteTempFile(
119                    long userId, String fileName, String tempPathName) {
120    
121                    File file = getTempFile(userId, fileName, tempPathName);
122    
123                    FileUtil.delete(file);
124            }
125    
126            public static void deleteTempFile(String fileName, String tempPathName) {
127                    File file = getTempFile(fileName, tempPathName);
128    
129                    FileUtil.delete(file);
130            }
131    
132            public static File getTempFile(
133                    long userId, String fileName, String tempPathName) {
134    
135                    String absoluteFilePath = _getTempAbsolutePath(
136                            userId, fileName, tempPathName);
137    
138                    return new File(absoluteFilePath);
139            }
140    
141            public static File getTempFile(String fileName, String tempPathName) {
142                    String absoluteFilePath = _getTempAbsolutePath(fileName, tempPathName);
143    
144                    return new File(absoluteFilePath);
145            }
146    
147            public static String[] getTempFileEntryNames(
148                    long userId, String tempPathName) {
149    
150                    File dir = new File(_getTempAbsolutePath(tempPathName));
151    
152                    StringBundler sb = new StringBundler(5);
153    
154                    sb.append(StringPool.PERIOD);
155                    sb.append(StringPool.STAR);
156                    sb.append(StringPool.UNDERLINE);
157                    sb.append(userId);
158                    sb.append(_SUFFIX_TEMP_FILENAME_USERID_REGEX);
159    
160                    FileFilter fileFilter = new FileFilter(sb.toString());
161    
162                    File[] files = dir.listFiles(fileFilter);
163    
164                    int count = 0;
165    
166                    if (files != null) {
167                            count = files.length;
168                    }
169    
170                    String[] fileNames = new String[count];
171    
172                    for (int i = 0; i < count; i++) {
173                            File file = files[i];
174    
175                            String fileName = StringUtil.replace(
176                                    file.getName(),
177                                    StringPool.UNDERLINE + userId + _SUFFIX_TEMP_FILENAME,
178                                    StringPool.BLANK);
179    
180                            fileNames[i] = fileName;
181                    }
182    
183                    return fileNames;
184            }
185    
186            public static String[] getTempFileEntryNames(String tempPathName) {
187                    File dir = new File(_getTempAbsolutePath(tempPathName));
188    
189                    File[] files = dir.listFiles(
190                            new FileFilter(_SUFFIX_TEMP_FILENAME_REGEX));
191    
192                    String[] fileNames = new String[files.length];
193    
194                    for (int i = 0; i < files.length; i++) {
195                            File file = files[i];
196    
197                            String fileName = StringUtil.replace(
198                                    file.getName(), _SUFFIX_TEMP_FILENAME, StringPool.BLANK);
199    
200                            fileNames[i] = fileName;
201                    }
202    
203                    return fileNames;
204            }
205    
206            private static String _getTempAbsolutePath(
207                    long userId, String fileName, String tempPathName) {
208    
209                    StringBundler sb = new StringBundler(5);
210    
211                    sb.append(SystemProperties.get(SystemProperties.TMP_DIR));
212                    sb.append(_BASE_TEMP_PATHNAME);
213                    sb.append(tempPathName);
214                    sb.append(StringPool.SLASH);
215                    sb.append(_getTempFileName(userId, fileName));
216    
217                    return sb.toString();
218            }
219    
220            private static String _getTempAbsolutePath(String tempPathName) {
221                    StringBundler sb = new StringBundler(4);
222    
223                    sb.append(SystemProperties.get(SystemProperties.TMP_DIR));
224                    sb.append(_BASE_TEMP_PATHNAME);
225                    sb.append(tempPathName);
226                    sb.append(StringPool.SLASH);
227    
228                    return sb.toString();
229            }
230    
231            private static String _getTempAbsolutePath(
232                    String fileName, String tempPathName) {
233    
234                    StringBundler sb = new StringBundler(5);
235    
236                    sb.append(SystemProperties.get(SystemProperties.TMP_DIR));
237                    sb.append(_BASE_TEMP_PATHNAME);
238                    sb.append(tempPathName);
239                    sb.append(StringPool.SLASH);
240                    sb.append(_getTempFileName(fileName));
241    
242                    return sb.toString();
243            }
244    
245            private static String _getTempFileName(long userId, String fileName) {
246                    StringBundler sb = new StringBundler(4);
247    
248                    sb.append(fileName);
249                    sb.append(StringPool.UNDERLINE);
250                    sb.append(userId);
251                    sb.append(_SUFFIX_TEMP_FILENAME);
252    
253                    return sb.toString();
254            }
255    
256            private static String _getTempFileName(String fileName) {
257                    return fileName + _SUFFIX_TEMP_FILENAME;
258            }
259    
260            private static final String _BASE_TEMP_PATHNAME = "/liferay/";
261    
262            private static final String _SUFFIX_TEMP_FILENAME = "_temp.tmp";
263    
264            private static final String _SUFFIX_TEMP_FILENAME_REGEX = ".*_temp\\.tmp";
265    
266            private static final String _SUFFIX_TEMP_FILENAME_USERID_REGEX =
267                    "_temp\\.tmp";
268    
269    }