public MediaType detect(InputStream input, Metadata metadata) {
        // Look for a resource name in the input metadata
        String name = metadata.get(TikaCoreProperties.RESOURCE_NAME_KEY);
        if (name != null) {
            // If the name is a URL, skip the trailing query
            int question = name.indexOf('?');
            if (question != -1) {
                name = name.substring(0, question);
            }

            // If the name is a URL or a path, skip all but the last component
            int slash = name.lastIndexOf('/');
            if (slash != -1) {
                name = name.substring(slash + 1);
            }
            int backslash = name.lastIndexOf('\\');
            if (backslash != -1) {
                name = name.substring(backslash + 1);
            }

            // Strip any fragments from the end, but only ones after the extension
            int hash = name.lastIndexOf('#');
            int dot = name.lastIndexOf('.');
            if (hash != -1) {
                if (dot == -1 || hash > dot) {
                    name = name.substring(0, hash);
                }
            }

            // Decode any potential URL encoding
            int percent = name.indexOf('%');
            if (percent != -1) {
                try {
                    name = URLDecoder.decode(name, UTF_8.name());
                } catch (UnsupportedEncodingException e) {
                    throw new IllegalStateException("UTF-8 not supported", e);
                }
            }

            // Skip any leading or trailing whitespace
            name = name.trim();
            if (name.length() > 0) {
                // Match the name against the registered patterns
                for (Map.Entry<Pattern, MediaType> entry : patterns.entrySet()) {
                    if (entry.getKey().matcher(name).matches()) {
                        return entry.getValue();
                    }
                }
            }
        }

        return MediaType.OCTET_STREAM;
    }
Detects the content type of an input document based on the document
name given in the input metadata. The RESOURCE_NAME_KEY attribute of
the given input metadata is expected to contain the name (normally
a file name or a URL) of the input document.
<p>
If a resource name is given, then it is first processed as follows.
<ol>
  <li>
    Potential URL query (?...) and fragment identifier (#...)
    parts are removed from the end of the resource name.
  </li>
  <li>
    Potential leading path elements (up to the last slash or backslash)
    are removed from the beginning of the resource name.
  </li>
  <li>
    Potential URL encodings (%nn, in UTF-8) are decoded.
  </li>
  <li>
    Any leading and trailing whitespace is removed.
  </li>
</ol>
<p>
The resulting name string (if any) is then matched in sequence against
all the configured name patterns. If a match is found, then the (first)
matching media type is returned.

@param input    ignored
@param metadata input metadata, possibly with a RESOURCE_NAME_KEY value
@return detected media type, or <code>application/octet-stream</code>
tika-core/src/main/java/org/apache/tika/detect/NameDetector.java