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.portlet.documentlibrary.util;
016    
017    import com.liferay.portal.kernel.repository.model.FileVersion;
018    import com.liferay.portal.kernel.util.ContentTypes;
019    import com.liferay.portal.kernel.util.FileUtil;
020    import com.liferay.portal.kernel.util.GetterUtil;
021    import com.liferay.portal.kernel.util.MimeTypesUtil;
022    import com.liferay.portal.kernel.util.Validator;
023    
024    import java.io.File;
025    import java.io.InputStream;
026    
027    /**
028     * @author Alexander Chow
029     */
030    public class DLAppUtil {
031    
032            public static String getExtension(String title, String sourceFileName) {
033                    String extension = FileUtil.getExtension(sourceFileName);
034    
035                    if (Validator.isNull(extension)) {
036                            extension = FileUtil.getExtension(title);
037                    }
038    
039                    return extension;
040            }
041    
042            public static String getMimeType(
043                    String sourceFileName, String mimeType, String title, File file,
044                    InputStream is) {
045    
046                    if (Validator.isNull(mimeType) ||
047                            !mimeType.equals(ContentTypes.APPLICATION_OCTET_STREAM)) {
048    
049                            return mimeType;
050                    }
051    
052                    if (Validator.isNull(title)) {
053                            title = sourceFileName;
054                    }
055    
056                    String extension = getExtension(title, sourceFileName);
057    
058                    String titleWithExtension = DLUtil.getTitleWithExtension(
059                            title, extension);
060    
061                    if (file != null) {
062                            mimeType = MimeTypesUtil.getContentType(file, titleWithExtension);
063                    }
064                    else {
065                            mimeType = MimeTypesUtil.getContentType(is, titleWithExtension);
066                    }
067    
068                    return mimeType;
069            }
070    
071            public static boolean isMajorVersion(
072                    FileVersion previousFileVersion, FileVersion currentFileVersion) {
073    
074                    long currentVersion = GetterUtil.getLong(
075                            currentFileVersion.getVersion());
076                    long previousVersion = GetterUtil.getLong(
077                            previousFileVersion.getVersion());
078    
079                    return (currentVersion - previousVersion) >= 1;
080            }
081    
082    }