<commit_before>package image

import (
	"bytes"
	"errors"
	"fmt"
	"github.com/henvic/picel/logger"
	"os/exec"
	"strings"
)

const (
	WEBP_QUALITY    = "92"
	IMAGICK_QUALITY = "92"
)

var (
	ErrOutputFormatNotSupported = errors.New("The requested output format is not supported")
	Verbose                     = false
)

var OutputFormats = map[string]string{
	"jpg":  "Imagick",
	"jpeg": "Imagick",
	"gif":  "Imagick",
	"png":  "Imagick",
	"pdf":  "Imagick",
	"webp": "Webp",
}

func Process(t Transform, input string, output string) (err error) {
	tool, valid := OutputFormats[strings.ToLower(t.Output)]

	if !valid {
		return ErrOutputFormatNotSupported
	}

	if tool == "Imagick" {
		return processImagick(t, input, output)
	}

	return processWebp(t, input, output)
}

func callProgram(name string, params []string) error {
	cmd := exec.Command(name, params...)
	var bOut bytes.Buffer
	var bErr bytes.Buffer
	cmd.Stdout = &bOut
	cmd.Stderr = &bErr

	if Verbose {
		logger.Stdout.Println(fmt.Sprintf("%v %v", name, strings.Join(params, " ")))
	}

	cmdErr := cmd.Run()

	if Verbose {
		logger.Stdout.Println(string(bOut.Bytes()))
		logger.Stderr.Println(string(bErr.Bytes()))
	}<commit_msg>Adding mime type validation to the bridge.<commit_after>package image

import (
	"bytes"
	"errors"
	"fmt"
	"log"
	"os/exec"
	"strings"

	"github.com/henvic/picel/logger"
	"github.com/rakyll/magicmime"
)

const (
	WEBP_QUALITY    = "92"
	IMAGICK_QUALITY = "92"
)

var (
	ErrOutputFormatNotSupported = errors.New("The requested output format is not supported")
	ErrMimeTypeExtension        = errors.New("Internal mime type extension error")
	ErrMimeTypeNotSupported     = errors.New("The loaded file mime type is not supported")
	Verbose                     = false
)

var OutputFormats = map[string]string{
	"jpg":  "Imagick",
	"jpeg": "Imagick",
	"gif":  "Imagick",
	"png":  "Imagick",
	"pdf":  "Imagick",
	"webp": "Webp",
}

var ValidInputMimeTypes = map[string]bool{
	"image/jpeg": true,
	"image/png":  true,
	"image/webp": true,
	"image/gif":  true,
}

func init() {
	if err := magicmime.Open(
		magicmime.MAGIC_MIME_TYPE |
			magicmime.MAGIC_SYMLINK |
			magicmime.MAGIC_ERROR); err != nil {
		log.Fatal(err)
	}
}

func Process(t Transform, input string, output string) (err error) {
	tool, valid := OutputFormats[strings.ToLower(t.Output)]

	if !valid {
		return ErrOutputFormatNotSupported
	}

	mimeType, mimeErr := magicmime.TypeByFile(input)

	if mimeErr != nil {
		return ErrMimeTypeExtension
	}

	if !ValidInputMimeTypes[mimeType] {
		return ErrMimeTypeNotSupported
	}

	if tool == "Imagick" {
		return processImagick(t, input, output)
	}

	return processWebp(t, input, output)
}

func callProgram(name string, params []string) error {
	cmd := exec.Command(name, params...)
	var bOut bytes.Buffer
	var bErr bytes.Buffer
	cmd.Stdout = &bOut
	cmd.Stderr = &bErr

	if Verbose {
		logger.Stdout.Println(fmt.Sprintf("%v %v", name, strings.Join(params, " ")))
	}

	cmdErr := cmd.Run()

	if Verbose {
		logger.Stdout.Println(string(bOut.Bytes()))
		logger.Stderr.Println(string(bErr.Bytes()))
	}
{'fraction_non_alphanumeric': 0.14512471655328799, 'fraction_numerical': 0.0025915127955944283, 'mean_word_length': 4.045751633986928, 'pattern_counts': {'":': 16, '<': 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': '4774783', 'n_tokens_mistral': 1232, 'n_tokens_neox': 1165, 'n_words': 253}