/portal-service/src/com/liferay/portlet/documentlibrary/util/DLPreviewableProcessor.java

https://github.com/spreddy/liferay-portal · Java · 350 lines · 251 code · 83 blank · 16 comment · 14 complexity · a5bb5e783a506f47228a905ac0676b3c MD5 · raw file

  1. /**
  2. * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
  3. *
  4. * This library is free software; you can redistribute it and/or modify it under
  5. * the terms of the GNU Lesser General Public License as published by the Free
  6. * Software Foundation; either version 2.1 of the License, or (at your option)
  7. * any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  11. * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
  12. * details.
  13. */
  14. package com.liferay.portlet.documentlibrary.util;
  15. import com.liferay.portal.kernel.io.FileFilter;
  16. import com.liferay.portal.kernel.log.Log;
  17. import com.liferay.portal.kernel.log.LogFactoryUtil;
  18. import com.liferay.portal.kernel.repository.model.FileEntry;
  19. import com.liferay.portal.kernel.repository.model.FileVersion;
  20. import com.liferay.portal.kernel.util.StringBundler;
  21. import com.liferay.portal.kernel.util.StringPool;
  22. import com.liferay.portal.kernel.util.SystemProperties;
  23. import com.liferay.portal.model.CompanyConstants;
  24. import com.liferay.portal.util.PortalUtil;
  25. import com.liferay.portlet.documentlibrary.DuplicateDirectoryException;
  26. import com.liferay.portlet.documentlibrary.store.DLStoreUtil;
  27. import java.io.File;
  28. import java.io.InputStream;
  29. /**
  30. * @author Alexander Chow
  31. */
  32. public abstract class DLPreviewableProcessor implements DLProcessor {
  33. public static final String PREVIEW_PATH = "document_preview/";
  34. public static final String PREVIEW_TMP_PATH =
  35. SystemProperties.get(SystemProperties.TMP_DIR) +
  36. "/liferay/" + PREVIEW_PATH;
  37. public static final long REPOSITORY_ID = CompanyConstants.SYSTEM;
  38. public static final String THUMBNAIL_PATH = "document_thumbnail/";
  39. public static final String THUMBNAIL_TMP_PATH =
  40. SystemProperties.get(SystemProperties.TMP_DIR) +
  41. "/liferay/" + THUMBNAIL_PATH;
  42. public static void deleteFiles() {
  43. long[] companyIds = PortalUtil.getCompanyIds();
  44. for (long companyId : companyIds) {
  45. try {
  46. DLStoreUtil.deleteDirectory(
  47. companyId, REPOSITORY_ID, PREVIEW_PATH);
  48. }
  49. catch (Exception e) {
  50. }
  51. try {
  52. DLStoreUtil.deleteDirectory(
  53. companyId, REPOSITORY_ID, THUMBNAIL_PATH);
  54. }
  55. catch (Exception e) {
  56. }
  57. }
  58. }
  59. public static void deleteFiles(FileEntry fileEntry) {
  60. deleteFiles(
  61. fileEntry.getCompanyId(), fileEntry.getGroupId(),
  62. fileEntry.getFileEntryId(), -1);
  63. }
  64. public static void deleteFiles(FileVersion fileVersion) {
  65. deleteFiles(
  66. fileVersion.getCompanyId(), fileVersion.getGroupId(),
  67. fileVersion.getFileEntryId(), fileVersion.getFileVersionId());
  68. }
  69. public static void deleteFiles(
  70. long companyId, long groupId, long fileEntryId, long fileVersionId) {
  71. try {
  72. DLStoreUtil.deleteDirectory(
  73. companyId, REPOSITORY_ID,
  74. _getPathSegment(groupId, fileEntryId, fileVersionId, true));
  75. }
  76. catch (Exception e) {
  77. }
  78. try {
  79. DLStoreUtil.deleteDirectory(
  80. companyId, REPOSITORY_ID,
  81. _getPathSegment(groupId, fileEntryId, fileVersionId, false));
  82. }
  83. catch (Exception e) {
  84. }
  85. }
  86. protected void addFileToStore(
  87. long companyId, String dirName, String filePath, File srcFile)
  88. throws Exception {
  89. try {
  90. DLStoreUtil.addDirectory(companyId, REPOSITORY_ID, dirName);
  91. }
  92. catch (DuplicateDirectoryException dde) {
  93. }
  94. DLStoreUtil.addFile(companyId, REPOSITORY_ID, filePath, srcFile);
  95. }
  96. protected InputStream doGetPreviewAsStream(FileVersion fileVersion)
  97. throws Exception {
  98. return doGetPreviewAsStream(fileVersion, 0);
  99. }
  100. protected InputStream doGetPreviewAsStream(
  101. FileVersion fileVersion, int index)
  102. throws Exception {
  103. return DLStoreUtil.getFileAsStream(
  104. fileVersion.getCompanyId(), CompanyConstants.SYSTEM,
  105. getPreviewFilePath(fileVersion, index));
  106. }
  107. protected long doGetPreviewFileSize(FileVersion fileVersion)
  108. throws Exception {
  109. return doGetPreviewFileSize(fileVersion, 0);
  110. }
  111. protected long doGetPreviewFileSize(FileVersion fileVersion, int index)
  112. throws Exception {
  113. return DLStoreUtil.getFileSize(
  114. fileVersion.getCompanyId(), CompanyConstants.SYSTEM,
  115. getPreviewFilePath(fileVersion, index));
  116. }
  117. protected InputStream doGetThumbnailAsStream(FileVersion fileVersion)
  118. throws Exception {
  119. return doGetThumbnailAsStream(fileVersion, 0);
  120. }
  121. protected InputStream doGetThumbnailAsStream(
  122. FileVersion fileVersion, int index)
  123. throws Exception {
  124. return DLStoreUtil.getFileAsStream(
  125. fileVersion.getCompanyId(), CompanyConstants.SYSTEM,
  126. getThumbnailFilePath(fileVersion));
  127. }
  128. protected long doGetThumbnailFileSize(FileVersion fileVersion)
  129. throws Exception {
  130. return doGetThumbnailFileSize(fileVersion, 0);
  131. }
  132. protected long doGetThumbnailFileSize(FileVersion fileVersion, int index)
  133. throws Exception {
  134. return DLStoreUtil.getFileSize(
  135. fileVersion.getCompanyId(), CompanyConstants.SYSTEM,
  136. getThumbnailFilePath(fileVersion));
  137. }
  138. protected int getPreviewFileCount(FileVersion fileVersion)
  139. throws Exception {
  140. try {
  141. String[] fileNames = DLStoreUtil.getFileNames(
  142. fileVersion.getCompanyId(), REPOSITORY_ID,
  143. _getPathSegment(fileVersion, true));
  144. return fileNames.length;
  145. }
  146. catch (Exception e) {
  147. }
  148. return 0;
  149. }
  150. protected String getPreviewFilePath(FileVersion fileVersion) {
  151. return getPreviewFilePath(fileVersion, 0);
  152. }
  153. protected String getPreviewFilePath(FileVersion fileVersion, int index) {
  154. StringBundler sb = null;
  155. if (index > 0) {
  156. sb = new StringBundler(5);
  157. }
  158. else {
  159. sb = new StringBundler(3);
  160. }
  161. sb.append(_getPathSegment(fileVersion, true));
  162. if (index > 0) {
  163. sb.append(StringPool.SLASH);
  164. sb.append(index - 1);
  165. }
  166. sb.append(StringPool.PERIOD);
  167. sb.append(getPreviewType());
  168. return sb.toString();
  169. }
  170. protected File getPreviewTempFile(String tempFileId) {
  171. return getPreviewTempFile(tempFileId, 0);
  172. }
  173. protected File getPreviewTempFile(String tempFileId, int index) {
  174. String previewTempFilePath = getPreviewTempFilePath(tempFileId, index);
  175. return new File(previewTempFilePath);
  176. }
  177. protected int getPreviewTempFileCount(FileVersion fileVersion) {
  178. String tempFileId = DLUtil.getTempFileId(
  179. fileVersion.getFileEntryId(), fileVersion.getVersion());
  180. StringBundler sb = new StringBundler(5);
  181. sb.append(tempFileId);
  182. sb.append(StringPool.DASH);
  183. sb.append("(.*)");
  184. sb.append(StringPool.PERIOD);
  185. sb.append(getPreviewType());
  186. File dir = new File(PREVIEW_TMP_PATH);
  187. File[] files = dir.listFiles(new FileFilter(sb.toString()));
  188. if (_log.isDebugEnabled()) {
  189. for (File file : files) {
  190. _log.debug("Preview page for " + tempFileId + " " + file);
  191. }
  192. }
  193. return files.length;
  194. }
  195. protected String getPreviewTempFilePath(String id) {
  196. return getPreviewTempFilePath(id, 0);
  197. }
  198. protected String getPreviewTempFilePath(String id, int index) {
  199. StringBundler sb = null;
  200. if (index > 0) {
  201. sb = new StringBundler(6);
  202. }
  203. else {
  204. sb = new StringBundler(4);
  205. }
  206. sb.append(PREVIEW_TMP_PATH);
  207. sb.append(id);
  208. if (index > 0) {
  209. sb.append(StringPool.DASH);
  210. sb.append(index - 1);
  211. }
  212. sb.append(StringPool.PERIOD);
  213. sb.append(getPreviewType());
  214. return sb.toString();
  215. }
  216. protected abstract String getPreviewType();
  217. protected String getThumbnailFilePath(FileVersion fileVersion) {
  218. return _getPathSegment(fileVersion, false).concat(
  219. StringPool.PERIOD).concat(getThumbnailType());
  220. }
  221. protected File getThumbnailTempFile(String id) {
  222. String thumbnailTempFilePath = getThumbnailTempFilePath(id);
  223. return new File(thumbnailTempFilePath);
  224. }
  225. protected String getThumbnailTempFilePath(String id) {
  226. StringBundler sb = new StringBundler(4);
  227. sb.append(THUMBNAIL_TMP_PATH);
  228. sb.append(id);
  229. sb.append(StringPool.PERIOD);
  230. sb.append(getThumbnailType());
  231. return sb.toString();
  232. }
  233. protected String getThumbnailType() {
  234. return null;
  235. }
  236. private static String _getPathSegment(
  237. FileVersion fileVersion, boolean preview) {
  238. return _getPathSegment(
  239. fileVersion.getGroupId(), fileVersion.getFileEntryId(),
  240. fileVersion.getFileVersionId(), preview);
  241. }
  242. private static String _getPathSegment(
  243. long groupId, long fileEntryId, long fileVersionId, boolean preview) {
  244. StringBundler sb = null;
  245. if (fileVersionId > 0) {
  246. sb = new StringBundler(5);
  247. }
  248. else {
  249. sb = new StringBundler(3);
  250. }
  251. if (preview) {
  252. sb.append(PREVIEW_PATH);
  253. }
  254. else {
  255. sb.append(THUMBNAIL_PATH);
  256. }
  257. sb.append(groupId);
  258. sb.append(DLUtil.getDividedPath(fileEntryId));
  259. if (fileVersionId > 0) {
  260. sb.append(StringPool.SLASH);
  261. sb.append(fileVersionId);
  262. }
  263. return sb.toString();
  264. }
  265. private static Log _log = LogFactoryUtil.getLog(
  266. DLPreviewableProcessor.class);
  267. }