001 /** 002 * Copyright (c) 2000-2012 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.kernel.util; 016 017 import com.liferay.portal.kernel.security.pacl.permission.PortalRuntimePermission; 018 019 import java.io.File; 020 import java.io.InputStream; 021 022 import java.util.Set; 023 024 /** 025 * @author Jorge Ferrer 026 * @author Brian Wing Shun Chan 027 * @author Alexander Chow 028 */ 029 public class MimeTypesUtil { 030 031 public static String getContentType(File file) { 032 return getMimeTypes().getContentType(file); 033 } 034 035 public static String getContentType(File file, String fileName) { 036 return getMimeTypes().getContentType(file, fileName); 037 } 038 039 /** 040 * Returns the content type from an input stream and file name. 041 * 042 * @param inputStream the input stream of the content (optionally 043 * <code>null</code>) 044 * @param fileName the full name or extension of file (e.g., "Test.doc", 045 * ".doc") 046 * @return the content type if it is a supported format or an empty string 047 * if it is an unsupported format 048 */ 049 public static String getContentType( 050 InputStream inputStream, String fileName) { 051 052 return getMimeTypes().getContentType(inputStream, fileName); 053 } 054 055 /** 056 * Returns the content type from a file name. 057 * 058 * @param fileName the full name or extension of the file (e.g., 059 * "Test.doc", ".doc") 060 * @return the content type if it is a supported format or an empty string 061 * if it is an unsupported format 062 */ 063 public static String getContentType(String fileName) { 064 return getMimeTypes().getContentType(fileName); 065 } 066 067 public static String getExtensionContentType(String extension) { 068 return getMimeTypes().getExtensionContentType(extension); 069 } 070 071 /** 072 * Returns the possible file extensions for a given content type. 073 * 074 * @param contentType the content type of the file (e.g., "image/jpeg") 075 * @return the set of extensions if it is a known content type or an empty 076 * set if it is an unknown content type 077 */ 078 public static Set<String> getExtensions(String contentType) { 079 return getMimeTypes().getExtensions(contentType); 080 } 081 082 public static MimeTypes getMimeTypes() { 083 PortalRuntimePermission.checkGetBeanProperty(MimeTypesUtil.class); 084 085 return _mimeTypes; 086 } 087 088 public static boolean isWebImage(String mimeType) { 089 return getMimeTypes().isWebImage(mimeType); 090 } 091 092 public void setMimeTypes(MimeTypes mimeTypes) { 093 PortalRuntimePermission.checkSetBeanProperty(getClass()); 094 095 _mimeTypes = mimeTypes; 096 } 097 098 private static MimeTypes _mimeTypes; 099 100 }