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