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