<commit_before>package uploader

import (
	"fmt"
	"io/ioutil"
	"net/http"
)

func UploadHandler(uploader *Uploader, uploadField, filenameField string) http.HandlerFunc {

	return func(res http.ResponseWriter, req *http.Request) {
		req.ParseForm()

		switch req.Method {
		case "GET":
			filename := req.FormValue(filenameField)
			if filename == "" {
				res.WriteHeader(http.StatusBadRequest)
				return
			}
			imageData, err := uploader.Get(filename)
			if err != nil {
				if !uploader.Has(filename) {
					res.WriteHeader(http.StatusNotFound)
					return
				}
				res.WriteHeader(http.StatusInternalServerError)
				return
			}
			res.WriteHeader(http.StatusOK)
			res.Write(imageData)
			return
		case "PUT":
			// same for now
			fallthrough
		case "POST":
			file, _, err := req.FormFile(uploadField)
			if err != nil {
				res.WriteHeader(http.StatusBadRequest)
				return
			}
			//fmt.Println(file, fileinfo)
			imageData, err := ioutil.ReadAll(file)
			if err != nil {
				res.WriteHeader(http.StatusInternalServerError)
				return
			}

			filename, err := uploader.Store(imageData)
			if err != nil {
				res.WriteHeader(http.StatusInternalServerError)
				return
			}
			res.WriteHeader(http.StatusOK)
			res.Write([]byte(fmt.Sprintf("{\"status\": \"OK\", \"filename\": \"%s\"}", filename)))
			return
		case "DELETE":
			filename := req.FormValue(filenameField)
			if filename == "" {
				res.WriteHeader(http.StatusBadRequest)
				return
			}
			if !uploader.Has(filename) {
				res.WriteHeader(http.StatusNotFound)
				return
			}
			err := uploader.Delete(filename)
			if err != nil {
				res.WriteHeader(http.StatusInternalServerError)
				return
			}
			res.WriteHeader(http.StatusOK)
			res.Write([]byte("{\"status\": \"OK\"}"))
		default:
			res.WriteHeader(http.StatusMethodNotAllowed)
		}

	}
}<commit_msg>Create Handler so this code can use with other routers<commit_after>package uploader

import (
	"fmt"
	"io/ioutil"
	"mime/multipart"
	"net/http"
)

type Handler struct {
	uploader *Uploader
}

func NewHandler(uploader *Uploader) *Handler {
	return &Handler{uploader: uploader}
}

func (h *Handler) Get(res http.ResponseWriter, filename string) {
	if filename == "" {
		res.WriteHeader(http.StatusBadRequest)
		return
	}
	imageData, err := h.uploader.Get(filename)
	if err != nil {
		if !h.uploader.Has(filename) {
			res.WriteHeader(http.StatusNotFound)
			return
		}
		res.WriteHeader(http.StatusInternalServerError)
		return
	}
	res.WriteHeader(http.StatusOK)
	res.Write(imageData)
}

func (h *Handler) Post(res http.ResponseWriter, file multipart.File) {
	imageData, err := ioutil.ReadAll(file)
	if err != nil {
		res.WriteHeader(http.StatusInternalServerError)
		return
	}

	filename, err := h.uploader.Store(imageData)
	if err != nil {
		res.WriteHeader(http.StatusInternalServerError)
		return
	}
	res.WriteHeader(http.StatusOK)
	res.Write([]byte(fmt.Sprintf("{\"status\": \"OK\", \"filename\": \"%s\"}", filename)))
}

func (h *Handler) Delete(res http.ResponseWriter, filename string) {
	if filename == "" {
		res.WriteHeader(http.StatusBadRequest)
		return
	}
	if !h.uploader.Has(filename) {
		res.WriteHeader(http.StatusNotFound)
		return
	}
	err := h.uploader.Delete(filename)
	if err != nil {
		res.WriteHeader(http.StatusInternalServerError)
		return
	}
	res.WriteHeader(http.StatusOK)
	res.Write([]byte("{\"status\": \"OK\"}"))
}

func UploadHandler(uploader *Uploader, uploadField, filenameField string) http.HandlerFunc {
	handler := NewHandler(uploader)
	return func(res http.ResponseWriter, req *http.Request) {
		req.ParseForm()

		switch req.Method {
		case "GET":
			handler.Get(res, req.FormValue(filenameField))
		case "PUT":
			// same for now
			fallthrough
		case "POST":
			file, _, err := req.FormFile(uploadField)
			if err != nil {
				res.WriteHeader(http.StatusBadRequest)
				return
			}
			handler.Post(res, file)
		case "DELETE":
			handler.Delete(res, req.FormValue(filenameField))
		default:
			res.WriteHeader(http.StatusMethodNotAllowed)
		}

	}
}
{'fraction_non_alphanumeric': 0.13809405324707638, 'fraction_numerical': 0.0, 'mean_word_length': 3.7914183551847436, 'pattern_counts': {'":': 14, '<': 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': '4168317', 'n_tokens_mistral': 1563, 'n_tokens_neox': 1363, 'n_words': 261}