private static boolean isValidFileContent(PrimeApplicationContext primeAppContext, String allowedContentTypes,
                                              String fileName, InputStream stream) throws IOException {
        if (LangUtils.isBlank(allowedContentTypes)) {
            return true;
        }

        String prefix = FilenameUtils.removeExtension(fileName);
        Path tempFile = Files.createTempFile(prefix, Constants.EMPTY_STRING);
        String contentType;

        try (InputStream in = new PushbackInputStream(new BufferedInputStream(stream));
             OutputStream out = Files.newOutputStream(tempFile)) {
            IOUtils.copyLarge(in, out);

            contentType = primeAppContext.getFileTypeDetector().probeContentType(tempFile);

            // We want to force the FileTypeDetector impl to try to detect the file type by its content,
            // this is why we intentionally hide the original file name/extension at first because both
            // tika and mime-types go the easy way of looking up the file extension if possible.
            // If this first attempt failed we try again, but now while preserving the original file name/extension
            // to e.g. make the JDK default FileTypeDetector work
            if (contentType == null) {
                String extension = FilenameUtils.getExtension(fileName);
                if (!extension.isEmpty()) {
                    String newFileName = tempFile.getFileName().toString() + "." + extension;
                    tempFile = Files.move(tempFile, tempFile.resolveSibling(newFileName));
                    contentType = primeAppContext.getFileTypeDetector().probeContentType(tempFile);
                }
            }
        }
        finally {
            deleteFile(tempFile);
        }

        if (contentType == null) {
            LOGGER.log(Level.WARNING, () -> String.format("Could not determine content type of uploaded file %s", fileName));
            return false;
        }

        //Comma-separated values: file_extension|audio/*|video/*|image/*|media_type (see https://www.w3schools.com/tags/att_input_accept.asp)
        final String filenameLC = fileName.toLowerCase();
        final String contentTypeLC = contentType.toLowerCase();

        boolean accepted = Stream.of(allowedContentTypes.split(","))
                .map(String::trim)
                .anyMatch(allowedContentType -> {
                    // try with extension first
                    if (allowedContentType.startsWith(".") && filenameLC.endsWith(allowedContentType)) {
                        LOGGER.log(Level.FINE, () -> String.format("File extension %s of the uploaded file %s is accepted", allowedContentType, fileName));
                        return true;
                    }
                    // or try with IANA media types
                    if (FilenameUtils.wildcardMatch(contentTypeLC, allowedContentType)) {
                        LOGGER.log(Level.FINE,
                                () -> String.format("Content type %s of the uploaded file %s is accepted by %s", contentTypeLC, fileName, allowedContentType));
                        return true;
                    }

                    return false;
                });

        if (!accepted) {
            LOGGER.log(Level.FINE,
                    () -> String.format("Uploaded file %s with content type %s does not match the accept specification %s",
                            fileName, contentTypeLC, allowedContentTypes));
            return false;
        }

        return true;
    }
Converts a JavaScript regular expression like '/(\.|\/)(gif|jpeg|jpg|png)$/i'
to the Java usable format '(\\.|\\/)(gif|jpeg|jpg|png)$'
@param jsRegex the client side JavaScript regex
@return the Java converted version of the regex
primefaces/src/main/java/org/primefaces/util/FileUploadUtils.java