001
014
015 package com.liferay.portal.metadata;
016
017 import com.liferay.portal.kernel.exception.SystemException;
018 import com.liferay.portal.kernel.lar.PortletDataContext;
019 import com.liferay.portal.kernel.log.Log;
020 import com.liferay.portal.kernel.log.LogFactoryUtil;
021 import com.liferay.portal.kernel.repository.model.FileEntry;
022 import com.liferay.portal.kernel.util.FileUtil;
023 import com.liferay.portal.kernel.util.StringBundler;
024 import com.liferay.portal.kernel.util.StringPool;
025 import com.liferay.portal.kernel.util.Time;
026 import com.liferay.portal.kernel.xml.Element;
027 import com.liferay.portal.kernel.xuggler.XugglerUtil;
028 import com.liferay.portlet.documentlibrary.util.AudioProcessorUtil;
029 import com.liferay.portlet.documentlibrary.util.VideoProcessorUtil;
030
031 import com.xuggle.xuggler.IContainer;
032
033 import java.io.File;
034 import java.io.InputStream;
035
036 import java.text.DecimalFormat;
037
038 import org.apache.tika.metadata.Metadata;
039
040
044 public class XugglerRawMetadataProcessor extends BaseRawMetadataProcessor {
045
046 public void exportGeneratedFiles(
047 PortletDataContext portletDataContext, FileEntry fileEntry,
048 Element fileEntryElement)
049 throws Exception {
050
051 return;
052 }
053
054 public void importGeneratedFiles(
055 PortletDataContext portletDataContext, FileEntry fileEntry,
056 FileEntry importedFileEntry, Element fileEntryElement)
057 throws Exception {
058
059 return;
060 }
061
062 protected String convertTime(long microseconds) {
063 long milliseconds = microseconds / 1000L;
064
065 StringBundler sb = new StringBundler(7);
066
067 sb.append(_decimalFormatter.format(milliseconds / Time.HOUR));
068 sb.append(StringPool.COLON);
069 sb.append(
070 _decimalFormatter.format(milliseconds % Time.HOUR / Time.MINUTE));
071 sb.append(StringPool.COLON);
072 sb.append(
073 _decimalFormatter.format(milliseconds % Time.MINUTE / Time.SECOND));
074 sb.append(StringPool.PERIOD);
075 sb.append(_decimalFormatter.format(milliseconds % Time.SECOND / 10));
076
077 return sb.toString();
078 }
079
080 protected Metadata extractMetadata(File file) throws Exception {
081 IContainer container = IContainer.make();
082
083 try {
084 Metadata metadata = new Metadata();
085
086 if (container.open(
087 file.getCanonicalPath(), IContainer.Type.READ, null) < 0) {
088
089 throw new IllegalArgumentException("Could not open stream");
090 }
091
092 if (container.queryStreamMetaData() < 0) {
093 throw new IllegalStateException(
094 "Could not query stream metadata");
095 }
096
097 long microseconds = container.getDuration();
098
099 metadata.set(XMPDM.DURATION, convertTime(microseconds));
100
101 return metadata;
102 }
103 finally {
104 if (container.isOpened()) {
105 container.close();
106 }
107 }
108 }
109
110 @Override
111 @SuppressWarnings("unused")
112 protected Metadata extractMetadata(
113 String extension, String mimeType, File file)
114 throws SystemException {
115
116 Metadata metadata = null;
117
118 if (!isSupported(mimeType)) {
119 return metadata;
120 }
121
122 try {
123 metadata = extractMetadata(file);
124 }
125 catch (Exception e) {
126 _log.error(e, e);
127 }
128
129 return metadata;
130 }
131
132 @Override
133 @SuppressWarnings("unused")
134 protected Metadata extractMetadata(
135 String extension, String mimeType, InputStream inputStream)
136 throws SystemException {
137
138 Metadata metadata = null;
139
140 File file = null;
141
142 if (!isSupported(mimeType)) {
143 return metadata;
144 }
145
146 try {
147 file = FileUtil.createTempFile(extension);
148
149 FileUtil.write(file, inputStream);
150
151 metadata = extractMetadata(file);
152 }
153 catch (Exception e) {
154 _log.error(e, e);
155 }
156 finally {
157 FileUtil.delete(file);
158 }
159
160 return metadata;
161 }
162
163 protected boolean isSupported(String mimeType) {
164 if (XugglerUtil.isEnabled()) {
165 if (AudioProcessorUtil.isAudioSupported(mimeType)) {
166 return true;
167 }
168
169 if (VideoProcessorUtil.isVideoSupported(mimeType)) {
170 return true;
171 }
172 }
173
174 return false;
175 }
176
177 private static Log _log = LogFactoryUtil.getLog(
178 XugglerRawMetadataProcessor.class);
179
180 private static DecimalFormat _decimalFormatter = new DecimalFormat("00");
181
182 }