001    /**
002     * Copyright (c) 2000-2013 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.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    /**
041     * @author Juan Gonz??lez
042     * @author Alexander Chow
043     */
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            @SuppressWarnings("unused")
114            protected Metadata extractMetadata(
115                            String extension, String mimeType, File file)
116                    throws SystemException {
117    
118                    Metadata metadata = null;
119    
120                    if (!isSupported(mimeType)) {
121                            return metadata;
122                    }
123    
124                    try {
125                            metadata = extractMetadata(file);
126                    }
127                    catch (Exception e) {
128                            _log.error(e, e);
129                    }
130    
131                    return metadata;
132            }
133    
134            @Override
135            @SuppressWarnings("unused")
136            protected Metadata extractMetadata(
137                            String extension, String mimeType, InputStream inputStream)
138                    throws SystemException {
139    
140                    Metadata metadata = null;
141    
142                    File file = null;
143    
144                    if (!isSupported(mimeType)) {
145                            return metadata;
146                    }
147    
148                    try {
149                            file = FileUtil.createTempFile(extension);
150    
151                            FileUtil.write(file, inputStream);
152    
153                            metadata = extractMetadata(file);
154                    }
155                    catch (Exception e) {
156                            _log.error(e, e);
157                    }
158                    finally {
159                            FileUtil.delete(file);
160                    }
161    
162                    return metadata;
163            }
164    
165            protected boolean isSupported(String mimeType) {
166                    if (XugglerUtil.isEnabled()) {
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    }