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.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 * Determine the content type from an input stream and file name. 041 * 042 * @param fileName full name or extension of file (e.g., "Test.doc", 043 * ".doc") 044 * @return content type if it is a supported format or an empty string if it 045 * is an unsupported format 046 */ 047 public static String getContentType( 048 InputStream inputStream, String fileName) { 049 050 return getMimeTypes().getContentType(inputStream, fileName); 051 } 052 053 /** 054 * Determine the content type from a file name. 055 * 056 * @param fileName full name or extension of file (e.g., "Test.doc", 057 * ".doc") 058 * @return content type if it is a supported format or an empty string if it 059 * is an unsupported format 060 */ 061 public static String getContentType(String fileName) { 062 return getMimeTypes().getContentType(fileName); 063 } 064 065 /** 066 * Determine the possible file extensions for a given content type. 067 * 068 * @param contentType content type of file (e.g., "image/jpeg") 069 * @return set of extensions if it is a known content type or an empty set 070 * if it is an unknown content type 071 */ 072 public static Set<String> getExtensions(String contentType) { 073 return getMimeTypes().getExtensions(contentType); 074 } 075 076 public static MimeTypes getMimeTypes() { 077 PortalRuntimePermission.checkGetBeanProperty(MimeTypesUtil.class); 078 079 return _mimeTypes; 080 } 081 082 public void setMimeTypes(MimeTypes mimeTypes) { 083 PortalRuntimePermission.checkSetBeanProperty(getClass()); 084 085 _mimeTypes = mimeTypes; 086 } 087 088 private static MimeTypes _mimeTypes; 089 090 }