001
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
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 }