001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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, String thumbnailType) {
073                    deleteFiles(
074                            fileEntry.getCompanyId(), fileEntry.getRepositoryId(),
075                            fileEntry.getFileEntryId(), -1, thumbnailType);
076            }
077    
078            public static void deleteFiles(
079                    FileVersion fileVersion, String thumbnailType) {
080    
081                    deleteFiles(
082                            fileVersion.getCompanyId(), fileVersion.getRepositoryId(),
083                            fileVersion.getFileEntryId(), fileVersion.getFileVersionId(),
084                            thumbnailType);
085            }
086    
087            public boolean isSupported(FileVersion fileVersion) {
088                    if (fileVersion == null) {
089                            return false;
090                    }
091    
092                    return isSupported(fileVersion.getMimeType());
093            }
094    
095            protected static void deleteFiles(
096                    long companyId, long groupId, long fileEntryId, long fileVersionId,
097                    String thumbnailType) {
098    
099                    try {
100                            DLStoreUtil.deleteDirectory(
101                                    companyId, REPOSITORY_ID,
102                                    getPathSegment(groupId, fileEntryId, fileVersionId, true));
103                    }
104                    catch (Exception e) {
105                    }
106    
107                    try {
108                            String dirName = getPathSegment(
109                                    groupId, fileEntryId, fileVersionId, false);
110    
111                            if (fileVersionId > 0) {
112                                    dirName = dirName.concat(StringPool.PERIOD);
113                                    dirName = dirName.concat(thumbnailType);
114                            }
115    
116                            DLStoreUtil.deleteDirectory(companyId, REPOSITORY_ID, dirName);
117                    }
118                    catch (Exception e) {
119                    }
120            }
121    
122            protected static String getPathSegment(
123                    FileVersion fileVersion, boolean preview) {
124    
125                    return getPathSegment(
126                            fileVersion.getGroupId(), fileVersion.getFileEntryId(),
127                            fileVersion.getFileVersionId(), preview);
128            }
129    
130            protected static String getPathSegment(
131                    long groupId, long fileEntryId, long fileVersionId, boolean preview) {
132    
133                    StringBundler sb = null;
134    
135                    if (fileVersionId > 0) {
136                            sb = new StringBundler(5);
137                    }
138                    else {
139                            sb = new StringBundler(3);
140                    }
141    
142                    if (preview) {
143                            sb.append(PREVIEW_PATH);
144                    }
145                    else {
146                            sb.append(THUMBNAIL_PATH);
147                    }
148    
149                    sb.append(groupId);
150                    sb.append(DLUtil.getDividedPath(fileEntryId));
151    
152                    if (fileVersionId > 0) {
153                            sb.append(StringPool.SLASH);
154                            sb.append(fileVersionId);
155                    }
156    
157                    return sb.toString();
158            }
159    
160            protected void addFileToStore(
161                            long companyId, String dirName, String filePath, File srcFile)
162                    throws Exception {
163    
164                    try {
165                            DLStoreUtil.addDirectory(companyId, REPOSITORY_ID, dirName);
166                    }
167                    catch (DuplicateDirectoryException dde) {
168                    }
169    
170                    DLStoreUtil.addFile(companyId, REPOSITORY_ID, filePath, srcFile);
171            }
172    
173            protected InputStream doGetPreviewAsStream(
174                            FileVersion fileVersion, int index, String type)
175                    throws Exception {
176    
177                    return DLStoreUtil.getFileAsStream(
178                            fileVersion.getCompanyId(), CompanyConstants.SYSTEM,
179                            getPreviewFilePath(fileVersion, index, type));
180            }
181    
182            protected InputStream doGetPreviewAsStream(
183                            FileVersion fileVersion, String type)
184                    throws Exception {
185    
186                    return doGetPreviewAsStream(fileVersion, 0, type);
187            }
188    
189            protected int doGetPreviewFileCount(FileVersion fileVersion)
190                    throws Exception {
191    
192                    try {
193                            String[] fileNames = DLStoreUtil.getFileNames(
194                                    fileVersion.getCompanyId(), REPOSITORY_ID,
195                                    getPathSegment(fileVersion, true));
196    
197                            return fileNames.length;
198                    }
199                    catch (Exception e) {
200                    }
201    
202                    return 0;
203            }
204    
205            protected long doGetPreviewFileSize(
206                            FileVersion fileVersion, int index, String type)
207                    throws Exception {
208    
209                    return DLStoreUtil.getFileSize(
210                            fileVersion.getCompanyId(), CompanyConstants.SYSTEM,
211                            getPreviewFilePath(fileVersion, index, type));
212            }
213    
214            protected long doGetPreviewFileSize(FileVersion fileVersion, String type)
215                    throws Exception {
216    
217                    return doGetPreviewFileSize(fileVersion, 0, type);
218            }
219    
220            protected InputStream doGetThumbnailAsStream(
221                            FileVersion fileVersion, String type)
222                    throws Exception {
223    
224                    return DLStoreUtil.getFileAsStream(
225                            fileVersion.getCompanyId(), CompanyConstants.SYSTEM,
226                            getThumbnailFilePath(fileVersion, type));
227            }
228    
229            protected long doGetThumbnailFileSize(FileVersion fileVersion, String type)
230                    throws Exception {
231    
232                    return DLStoreUtil.getFileSize(
233                            fileVersion.getCompanyId(), CompanyConstants.SYSTEM,
234                            getThumbnailFilePath(fileVersion, type));
235            }
236    
237            protected String getPreviewFilePath(
238                    FileVersion fileVersion, int index, String type) {
239    
240                    StringBundler sb = null;
241    
242                    if (index > 0) {
243                            sb = new StringBundler(5);
244                    }
245                    else {
246                            sb = new StringBundler(3);
247                    }
248    
249                    sb.append(getPathSegment(fileVersion, true));
250    
251                    if (index > 0) {
252                            sb.append(StringPool.SLASH);
253                            sb.append(index - 1);
254                    }
255    
256                    sb.append(StringPool.PERIOD);
257                    sb.append(type);
258    
259                    return sb.toString();
260            }
261    
262            protected String getPreviewFilePath(FileVersion fileVersion, String type) {
263                    return getPreviewFilePath(fileVersion, 0, type);
264            }
265    
266            protected File getPreviewTempFile(String id, int index, String type) {
267                    String previewTempFilePath = getPreviewTempFilePath(id, index, type);
268    
269                    return new File(previewTempFilePath);
270            }
271    
272            protected File getPreviewTempFile(String id, String type) {
273                    return getPreviewTempFile(id, 0, type);
274            }
275    
276            protected int getPreviewTempFileCount(
277                    FileVersion fileVersion, String type) {
278    
279                    String tempFileId = DLUtil.getTempFileId(
280                            fileVersion.getFileEntryId(), fileVersion.getVersion());
281    
282                    StringBundler sb = new StringBundler(5);
283    
284                    sb.append(tempFileId);
285                    sb.append(StringPool.DASH);
286                    sb.append("(.*)");
287                    sb.append(StringPool.PERIOD);
288                    sb.append(type);
289    
290                    File dir = new File(PREVIEW_TMP_PATH);
291    
292                    File[] files = dir.listFiles(new FileFilter(sb.toString()));
293    
294                    if (_log.isDebugEnabled()) {
295                            for (File file : files) {
296                                    _log.debug("Preview page for " + tempFileId + " " + file);
297                            }
298                    }
299    
300                    return files.length;
301            }
302    
303            protected String getPreviewTempFilePath(String id, int index, String type) {
304                    StringBundler sb = null;
305    
306                    if (index > 0) {
307                            sb = new StringBundler(6);
308                    }
309                    else {
310                            sb = new StringBundler(4);
311                    }
312    
313                    sb.append(PREVIEW_TMP_PATH);
314                    sb.append(id);
315    
316                    if (index > 0) {
317                            sb.append(StringPool.DASH);
318                            sb.append(index - 1);
319                    }
320    
321                    sb.append(StringPool.PERIOD);
322                    sb.append(type);
323    
324                    return sb.toString();
325            }
326    
327            protected String getPreviewTempFilePath(String id, String type) {
328                    return getPreviewTempFilePath(id, 0, type);
329            }
330    
331            protected String getThumbnailFilePath(
332                    FileVersion fileVersion, String type) {
333    
334                    return getPathSegment(fileVersion, false).concat(
335                            StringPool.PERIOD).concat(type);
336            }
337    
338            protected File getThumbnailTempFile(String id, String type) {
339                    String thumbnailTempFilePath = getThumbnailTempFilePath(id, type);
340    
341                    return new File(thumbnailTempFilePath);
342            }
343    
344            protected String getThumbnailTempFilePath(String id, String type) {
345                    StringBundler sb = new StringBundler(4);
346    
347                    sb.append(THUMBNAIL_TMP_PATH);
348                    sb.append(id);
349                    sb.append(StringPool.PERIOD);
350                    sb.append(type);
351    
352                    return sb.toString();
353            }
354    
355            private static Log _log = LogFactoryUtil.getLog(
356                    DLPreviewableProcessor.class);
357    
358    }