001    /**
002     * Copyright (c) 2000-2011 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.portlet.documentlibrary.util;
016    
017    import com.liferay.portal.kernel.io.FileFilter;
018    import com.liferay.portal.kernel.log.Log;
019    import com.liferay.portal.kernel.log.LogFactoryUtil;
020    import com.liferay.portal.kernel.repository.model.FileEntry;
021    import com.liferay.portal.kernel.repository.model.FileVersion;
022    import com.liferay.portal.kernel.util.StringBundler;
023    import com.liferay.portal.kernel.util.StringPool;
024    import com.liferay.portal.kernel.util.SystemProperties;
025    import com.liferay.portal.model.CompanyConstants;
026    import com.liferay.portal.util.PortalUtil;
027    import com.liferay.portlet.documentlibrary.DuplicateDirectoryException;
028    import com.liferay.portlet.documentlibrary.store.DLStoreUtil;
029    
030    import java.io.File;
031    import java.io.InputStream;
032    
033    /**
034     * @author Alexander Chow
035     */
036    public abstract class DLPreviewableProcessor implements DLProcessor {
037    
038            public static final String PREVIEW_PATH = "document_preview/";
039    
040            public static final String PREVIEW_TMP_PATH =
041                    SystemProperties.get(SystemProperties.TMP_DIR) +
042                            "/liferay/" + PREVIEW_PATH;
043    
044            public static final long REPOSITORY_ID = CompanyConstants.SYSTEM;
045    
046            public static final String THUMBNAIL_PATH = "document_thumbnail/";
047    
048            public static final String THUMBNAIL_TMP_PATH =
049                    SystemProperties.get(SystemProperties.TMP_DIR) +
050                            "/liferay/" + THUMBNAIL_PATH;
051    
052            public static void deleteFiles() {
053                    long[] companyIds = PortalUtil.getCompanyIds();
054    
055                    for (long companyId : companyIds) {
056                            try {
057                                    DLStoreUtil.deleteDirectory(
058                                            companyId, REPOSITORY_ID, PREVIEW_PATH);
059                            }
060                            catch (Exception e) {
061                            }
062    
063                            try {
064                                    DLStoreUtil.deleteDirectory(
065                                            companyId, REPOSITORY_ID, THUMBNAIL_PATH);
066                            }
067                            catch (Exception e) {
068                            }
069                    }
070            }
071    
072            public static void deleteFiles(FileEntry fileEntry) {
073                    deleteFiles(
074                            fileEntry.getCompanyId(), fileEntry.getGroupId(),
075                            fileEntry.getFileEntryId(), -1);
076            }
077    
078            public static void deleteFiles(FileVersion fileVersion) {
079                    deleteFiles(
080                            fileVersion.getCompanyId(), fileVersion.getGroupId(),
081                            fileVersion.getFileEntryId(), fileVersion.getFileVersionId());
082            }
083    
084            public static void deleteFiles(
085                    long companyId, long groupId, long fileEntryId, long fileVersionId) {
086    
087                    try {
088                            DLStoreUtil.deleteDirectory(
089                                    companyId, REPOSITORY_ID,
090                                    _getPathSegment(groupId, fileEntryId, fileVersionId, true));
091                    }
092                    catch (Exception e) {
093                    }
094    
095                    try {
096                            DLStoreUtil.deleteDirectory(
097                                    companyId, REPOSITORY_ID,
098                                    _getPathSegment(groupId, fileEntryId, fileVersionId, false));
099                    }
100                    catch (Exception e) {
101                    }
102            }
103    
104            protected void addFileToStore(
105                            long companyId, String dirName, String filePath, File srcFile)
106                    throws Exception {
107    
108                    try {
109                            DLStoreUtil.addDirectory(companyId, REPOSITORY_ID, dirName);
110                    }
111                    catch (DuplicateDirectoryException dde) {
112                    }
113    
114                    DLStoreUtil.addFile(companyId, REPOSITORY_ID, filePath, srcFile);
115            }
116    
117            protected InputStream doGetPreviewAsStream(FileVersion fileVersion)
118                    throws Exception {
119    
120                    return doGetPreviewAsStream(fileVersion, 0);
121            }
122    
123            protected InputStream doGetPreviewAsStream(
124                            FileVersion fileVersion, int index)
125                    throws Exception {
126    
127                    return DLStoreUtil.getFileAsStream(
128                            fileVersion.getCompanyId(), CompanyConstants.SYSTEM,
129                            getPreviewFilePath(fileVersion, index));
130            }
131    
132            protected long doGetPreviewFileSize(FileVersion fileVersion)
133                    throws Exception {
134    
135                    return doGetPreviewFileSize(fileVersion, 0);
136            }
137    
138            protected long doGetPreviewFileSize(FileVersion fileVersion, int index)
139                    throws Exception {
140    
141                    return DLStoreUtil.getFileSize(
142                            fileVersion.getCompanyId(), CompanyConstants.SYSTEM,
143                            getPreviewFilePath(fileVersion, index));
144            }
145    
146            protected InputStream doGetThumbnailAsStream(FileVersion fileVersion)
147                    throws Exception {
148    
149                    return doGetThumbnailAsStream(fileVersion, 0);
150            }
151    
152            protected InputStream doGetThumbnailAsStream(
153                            FileVersion fileVersion, int index)
154                    throws Exception {
155    
156                    return DLStoreUtil.getFileAsStream(
157                            fileVersion.getCompanyId(), CompanyConstants.SYSTEM,
158                            getThumbnailFilePath(fileVersion));
159            }
160    
161            protected long doGetThumbnailFileSize(FileVersion fileVersion)
162                    throws Exception {
163    
164                    return doGetThumbnailFileSize(fileVersion, 0);
165            }
166    
167            protected long doGetThumbnailFileSize(FileVersion fileVersion, int index)
168                    throws Exception {
169    
170                    return DLStoreUtil.getFileSize(
171                            fileVersion.getCompanyId(), CompanyConstants.SYSTEM,
172                            getThumbnailFilePath(fileVersion));
173            }
174    
175            protected int getPreviewFileCount(FileVersion fileVersion)
176                    throws Exception {
177    
178                    try {
179                            String[] fileNames = DLStoreUtil.getFileNames(
180                                    fileVersion.getCompanyId(), REPOSITORY_ID,
181                                    _getPathSegment(fileVersion, true));
182    
183                            return fileNames.length;
184                    }
185                    catch (Exception e) {
186                    }
187    
188                    return 0;
189            }
190    
191            protected String getPreviewFilePath(FileVersion fileVersion) {
192                    return getPreviewFilePath(fileVersion, 0);
193            }
194    
195            protected String getPreviewFilePath(FileVersion fileVersion, int index) {
196                    StringBundler sb = null;
197    
198                    if (index > 0) {
199                            sb = new StringBundler(5);
200                    }
201                    else {
202                            sb = new StringBundler(3);
203                    }
204    
205                    sb.append(_getPathSegment(fileVersion, true));
206    
207                    if (index > 0) {
208                            sb.append(StringPool.SLASH);
209                            sb.append(index - 1);
210                    }
211    
212                    sb.append(StringPool.PERIOD);
213                    sb.append(getPreviewType());
214    
215                    return sb.toString();
216            }
217    
218            protected File getPreviewTempFile(String tempFileId) {
219                    return getPreviewTempFile(tempFileId, 0);
220            }
221    
222            protected File getPreviewTempFile(String tempFileId, int index) {
223                    String previewTempFilePath = getPreviewTempFilePath(tempFileId, index);
224    
225                    return new File(previewTempFilePath);
226            }
227    
228            protected int getPreviewTempFileCount(FileVersion fileVersion) {
229                    String tempFileId = DLUtil.getTempFileId(
230                            fileVersion.getFileEntryId(), fileVersion.getVersion());
231    
232                    StringBundler sb = new StringBundler(5);
233    
234                    sb.append(tempFileId);
235                    sb.append(StringPool.DASH);
236                    sb.append("(.*)");
237                    sb.append(StringPool.PERIOD);
238                    sb.append(getPreviewType());
239    
240                    File dir = new File(PREVIEW_TMP_PATH);
241    
242                    File[] files = dir.listFiles(new FileFilter(sb.toString()));
243    
244                    if (_log.isDebugEnabled()) {
245                            for (File file : files) {
246                                    _log.debug("Preview page for " + tempFileId + " " + file);
247                            }
248                    }
249    
250                    return files.length;
251            }
252    
253            protected String getPreviewTempFilePath(String id) {
254                    return getPreviewTempFilePath(id, 0);
255            }
256    
257            protected String getPreviewTempFilePath(String id, int index) {
258                    StringBundler sb = null;
259    
260                    if (index > 0) {
261                            sb = new StringBundler(6);
262                    }
263                    else {
264                            sb = new StringBundler(4);
265                    }
266    
267                    sb.append(PREVIEW_TMP_PATH);
268                    sb.append(id);
269    
270                    if (index > 0) {
271                            sb.append(StringPool.DASH);
272                            sb.append(index - 1);
273                    }
274    
275                    sb.append(StringPool.PERIOD);
276                    sb.append(getPreviewType());
277    
278                    return sb.toString();
279            }
280    
281            protected abstract String getPreviewType();
282    
283            protected String getThumbnailFilePath(FileVersion fileVersion) {
284                    return _getPathSegment(fileVersion, false).concat(
285                            StringPool.PERIOD).concat(getThumbnailType());
286            }
287    
288            protected File getThumbnailTempFile(String id) {
289                    String thumbnailTempFilePath = getThumbnailTempFilePath(id);
290    
291                    return new File(thumbnailTempFilePath);
292            }
293    
294            protected String getThumbnailTempFilePath(String id) {
295                    StringBundler sb = new StringBundler(4);
296    
297                    sb.append(THUMBNAIL_TMP_PATH);
298                    sb.append(id);
299                    sb.append(StringPool.PERIOD);
300                    sb.append(getThumbnailType());
301    
302                    return sb.toString();
303            }
304    
305            protected String getThumbnailType() {
306                    return null;
307            }
308    
309            private static String _getPathSegment(
310                    FileVersion fileVersion, boolean preview) {
311    
312                    return _getPathSegment(
313                            fileVersion.getGroupId(), fileVersion.getFileEntryId(),
314                            fileVersion.getFileVersionId(), preview);
315            }
316    
317            private static String _getPathSegment(
318                    long groupId, long fileEntryId, long fileVersionId, boolean preview) {
319    
320                    StringBundler sb = null;
321    
322                    if (fileVersionId > 0) {
323                            sb = new StringBundler(5);
324                    }
325                    else {
326                            sb = new StringBundler(3);
327                    }
328    
329                    if (preview) {
330                            sb.append(PREVIEW_PATH);
331                    }
332                    else {
333                            sb.append(THUMBNAIL_PATH);
334                    }
335    
336                    sb.append(groupId);
337                    sb.append(DLUtil.getDividedPath(fileEntryId));
338    
339                    if (fileVersionId > 0) {
340                            sb.append(StringPool.SLASH);
341                            sb.append(fileVersionId);
342                    }
343    
344                    return sb.toString();
345            }
346    
347            private static Log _log = LogFactoryUtil.getLog(
348                    DLPreviewableProcessor.class);
349    
350    }