<commit_before>package main

import (
	"encoding/json"
	"errors"
	"fmt"
	"io/ioutil"
	"log"
	"net/http"
	"net/url"
	"os"
	"strconv"
	"strings"
)

/**
 * I can't use http.ServeFile due to https://github.com/golang/go/issues/13892
 *
 * As a workaround I'm going to spool the entire file into memory and then
 * write it back.. This won't fly on a popular server, but ehh right now
 * I'm only serving myself ;-)
 */
func serveFile(w http.ResponseWriter, r *http.Request, path string, content_type string) {
	content, err := ioutil.ReadFile(path)
	if err != nil {
		sendResponse(w, InternalError,
			map[string]interface{}{
				"code":    "InternalError",
				"message": fmt.Sprintf("Failed to read file %s: %v", path, err),
			})
		return
	}

	h := w.Header()
	h.Set("Server", "Norbye Public Images Repo")
	h.Set("Content-Type", content_type)
	h.Set("Content-Length", strconv.Itoa(len(content)))
	_, err = w.Write(content)
	if err != nil {
		log.Printf("Failed to send %s: %v", path, err)
	}
}

func sendResponse(w http.ResponseWriter, code int, content map[string]interface{}) {
	h := w.Header()
	h.Set("Server", "Norbye Public Images Repo")

	if content == nil {
		w.WriteHeader(code)
		return
	}

	h.Set("Content-Type", "application/json; charset=utf-8")

	a, err := json.MarshalIndent(content, "", "  ")
	if err != nil {
		log.Printf("Failed to convert response to JSON: %v",
			err)
		a, err = json.MarshalIndent(map[string]interface{}{
			"code":    "InvalidError",
			"message": fmt.Sprintf("%v", err),
		}, "", "")
		code = InternalError
	}

	w.WriteHeader(code)
	w.Write(a)
}

/**<commit_msg>Report error if size of sent payload does not match<commit_after>package main

import (
	"encoding/json"
	"errors"
	"fmt"
	"io/ioutil"
	"log"
	"net/http"
	"net/url"
	"os"
	"strconv"
	"strings"
)

/**
 * I can't use http.ServeFile due to https://github.com/golang/go/issues/13892
 *
 * As a workaround I'm going to spool the entire file into memory and then
 * write it back.. This won't fly on a popular server, but ehh right now
 * I'm only serving myself ;-)
 */
func serveFile(w http.ResponseWriter, r *http.Request, path string, content_type string) {
	content, err := ioutil.ReadFile(path)
	if err != nil {
		sendResponse(w, InternalError,
			map[string]interface{}{
				"code":    "InternalError",
				"message": fmt.Sprintf("Failed to read file %s: %v", path, err),
			})
		return
	}

	h := w.Header()
	h.Set("Server", "Norbye Public Images Repo")
	h.Set("Content-Type", content_type)
	contentLength := len(content)
	h.Set("Content-Length", strconv.Itoa(contentLength))
	nw, err := w.Write(content)
	if err != nil {
		log.Printf("Failed to send %s: %v", path, err)
	}
	if nw != contentLength {
		log.Printf("Size of sent payload (%d) does not match expected (%d) fo %s", nw, contentLength, path)
	}
}

func sendResponse(w http.ResponseWriter, code int, content map[string]interface{}) {
	h := w.Header()
	h.Set("Server", "Norbye Public Images Repo")

	if content == nil {
		w.WriteHeader(code)
		return
	}

	h.Set("Content-Type", "application/json; charset=utf-8")

	a, err := json.MarshalIndent(content, "", "  ")
	if err != nil {
		log.Printf("Failed to convert response to JSON: %v",
			err)
		a, err = json.MarshalIndent(map[string]interface{}{
			"code":    "InvalidError",
			"message": fmt.Sprintf("%v", err),
		}, "", "")
		code = InternalError
	}

	w.WriteHeader(code)
	w.Write(a)
}

/**
{'fraction_non_alphanumeric': 0.16573611519247722, 'fraction_numerical': 0.003526300323244196, 'mean_word_length': 3.835227272727273, 'pattern_counts': {'":': 8, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, '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': '6107565', 'n_tokens_mistral': 1309, 'n_tokens_neox': 1225, 'n_words': 336}