Ejemplo n.º 1
0
func (ac *ActionsConfig) Pixelette(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {

	username := ps.ByName("username")
	chopid := ps.ByName("chopid")
	s := chopper.NewSlapchop(ac.Host, username, chopid)
	t_files, _ := s.LoadFiles(ac.UploadDir)

	up := s.UploadPoint(ac.UploadDir)
	var sR, sG, sB, sA uint64
	sR, sG, sB, sA = 0, 0, 0, 0
	for _, f := range t_files {
		file, _ := os.Open(fmt.Sprintf("%s/%s", up, f.Name()))
		img, _, _ := chopper.Load(file)
		subImg := image.NewRGBA(image.Rect(0, 0, ac.TileSize, ac.TileSize))
		draw.Draw(subImg, subImg.Bounds(), *img, image.Rect(0, 0, ac.TileSize, ac.TileSize).Min, draw.Src)

		for x := 0; x <= ac.TileSize; x++ {
			for y := 0; y <= ac.TileSize; y++ {
				r, g, b, a := subImg.At(x, y).RGBA()
				sR += uint64(r)
				sG += uint64(g)
				sB += uint64(b)
				sA += uint64(a)
			}
		}
		ms := uint64(ac.TileSize * ac.TileSize)
		println(sR/ms, sG/ms, sB/ms, sA/ms)

	}

	w.WriteHeader(http.StatusOK)
	w.Write([]byte(``))
}
Ejemplo n.º 2
0
/* Let's use here the CRUD standard names */
func (ac *ActionsConfig) Create(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {

	username := ps.ByName("username")
	r.ParseMultipartForm(ac.MaxUploadSize)

	// Opening the file
	file, _, err := r.FormFile("uploadfile")
	if err != nil {
		log.Println(err)
		return
	}

	// Setting to close filehandler at the end of this function
	defer file.Close()
	chop_id := time.Now().Format("020106150405")
	path := fmt.Sprintf("%s/%s/%s", ac.UploadDir, username, chop_id)
	img, format, err := chopper.Load(file)
	tiles := chopper.Slice(*img, ac.TileSize, format, path)
	chopper.SaveAll(tiles)

	href_base := fmt.Sprintf("http://%s:8080/upload/%s/%s", ac.HostName, username, chop_id)

	tilesR := make([]*chopper.TileEntry, len(tiles))
	for i, t := range tiles {
		tilesR[i] = t.ToResp(href_base)
	}

	resp := chopper.CreateResponse{
		User:   username,
		ChopId: chop_id,
		Href:   fmt.Sprintf("http://%s/chopit/%s/%s", ac.Host, username, chop_id),
		Tiles:  tilesR,
	}

	// If it is setted to connect with Puzzler Service
	if len(ac.PuzzlerHost) > 0 {
		status, body, err := puzzler.CreatePuzzle(ac.PuzzlerHost, username, tilesR)
		if err != nil {
			log.Print(err)
		}

		if status == 201 {
			puzzler_resp := puzzler.CreateResponse{}
			_ = json.Unmarshal(body, &puzzler_resp)
			resp.PuzzleHref = puzzler_resp.PuzzleHref
			resp.SolutionHref = puzzler_resp.SolutionHref
		}
	}

	json_resp, err := json.Marshal(&resp)
	if err != nil {
		log.Fatalf("errr %v", err)
	}

	w.Write(json_resp)
	w.WriteHeader(http.StatusOK)
}