001/*
002 * (C) Copyright 2006-2007 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Nuxeo - initial API and implementation
018 *
019 * $Id: MimetypeRegistry.java 20731 2007-06-18 15:13:32Z ogrisel $
020 */
021package org.nuxeo.ecm.platform.mimetype.interfaces;
022
023import java.io.File;
024import java.io.InputStream;
025import java.util.List;
026
027import org.nuxeo.ecm.core.api.Blob;
028import org.nuxeo.ecm.platform.mimetype.MimetypeDetectionException;
029import org.nuxeo.ecm.platform.mimetype.MimetypeNotFoundException;
030
031/**
032 * MimetypeEntry registry.
033 * <p>
034 * Flexible registry of mimetypes.
035 *
036 * @author <a href="ja@nuxeo.com">Julien Anguenot</a>
037 */
038public interface MimetypeRegistry {
039
040    String DEFAULT_MIMETYPE = "application/octet-stream";
041
042    /**
043     * Returns the mime type from a given stream.
044     *
045     * @return String mimetype name.
046     * @throws MimetypeNotFoundException if mimetype sniffing failed to identify a registered mime type
047     * @throws MimetypeDetectionException if unexpected problem prevent the detection to work as expected
048     */
049    String getMimetypeFromStream(InputStream stream) throws MimetypeNotFoundException, MimetypeDetectionException;
050
051    /**
052     * Returns the mime type from a given stream.
053     *
054     * @return String mimetype name.
055     * @throws MimetypeNotFoundException if mimetype sniffing failed to identify a registered mime type
056     * @throws MimetypeDetectionException if unexpected problem prevent the detection to work as expected
057     */
058    String getMimetypeFromBlob(Blob blob) throws MimetypeNotFoundException, MimetypeDetectionException;
059
060    /**
061     * Returns the mime type from a given blob or provided default if not possible.
062     *
063     * @throws MimetypeDetectionException
064     */
065    String getMimetypeFromBlobWithDefault(Blob blob, String defaultMimetype) throws MimetypeDetectionException;
066
067    /**
068     * Returns the mime type from a given stream or provided default if not possible.
069     *
070     * @throws MimetypeDetectionException
071     */
072    String getMimetypeFromStreamWithDefault(InputStream is, String defaultMimetype) throws MimetypeDetectionException;
073
074    /**
075     * Returns the mime type from a given filename.
076     */
077    String getMimetypeFromFilename(String filename);
078
079    /**
080     * Returns the mime type given a file.
081     *
082     * @return string containing the mime type
083     * @throws MimetypeNotFoundException if mimetype sniffing failed
084     * @throws MimetypeDetectionException if unexpected problem prevent the detection to work as expected
085     */
086    String getMimetypeFromFile(File file) throws MimetypeNotFoundException, MimetypeDetectionException;
087
088    /**
089     * Returns the extension for given mimetype.
090     *
091     * @param mimetypeName the mimetype name.
092     * @return a list of strings containing the possible extensions.
093     */
094    List<String> getExtensionsFromMimetypeName(String mimetypeName);
095
096    /**
097     * Gets a mimetype entry by name.
098     *
099     * @param name the mimetype name.
100     * @return mimetype instance
101     */
102    MimetypeEntry getMimetypeEntryByName(String name);
103
104    /**
105     * Gets a mimetype entry given the normalized mimetype.
106     *
107     * @param mimetype the normalized mimetype
108     * @return mimetype instance
109     */
110    MimetypeEntry getMimetypeEntryByMimeType(String mimetype);
111
112    /**
113     * Finds the mimetype of some content according to its filename and / or binary content.
114     *
115     * @param filename extension to analyze
116     * @param blob content to be analyzed if filename is ambiguous
117     * @param defaultMimetype defaultMimeType to be used if no found
118     * @return the string mimetype
119     * @throws MimetypeDetectionException
120     */
121    String getMimetypeFromFilenameAndBlobWithDefault(String filename, Blob blob, String defaultMimetype)
122            throws MimetypeDetectionException;
123
124    /**
125     * Update the mimetype field of a Blob based on the provided filename with fallback to binary sniffing. If the
126     * embedded filename is null, the provided filename is embedded into the blob as well.
127     *
128     * @param blob content to be analyzed if filename is ambiguous
129     * @param filename with extension to analyze
130     * @return updated blob (persisted if necessary)
131     * @throws MimetypeDetectionException
132     */
133    Blob updateMimetype(Blob blob, String filename) throws MimetypeDetectionException;
134
135    /**
136     * Update the mimetype field of a Blob based on the embedded filename with fallback to binary sniffing. This method
137     * should not be called if the embedded filename is null for performance reasons (+ the fact that binary sniffing is
138     * no very reliable).
139     *
140     * @param blob content to be analyzed if filename is ambiguous
141     * @return updated blob (persisted if necessary)
142     * @throws MimetypeDetectionException
143     */
144    Blob updateMimetype(Blob blob) throws MimetypeDetectionException;
145
146    /**
147     * Returns the mime type from a given extension.
148     *
149     * @since 7.3
150     */
151    String getMimetypeFromExtension(String extension) throws MimetypeNotFoundException;
152
153}