<commit_before>package ims

import (
	"fmt"
	"image"
	"io"
	"net/http"
	"net/http/pprof"
	"net/url"
	"os"
	"strconv"
	"time"

	"github.com/Sirupsen/logrus"
	"github.com/disintegration/imaging"
	"github.com/pkg/errors"
	"github.com/urfave/negroni"
)

const (

	// timeout is the cache timeout used to add to requests to prevent the
	// browser from re-requesting the image.
	timeout = 15 * time.Minute
)

// ProcessImage uses the github.com/disintegration/imaging lib to perform the
// image transformations.
func ProcessImage(input io.Reader, w http.ResponseWriter, r *http.Request) error {
	srcImage, format, err := image.Decode(input)
	if err != nil {
		return errors.Wrap(err, "can't decode the image")
	}

	width, err := strconv.Atoi(r.URL.Query().Get("w"))
	if err == nil {
		srcImage = imaging.Resize(srcImage, width, 0, imaging.Linear)
	}

	w.Header().Set("Cache-Control", fmt.Sprintf("public, max-age=%d", int64(timeout.Seconds())))
	w.Header().Set("Last-Modified", time.Now().String())

	encoder := GetEncoder(format, r)
	if err := encoder.Encode(srcImage, w); err != nil {
		return errors.Wrap(err, "can't encode the image")
	}

	return nil
}

func GetFilename(r *http.Request) (string, error) {

	// We expect that the router sends us requests in the form `/resize/:filename`
	// so we check to see if the path contains the image url that we want to
	// parse. In this case, we check to see that the path is at least 9 characters
	// long, which will ensure that the filename has at least 1 character.
	if len(r.URL.Path) < 9 {
		return "", errors.New("filename too short")
	}

	return r.URL.Path[8:], nil
}

// HandleFileSystemResize performs the actual resizing by loading the image<commit_msg>linting<commit_after>package ims

import (
	"fmt"
	"image"
	"io"
	"net/http"
	"net/http/pprof"
	"net/url"
	"os"
	"strconv"
	"time"

	"github.com/Sirupsen/logrus"
	"github.com/disintegration/imaging"
	"github.com/pkg/errors"
	"github.com/urfave/negroni"
)

const (

	// timeout is the cache timeout used to add to requests to prevent the
	// browser from re-requesting the image.
	timeout = 15 * time.Minute
)

// ProcessImage uses the github.com/disintegration/imaging lib to perform the
// image transformations.
func ProcessImage(input io.Reader, w http.ResponseWriter, r *http.Request) error {
	srcImage, format, err := image.Decode(input)
	if err != nil {
		return errors.Wrap(err, "can't decode the image")
	}

	width, err := strconv.Atoi(r.URL.Query().Get("w"))
	if err == nil {
		srcImage = imaging.Resize(srcImage, width, 0, imaging.Linear)
	}

	w.Header().Set("Cache-Control", fmt.Sprintf("public, max-age=%d", int64(timeout.Seconds())))
	w.Header().Set("Last-Modified", time.Now().String())

	encoder := GetEncoder(format, r)
	if err := encoder.Encode(srcImage, w); err != nil {
		return errors.Wrap(err, "can't encode the image")
	}

	return nil
}

// GetFilename fetches the filename from the request path.
func GetFilename(r *http.Request) (string, error) {

	// We expect that the router sends us requests in the form `/resize/:filename`
	// so we check to see if the path contains the image url that we want to
	// parse. In this case, we check to see that the path is at least 9 characters
	// long, which will ensure that the filename has at least 1 character.
	if len(r.URL.Path) < 9 {
		return "", errors.New("filename too short")
	}

	return r.URL.Path[8:], nil
}

// HandleFileSystemResize performs the actual resizing by loading the image
{'fraction_non_alphanumeric': 0.13585232189212576, 'fraction_numerical': 0.005191808479953851, 'mean_word_length': 3.8639551192145865, 'pattern_counts': {'":': 0, '<': 5, '<?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': '4065774', 'n_tokens_mistral': 1215, 'n_tokens_neox': 1160, 'n_words': 373}