001
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.log.Log;
021 import com.liferay.portal.kernel.log.LogFactoryUtil;
022 import com.liferay.portlet.documentlibrary.store.DLStoreUtil;
023
024 import java.io.File;
025 import java.io.InputStream;
026
027
031 public class TempFileUtil {
032
033 public static final String SUFFIX_TEMP_FILE_NAME = "_temp.tmp";
034
035 public static String addTempFile(
036 long userId, String tempPathName, File file)
037 throws PortalException, SystemException {
038
039 String tempFileName = FileUtil.createTempFileName();
040
041 return addTempFile(userId, tempFileName, tempPathName, file);
042 }
043
044 public static String addTempFile(
045 long userId, String fileName, String tempPathName, File file)
046 throws PortalException, SystemException {
047
048 String tempFileName = getTempFileName(userId, fileName, tempPathName);
049
050 DLStoreUtil.addFile(_COMPANY_ID, _REPOSITORY_ID, tempFileName, file);
051
052 return fileName;
053 }
054
055 public static String addTempFile(
056 long userId, String fileName, String tempPathName,
057 InputStream inputStream)
058 throws PortalException, SystemException {
059
060 File file = null;
061
062 if (inputStream instanceof ByteArrayFileInputStream) {
063 ByteArrayFileInputStream byteArrayFileInputStream =
064 (ByteArrayFileInputStream)inputStream;
065
066 file = byteArrayFileInputStream.getFile();
067 }
068
069 String tempFileName = getTempFileName(userId, fileName, tempPathName);
070
071 if (file != null) {
072 DLStoreUtil.addFile(
073 _COMPANY_ID, _REPOSITORY_ID, tempFileName, file);
074 }
075 else {
076 DLStoreUtil.addFile(
077 _COMPANY_ID, _REPOSITORY_ID, tempFileName, inputStream);
078 }
079
080 return fileName;
081 }
082
083 public static String addTempFile(String tempPathName, File file)
084 throws PortalException, SystemException {
085
086 return addTempFile(_USER_ID, tempPathName, file);
087 }
088
089 public static String addTempFile(
090 String fileName, String tempPathName, File file)
091 throws PortalException, SystemException {
092
093 return addTempFile(_USER_ID, fileName, tempPathName, file);
094 }
095
096 public static void deleteTempFile(
097 long userId, String fileName, String tempPathName)
098 throws PortalException, SystemException {
099
100 String tempFileName = getTempFileName(userId, fileName, tempPathName);
101
102 deleteTempFile(tempFileName);
103 }
104
105 public static void deleteTempFile(String tempFileName)
106 throws PortalException, SystemException {
107
108 DLStoreUtil.deleteFile(_COMPANY_ID, _REPOSITORY_ID, tempFileName);
109 }
110
111 public static void deleteTempFile(String fileName, String tempPathName)
112 throws PortalException, SystemException {
113
114 deleteTempFile(_USER_ID, fileName, tempPathName);
115 }
116
117 public static InputStream getTempFileAsStream(String tempFileName)
118 throws PortalException, SystemException {
119
120 return DLStoreUtil.getFileAsStream(
121 _COMPANY_ID, _REPOSITORY_ID, tempFileName);
122 }
123
124 public static String[] getTempFileEntryNames(
125 long userId, String tempPathName) {
126
127 try {
128 String tempFolderName = getTempFolderName(userId, tempPathName);
129
130 String[] fileNames = DLStoreUtil.getFileNames(
131 _COMPANY_ID, _REPOSITORY_ID, tempFolderName);
132
133 for (int i = 0; i < fileNames.length; i++) {
134 String fileName = StringUtil.extractLast(
135 fileNames[i], StringPool.SLASH);
136
137 fileName = StringUtil.replace(
138 fileName, SUFFIX_TEMP_FILE_NAME, StringPool.BLANK);
139
140 fileNames[i] = fileName;
141 }
142
143 return fileNames;
144 }
145 catch (Exception e) {
146 if (_log.isDebugEnabled()) {
147 _log.debug(
148 "Unable to list temporary file names for " + userId +
149 " in " + tempPathName,
150 e);
151 }
152
153 return new String[0];
154 }
155 }
156
157 public static String[] getTempFileEntryNames(String tempPathName) {
158 return getTempFileEntryNames(_USER_ID, tempPathName);
159 }
160
161 public static String getTempFileName(
162 long userId, String fileName, String tempPathName)
163 throws PortalException {
164
165 if (!Validator.isFileName(fileName)) {
166 throw new TempFileNameException();
167 }
168
169 StringBundler sb = new StringBundler(3);
170
171 sb.append(getTempFolderName(userId, tempPathName));
172 sb.append(fileName);
173 sb.append(SUFFIX_TEMP_FILE_NAME);
174
175 return sb.toString();
176 }
177
178 public static long getTempFileSize(String tempFileName)
179 throws PortalException, SystemException {
180
181 return DLStoreUtil.getFileSize(
182 _COMPANY_ID, _REPOSITORY_ID, tempFileName);
183 }
184
185 protected static String getTempFolderName(long userId, String tempPathName)
186 throws PortalException {
187
188 if (!Validator.isFilePath(tempPathName, false)) {
189 throw new TempFileNameException();
190 }
191
192 StringBundler sb = new StringBundler(5);
193
194 sb.append(_BASE_TEMP_PATHNAME);
195 sb.append(tempPathName);
196 sb.append(StringPool.SLASH);
197 sb.append(userId);
198 sb.append(StringPool.SLASH);
199
200 return sb.toString();
201 }
202
203 private static final String _BASE_TEMP_PATHNAME = "liferay_temp/";
204
205 private static final long _COMPANY_ID = 0;
206
207 private static final long _REPOSITORY_ID = 0;
208
209 private static final long _USER_ID = 0;
210
211 private static Log _log = LogFactoryUtil.getLog(TempFileUtil.class);
212
213 }