<commit_before>package Stitcher

import (
	"encoding/json"
	"fmt"
	"github.com/mergesort/Response"
	"io/ioutil"
	"net/http"
)

///////////////////////////////////////////////////////////////////////////////
// Structs

type IncomingRequest struct {
	ImageURLs []string `json:"urls"`
	Rows      int      `json:"rows"`
	Columns   int      `json:"columns"`
}

///////////////////////////////////////////////////////////////////////////////
// Public functions

func StitcherHandler(rw http.ResponseWriter, req *http.Request) {
	if req.Method == "POST" {
		body, err := ioutil.ReadAll(req.Body)
		rw.Header().Set("Content-Type", "application/json")

		if err != nil {
			response := responseJSON.Response{"status": "400", "description": fmt.Sprintf("Your request could not be processed. %v", err), "url": ""}
			sendResponse(rw, response)
		} else {
			var unmarshalledRequest IncomingRequest

			unmarshallingError := json.Unmarshal(body, &unmarshalledRequest)

			if unmarshallingError != nil {
				response := responseJSON.Response{"status": "400", "description": "The data request was formatted incorrectly.", "url": ""}
				sendResponse(rw, response)
			} else {
				for i := 0; i < len(unmarshalledRequest.ImageURLs); i++ {
					currURL := unmarshalledRequest.ImageURLs[i]
					fmt.Println(i, currURL)
				}
				response := responseJSON.Response{"status": "200", "description": "Success!", "url": "http://app.net.com/candy"}
				sendResponse(rw, response)
			}
		}
	} else {
		response := responseJSON.Response{"status": "405", "description": "This endpoint requires a POST operation.", "url": ""}
		sendResponse(rw, response)
	}
}

///////////////////////////////////////////////////////////////////////////////
////// Private helpers

func sendResponse(rw http.ResponseWriter, response responseJSON.Response) {
	fmt.Fprint(rw, response)
}<commit_msg>Currently crashes on trying to process the image, which is not downloading correctly<commit_after>package Stitcher

import (
	"encoding/json"
	"fmt"
	"github.com/mergesort/Response"
	"image"
	"image/jpeg"
	"io/ioutil"
	"net/http"
	"sync"
)

///////////////////////////////////////////////////////////////////////////////
// Structs

type IncomingRequest struct {
	ImageURLs []string `json:"urls"`
	Rows      int      `json:"rows"`
	Columns   int      `json:"columns"`
}

///////////////////////////////////////////////////////////////////////////////
// Public functions

func StitcherHandler(rw http.ResponseWriter, req *http.Request) {
	if req.Method == "POST" {
		body, err := ioutil.ReadAll(req.Body)
		rw.Header().Set("Content-Type", "application/json")

		if err != nil {
			response := responseJSON.Response{"status": "400", "description": fmt.Sprintf("Your request could not be processed. %v", err), "url": ""}
			sendResponse(rw, response)
		} else {
			var unmarshalledRequest IncomingRequest

			unmarshallingError := json.Unmarshal(body, &unmarshalledRequest)

			if unmarshallingError != nil {
				response := responseJSON.Response{"status": "400", "description": "The data request was formatted incorrectly.", "url": ""}
				sendResponse(rw, response)
			} else {
				for i := 0; i < len(unmarshalledRequest.ImageURLs); i++ {
					currURL := unmarshalledRequest.ImageURLs[i]
					fmt.Println(i, currURL)
				}

				downloadImages(unmarshalledRequest.ImageURLs)
			}
		}
	} else {
		response := responseJSON.Response{"status": "405", "description": "This endpoint requires a POST operation.", "url": ""}
		sendResponse(rw, response)
	}
}

///////////////////////////////////////////////////////////////////////////////
////// Private helpers

func sendResponse(rw http.ResponseWriter, response responseJSON.Response) {
	fmt.Fprint(rw, response)
}

func sendImageToS3(image image.Image) string {
	return ""
}

func downloadImages(imageURLs []string) {
	var group sync.WaitGroup
	images := make([]image.Image, len(imageURLs))

	for _, imageURL := range imageURLs {
		group.Add(1)
		go func() {
			defer group.Done()
			downloadImage(imageURL)
		}()
	}
	group.Wait()

	resultingImage := StitchImages(images)
	sendImageToS3(resultingImage)

	//create slice of images
	//download each image
	//when each one is there, stitch them, then call sendImageToS3
	// DownloadImage(url)
	//when you get a response from s3, call sendResponse with the URL

}

func downloadImage(imageURL string) image.Image {
	response, fetchError := http.Get(imageURL)
	if fetchError != nil {
		fmt.Println("An error occurred downloading ", imageURL, fetchError)
		return nil
	} else {
		defer response.Body.Close()

		image, decodeError := jpeg.Decode(response.Body)

		if decodeError != nil {
			fmt.Println("An error occurred decoding ", imageURL, decodeError)
			return nil
		} else {
			fmt.Println(response)
			fmt.Println(response.Body)
			return image
		}
	}
}
{'fraction_non_alphanumeric': 0.22502081598667778, 'fraction_numerical': 0.005828476269775187, 'mean_word_length': 2.615500376222724, 'pattern_counts': {'":': 21, '<': 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': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2785105', 'n_tokens_mistral': 1564, 'n_tokens_neox': 1402, 'n_words': 345}