001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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.DLProcessor;
029    import com.liferay.portlet.documentlibrary.util.VideoProcessor;
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    /**
041     * @author Juan González
042     * @author Alexander Chow
043     */
044    public class XugglerRawMetadataProcessor extends BaseRawMetadataProcessor {
045    
046            @Override
047            public Metadata extractMetadata(
048                            String extension, String mimeType, File file)
049                    throws SystemException {
050    
051                    Metadata metadata = null;
052    
053                    if (!isSupported(mimeType)) {
054                            return metadata;
055                    }
056    
057                    try {
058                            metadata = extractMetadata(file);
059                    }
060                    catch (Exception e) {
061                            _log.error(e, e);
062                    }
063    
064                    return metadata;
065            }
066    
067            @Override
068            public Metadata extractMetadata(
069                            String extension, String mimeType, InputStream inputStream)
070                    throws SystemException {
071    
072                    Metadata metadata = null;
073    
074                    File file = null;
075    
076                    if (!isSupported(mimeType)) {
077                            return metadata;
078                    }
079    
080                    try {
081                            file = FileUtil.createTempFile(extension);
082    
083                            FileUtil.write(file, inputStream);
084    
085                            metadata = extractMetadata(file);
086                    }
087                    catch (Exception e) {
088                            _log.error(e, e);
089                    }
090                    finally {
091                            FileUtil.delete(file);
092                    }
093    
094                    return metadata;
095            }
096    
097            protected String convertTime(long microseconds) {
098                    long milliseconds = microseconds / 1000L;
099    
100                    StringBundler sb = new StringBundler(7);
101    
102                    sb.append(_decimalFormatter.format(milliseconds / Time.HOUR));
103                    sb.append(StringPool.COLON);
104                    sb.append(
105                            _decimalFormatter.format(milliseconds % Time.HOUR / Time.MINUTE));
106                    sb.append(StringPool.COLON);
107                    sb.append(
108                            _decimalFormatter.format(milliseconds % Time.MINUTE / Time.SECOND));
109                    sb.append(StringPool.PERIOD);
110                    sb.append(_decimalFormatter.format(milliseconds % Time.SECOND / 10));
111    
112                    return sb.toString();
113            }
114    
115            protected Metadata extractMetadata(File file) throws Exception {
116                    IContainer container = IContainer.make();
117    
118                    try {
119                            Metadata metadata = new Metadata();
120    
121                            if (container.open(
122                                            file.getCanonicalPath(), IContainer.Type.READ, null) < 0) {
123    
124                                    throw new IllegalArgumentException("Could not open stream");
125                            }
126    
127                            if (container.queryStreamMetaData() < 0) {
128                                    throw new IllegalStateException(
129                                            "Could not query stream metadata");
130                            }
131    
132                            long microseconds = container.getDuration();
133    
134                            metadata.set(XMPDM.DURATION, convertTime(microseconds));
135    
136                            return metadata;
137                    }
138                    finally {
139                            if (container.isOpened()) {
140                                    container.close();
141                            }
142                    }
143            }
144    
145            protected boolean isSupported(String mimeType) throws SystemException {
146                    if (PrefsPropsUtil.getBoolean(
147                                    PropsKeys.XUGGLER_ENABLED, PropsValues.XUGGLER_ENABLED)) {
148    
149                            DLProcessor audioDLProcessor = AudioProcessor.getInstance();
150    
151                            if (audioDLProcessor.isSupported(mimeType)) {
152                                    return true;
153                            }
154    
155                            DLProcessor videoDLProcessor = VideoProcessor.getInstance();
156    
157                            if (videoDLProcessor.isSupported(mimeType)) {
158                                    return true;
159                            }
160                    }
161    
162                    return false;
163            }
164    
165            private static Log _log = LogFactoryUtil.getLog(
166                    XugglerRawMetadataProcessor.class);
167    
168            private static DecimalFormat _decimalFormatter = new DecimalFormat("00");
169    
170    }