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 String addTempFile(
034 long userId, String tempPathName, File file)
035 throws PortalException, SystemException {
036
037 String tempFileName = FileUtil.createTempFileName();
038
039 return addTempFile(userId, tempFileName, tempPathName, file);
040 }
041
042 public static String addTempFile(
043 long userId, String fileName, String tempPathName, File file)
044 throws PortalException, SystemException {
045
046 String tempFileName = getTempFileName(userId, fileName, tempPathName);
047
048 DLStoreUtil.addFile(_COMPANY_ID, _REPOSITORY_ID, tempFileName, file);
049
050 return fileName;
051 }
052
053 public static String addTempFile(
054 long userId, String fileName, String tempPathName,
055 InputStream inputStream)
056 throws PortalException, SystemException {
057
058 File file = null;
059
060 if (inputStream instanceof ByteArrayFileInputStream) {
061 ByteArrayFileInputStream byteArrayFileInputStream =
062 (ByteArrayFileInputStream)inputStream;
063
064 file = byteArrayFileInputStream.getFile();
065 }
066
067 String tempFileName = getTempFileName(userId, fileName, tempPathName);
068
069 if (file != null) {
070 DLStoreUtil.addFile(
071 _COMPANY_ID, _REPOSITORY_ID, tempFileName, file);
072 }
073 else {
074 DLStoreUtil.addFile(
075 _COMPANY_ID, _REPOSITORY_ID, tempFileName, inputStream);
076 }
077
078 return fileName;
079 }
080
081 public static String addTempFile(String tempPathName, File file)
082 throws PortalException, SystemException {
083
084 return addTempFile(_USER_ID, tempPathName, file);
085 }
086
087 public static String addTempFile(
088 String fileName, String tempPathName, File file)
089 throws PortalException, SystemException {
090
091 return addTempFile(_USER_ID, fileName, tempPathName, file);
092 }
093
094 public static void deleteTempFile(
095 long userId, String fileName, String tempPathName)
096 throws PortalException, SystemException {
097
098 String tempFileName = getTempFileName(userId, fileName, tempPathName);
099
100 deleteTempFile(tempFileName);
101 }
102
103 public static void deleteTempFile(String tempFileName)
104 throws PortalException, SystemException {
105
106 DLStoreUtil.deleteFile(_COMPANY_ID, _REPOSITORY_ID, tempFileName);
107 }
108
109 public static void deleteTempFile(String fileName, String tempPathName)
110 throws PortalException, SystemException {
111
112 deleteTempFile(_USER_ID, fileName, tempPathName);
113 }
114
115 public static InputStream getTempFileAsStream(String tempFileName)
116 throws PortalException, SystemException {
117
118 return DLStoreUtil.getFileAsStream(
119 _COMPANY_ID, _REPOSITORY_ID, tempFileName);
120 }
121
122 public static String[] getTempFileEntryNames(
123 long userId, String tempPathName) {
124
125 try {
126 String tempFolderName = getTempFolderName(userId, tempPathName);
127
128 String[] fileNames = DLStoreUtil.getFileNames(
129 _COMPANY_ID, _REPOSITORY_ID, tempFolderName);
130
131 for (int i = 0; i < fileNames.length; i++) {
132 String fileName = StringUtil.extractLast(
133 fileNames[i], StringPool.SLASH);
134
135 fileName = StringUtil.replace(
136 fileName, _SUFFIX_TEMP_FILENAME, StringPool.BLANK);
137
138 fileNames[i] = fileName;
139 }
140
141 return fileNames;
142 }
143 catch (Exception e) {
144 if (_log.isDebugEnabled()) {
145 _log.debug(
146 "Unable to list temporary file names for " + userId +
147 " in " + tempPathName,
148 e);
149 }
150
151 return new String[0];
152 }
153 }
154
155 public static String[] getTempFileEntryNames(String tempPathName) {
156 return getTempFileEntryNames(_USER_ID, tempPathName);
157 }
158
159 public static String getTempFileName(
160 long userId, String fileName, String tempPathName)
161 throws PortalException {
162
163 validateFileName(fileName);
164
165 StringBundler sb = new StringBundler(3);
166
167 sb.append(getTempFolderName(userId, tempPathName));
168 sb.append(fileName);
169 sb.append(_SUFFIX_TEMP_FILENAME);
170
171 return sb.toString();
172 }
173
174 public static long getTempFileSize(String tempFileName)
175 throws PortalException, SystemException {
176
177 return DLStoreUtil.getFileSize(
178 _COMPANY_ID, _REPOSITORY_ID, tempFileName);
179 }
180
181 protected static String getTempFolderName(long userId, String tempPathName)
182 throws PortalException {
183
184 validatePathName(tempPathName);
185
186 StringBundler sb = new StringBundler(5);
187
188 sb.append(_BASE_TEMP_PATHNAME);
189 sb.append(tempPathName);
190 sb.append(StringPool.SLASH);
191 sb.append(userId);
192 sb.append(StringPool.SLASH);
193
194 return sb.toString();
195 }
196
197 protected static void validateFileName(String name) throws PortalException {
198 if ((name == null) || name.contains(StringPool.BACK_SLASH) ||
199 name.contains(StringPool.SLASH) ||
200 name.contains(File.pathSeparator) ||
201 (name.indexOf(_NULL_CHAR) > -1)) {
202
203 throw new TempFileNameException();
204 }
205 }
206
207 protected static void validatePathName(String pathName)
208 throws PortalException {
209
210 if (pathName == null) {
211 return;
212 }
213
214 if (pathName.indexOf(_NULL_CHAR) > -1) {
215 throw new TempFileNameException();
216 }
217
218 int pos = pathName.indexOf(StringPool.DOUBLE_PERIOD);
219
220 if (pos > -1) {
221 if (pathName.length() == 2) {
222 throw new TempFileNameException();
223 }
224
225 if (pos > 0) {
226 char c = pathName.charAt(pos - 1);
227
228 if ((c == CharPool.BACK_SLASH) || (c == CharPool.SLASH)) {
229 throw new TempFileNameException();
230 }
231 }
232
233 if ((pos + 2) < pathName.length()) {
234 char c = pathName.charAt(pos + 2);
235
236 if ((c == CharPool.BACK_SLASH) || (c == CharPool.SLASH)) {
237 throw new TempFileNameException();
238 }
239 }
240 }
241 }
242
243 private static final String _BASE_TEMP_PATHNAME = "liferay_temp/";
244
245 private static final long _COMPANY_ID = 0;
246
247 private static final char _NULL_CHAR = 0;
248
249 private static final long _REPOSITORY_ID = 0;
250
251 private static final String _SUFFIX_TEMP_FILENAME = "_temp.tmp";
252
253 private static final long _USER_ID = 0;
254
255 private static Log _log = LogFactoryUtil.getLog(TempFileUtil.class);
256
257 }