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