示例#1
0
func PutNoImageProcess(w http.ResponseWriter, r *http.Request) {
	t0 := time.Now()
	if r.Method != "PUT" {
		w.Write(json.Message("ERROR", "Not supported Method"))
		return
	}

	reader, err := r.MultipartReader()
	if err != nil {
		w.Write(json.Message("ERROR", "Client should support multipart/form-data"))
		return
	}

	buf := bytes.NewBufferString("")
	for {
		part, err := reader.NextPart()
		if err == io.EOF {
			break
		}
		if part.FileName() == "" { // if empy skip this iteration
			continue
		}
		_, err = io.Copy(buf, part)
		if err != nil {
			http.Error(w, err.Error(), http.StatusInternalServerError)
			return
		}
	}
	defer r.Body.Close()

	img, _, err := image.Decode(buf)
	if err != nil {
		w.Write(json.Message("ERROR", "Unable to decode your image"))
		return
	}

	fileOrig, err := imgToFile(img, "ACA0AC")
	if err != nil {
		w.Write(json.Message("ERROR", "Unable to save your image"))
	}

	result := json.Result{
		Newborn: fileOrig,
	}
	if err != nil {
		w.Write(json.Message("ERROR", "Unable to save your image meta into db"))
	} else {
		w.Write(json.Message("OK", &result))
	}

	t1 := time.Now()
	log.Printf("The call took %v to run.\n", t1.Sub(t0))
}
示例#2
0
/*
 *{
 *	status: "ok"
 * 	result: { newborn: "0001_040db0bc2fc49ab41fd81294c7d195c7d1de358b_ACA0AC_100_160" }
 *}
 */
func Put(w http.ResponseWriter, r *http.Request) {
	t0 := time.Now()
	if r.Method != "PUT" {
		w.Write(json.Message("ERROR", "Not supported Method"))
		return
	}

	reader, err := r.MultipartReader()
	if err != nil {
		w.Write(json.Message("ERROR", "Client should support multipart/form-data"))
		return
	}

	buf := bytes.NewBufferString("")
	for {
		part, err := reader.NextPart()
		if err == io.EOF {
			break
		}
		if part.FileName() == "" { // if empy skip this iteration
			continue
		}
		_, err = io.Copy(buf, part)
		if err != nil {
			http.Error(w, err.Error(), http.StatusInternalServerError)
			return
		}
	}
	defer r.Body.Close()

	img, _, err := image.Decode(buf)
	if err != nil {
		w.Write(json.Message("ERROR", "Unable to decode your image"))
		return
	}

	imgBaby := resize.Resize(conf.BabyWidth, 0, img, resize.NearestNeighbor)
	imgInfant := resize.Resize(conf.InfantWidth, 0, imgBaby, resize.NearestNeighbor)
	imgNewborn := resize.Resize(conf.NewbornWidth, 0, imgInfant, resize.NearestNeighbor)
	imgSperm := resize.Resize(conf.Sperm, conf.Sperm, imgNewborn, resize.NearestNeighbor)

	red, green, blue, _ := imgSperm.At(0, 0).RGBA()
	color := fmt.Sprintf("%X%X%X", red>>8, green>>8, blue>>8) // removing 1 byte 9A16->9A

	fileOrig, err := imgToFile(img, color)
	if err != nil {
		w.Write(json.Message("ERROR", "Unable to save your image"))
	}
	fileBaby, err := imgToFile(imgBaby, color)
	fileInfant, err := imgToFile(imgInfant, color)
	fileNewborn, err := imgToFile(imgNewborn, color)

	result := json.Result{
		Newborn: fileNewborn,
	}
	log.Printf("fileOrig=%s,fileBaby=%s,fileInfant=%s,fileNewborn=%s", fileOrig, fileBaby, fileInfant, fileNewborn)
	if err != nil {
		w.Write(json.Message("ERROR", "Unable to save your image meta into db"))
	} else {
		w.Write(json.Message("OK", &result))
	}

	t1 := time.Now()
	log.Printf("The call took %v to run.\n", t1.Sub(t0))
}