<commit_before>package main

import (
	"encoding/json"
	"fmt"
	"net/http"
	"time"

	"git.timschuster.info/rls.moe/catgi/logger"
	"github.com/gorilla/mux"
)

type handlerServeGet struct{}

func newHandlerServeGet() http.Handler {
	return &handlerServeGet{}
}

func (h *handlerServeGet) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
	log := logger.LogFromCtx("getFile", r.Context())

	log.Debug("Loading Flake")
	vars := mux.Vars(r)
	flake := vars["flake"]
	if len(flake) == 0 {
		log.Warn("Form contained no flake")
		rw.WriteHeader(500)
		fmt.Fprint(rw, "Missing flake")
		return
	}

	log.Debug("Loading File from Backend")
	f, err := curBe.Get(flake, r.Context())
	if err != nil {
		log.Warn("File error on backend: ", err)
		rw.WriteHeader(404)
		fmt.Fprint(rw, "Could not find file")
		return
	}

	log.Debug("Writing out response")

	mimetype := ""

	if f.ContentType == "" {
		if len(f.Data) > 512 {
			mimetype = http.DetectContentType(f.Data[:512])
		} else {
			mimetype = http.DetectContentType(f.Data)
		}
	} else {
		mimetype = f.ContentType
	}

	var filename = flake + f.FileExtension
	{
		if name, ok := vars["name"]; ok {
			if ext, ok := vars["ext"]; ok {
				filename = name + "." + ext
			}
		}
	}

	if r.URL.Query().Get("raw") == "1" {
		rw.Header().Add("Content-Type", "application/json")
		var dat []byte
		dat, err = json.Marshal(f)
		if err != nil {
			log.Errorf("Raw output error")
		}
		_, err = rw.Write(dat)
	} else {
		rw.Header().Add("Content-Disposition", "inline; filename="+filename)
		rw.Header().Add("Content-Type", mimetype)
		remainingAge := fmt.Sprintf("%.0f", f.DeleteAt.Sub(time.Now().UTC()).Seconds())
		rw.Header().Add("Cache-Control", "public, max-age="+remainingAge)
		rw.Header().Add("X-Catgi-Expires-At", f.DeleteAt.Format("2006-01-02"))
		rw.Header().Add("X-Catgi-Owner", f.User)
		_, err = rw.Write(f.Data)
	}
	if err != nil {
		log.Errorf("Error on write: %s", err)
	}

	return
}<commit_msg>Use http.ServeContent instead of crude mime detection and http.Write<commit_after>package main

import (
	"encoding/json"
	"fmt"
	"net/http"
	"time"

	"bytes"

	"git.timschuster.info/rls.moe/catgi/logger"
	"github.com/gorilla/mux"
)

type handlerServeGet struct{}

func newHandlerServeGet() http.Handler {
	return &handlerServeGet{}
}

func (h *handlerServeGet) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
	log := logger.LogFromCtx("getFile", r.Context())

	log.Debug("Loading Flake")
	vars := mux.Vars(r)
	flake := vars["flake"]
	if len(flake) == 0 {
		log.Warn("Form contained no flake")
		rw.WriteHeader(500)
		fmt.Fprint(rw, "Missing flake")
		return
	}

	log.Debug("Loading File from Backend")
	f, err := curBe.Get(flake, r.Context())
	if err != nil {
		log.Warn("File error on backend: ", err)
		rw.WriteHeader(404)
		fmt.Fprint(rw, "Could not find file")
		return
	}

	log.Debug("Writing out response")

	var filename = flake + f.FileExtension
	{
		if name, ok := vars["name"]; ok {
			if ext, ok := vars["ext"]; ok {
				filename = name + "." + ext
			}
		}
	}

	if r.URL.Query().Get("raw") == "1" {
		rw.Header().Add("Content-Type", "application/json")
		var dat []byte
		dat, err = json.Marshal(f)
		if err != nil {
			log.Errorf("Raw output error")
		}
		_, err = rw.Write(dat)
	} else {
		buf := bytes.NewReader(f.Data)
		remainingAge := fmt.Sprintf("%.0f", f.DeleteAt.Sub(time.Now().UTC()).Seconds())
		rw.Header().Add("Cache-Control", "public, max-age="+remainingAge)
		rw.Header().Add("X-Catgi-Expires-At", f.DeleteAt.Format("2006-01-02"))
		rw.Header().Add("X-Catgi-Owner", f.User)
		http.ServeContent(rw, r, f.Flake+"."+f.FileExtension, f.CreatedAt.Time, buf)
	}
	if err != nil {
		log.Errorf("Error on write: %s", err)
	}

	return
}
{'fraction_non_alphanumeric': 0.18763498920086394, 'fraction_numerical': 0.01079913606911447, 'mean_word_length': 3.774484536082474, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, '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': '1959802', 'n_tokens_mistral': 1552, 'n_tokens_neox': 1432, 'n_words': 291}