<commit_before>package com.composum.sling.core.util;

import com.composum.sling.core.ResourceHandle;
import org.apache.commons.lang3.StringUtils;
import org.apache.sling.api.resource.Resource;
import org.apache.tika.metadata.Metadata;
import org.apache.tika.mime.MediaType;
import org.apache.tika.mime.MimeType;
import org.apache.tika.mime.MimeTypeException;
import org.apache.tika.mime.MimeTypes;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.jcr.Binary;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import java.io.IOException;
import java.io.InputStream;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * some helpers for content mime types and resource file names
 */
public class MimeTypeUtil {

    private static final Logger LOG = LoggerFactory.getLogger(MimeTypeUtil.class);

    /** name pattern to check existence of a file extension at the end of the name */
    public static final Pattern HAS_FILE_EXT_PATTERN = Pattern.compile("^.*\\.([a-zA-Z]+[0-9]?)$");

    /** file extension from mime type value pattern fallback */
    public static final Pattern FILE_EXT_FROM_MIME = Pattern.compile("^.*/([a-zA-Z]+[0-9]?)$");

    //
    // mime types
    //

    /**
     * Detects the mime type of a binary resource (nt:file, nt:resource or another asset type).
     *
     * @param resource     the binary resource
     * @param defaultValue the default value if the detection has no useful result
     * @return he detected mime type or the default value given
     */
    public static String getMimeType(Resource resource, String defaultValue) {
        MimeType mimeType = getMimeType(resource);
        return mimeType != null ? mimeType.toString() : defaultValue;
    }

    /**
     * Detects the mime type of a binary property in the context of the nodes name.<commit_msg>Core : MimeTypeUtil.isMimeType... added<commit_after>package com.composum.sling.core.util;

import com.composum.sling.core.ResourceHandle;
import org.apache.commons.lang3.StringUtils;
import org.apache.sling.api.resource.Resource;
import org.apache.tika.metadata.Metadata;
import org.apache.tika.mime.MediaType;
import org.apache.tika.mime.MimeType;
import org.apache.tika.mime.MimeTypeException;
import org.apache.tika.mime.MimeTypes;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.jcr.Binary;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import java.io.IOException;
import java.io.InputStream;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * some helpers for content mime types and resource file names
 */
public class MimeTypeUtil {

    private static final Logger LOG = LoggerFactory.getLogger(MimeTypeUtil.class);

    /** name pattern to check existence of a file extension at the end of the name */
    public static final Pattern HAS_FILE_EXT_PATTERN = Pattern.compile("^.*\\.([a-zA-Z]+[0-9]?)$");

    /** file extension from mime type value pattern fallback */
    public static final Pattern FILE_EXT_FROM_MIME = Pattern.compile("^.*/([a-zA-Z]+[0-9]?)$");

    //
    // mime type check
    //

    public static boolean isMimeType(Resource resource, String pattern) {
        return isMimeType(resource, Pattern.compile(pattern));
    }

    public static boolean isMimeType(Resource resource, Pattern pattern) {
        String mimeType = getMimeType(resource, "");
        return pattern.matcher(mimeType).matches();
    }

    //
    // mime types
    //

    /**
     * Detects the mime type of a binary resource (nt:file, nt:resource or another asset type).
     *
     * @param resource     the binary resource
     * @param defaultValue the default value if the detection has no useful result
     * @return he detected mime type or the default value given
     */
    public static String getMimeType(Resource resource, String defaultValue) {
        MimeType mimeType = getMimeType(resource);
        return mimeType != null ? mimeType.toString() : defaultValue;
    }

    /**
     * Detects the mime type of a binary property in the context of the nodes name.
{'fraction_non_alphanumeric': 0.10572472594397077, 'fraction_numerical': 0.00341047503045067, 'mean_word_length': 3.5723830734966593, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7394886', 'n_tokens_mistral': 1251, 'n_tokens_neox': 1217, 'n_words': 352}