func TestKMZ(t *testing.T) { buf := &bytes.Buffer{} err := KMZ(image.Rect(0, 0, 1024, 1024), testPoints, 150, 128, schemes.AlphaFire, buf) if err != nil { t.Fatalf("Failed to build kmz: %v", err) } zr, err := zip.NewReader(bytes.NewReader(buf.Bytes()), int64(buf.Len())) if err != nil { t.Fatalf("Error reading kmz: %v", err) } for _, zf := range zr.File { switch zf.Name { case "doc.kml": gotK := rzd(t, zf) if !xsimilar(string(gotK), expKmz) { t.Errorf("Expected kml=%v, got %s", expKmz, gotK) } case "heatmap.png": img, _, err := image.Decode(bytes.NewReader(rzd(t, zf))) if err != nil { t.Errorf("Error decoding image: %v", err) } got := imghash.Average(img) if got != expHash { t.Errorf("Expected image hash %v, got %v", expHash, got) } default: t.Fatalf("Unexpected zipfile name: %v", err) } } }
func TestKML(t *testing.T) { kmlBuf := &bytes.Buffer{} img, err := KML(image.Rect(0, 0, 1024, 1024), testPoints, 150, 128, schemes.AlphaFire, testKmlImgURL, kmlBuf) if err != nil { t.Fatalf("Error generating kml: %v", err) } got := imghash.Average(img) if got != expHash { t.Errorf("Expected image hash %v, got %v", expHash, got) } gotK := kmlBuf.String() if !xsimilar(gotK, expKml) { t.Errorf("Expected kml=%v, got %v", expKml, gotK) } }
func (a *Api) Create(data []byte, name, descr, imgtype string, lon, lat float64) (Img, error) { bts := bytes.NewReader(data) img, _, err := image.Decode(bts) if err != nil { return Img{}, err } tm := time.Now() if name == "" { name = randName() } name = chpu.Chpu(name) title := name if len(name) <= int(a.nesting) { name = randName() } //TODO bad code for { found, err := a.ExistName(name) if err != nil { return Img{}, err } if found { name = randName() } if !found { break } } path := a.getPath(name) os.MkdirAll(path, 0777) out, err := os.Create(path + "/" + name + ".jpg") if err != nil { return Img{}, err } defer out.Close() jpeg.Encode(out, img, nil) //todo remove this(fly generated) if a.WebpAddr != "" { cmd := exec.Command(a.WebpAddr, path+"/"+name+".jpg", "-o", path+"/"+name+".webp") err := cmd.Run() if err != nil { fmt.Println(err) return Img{}, errors.New("Webp Not Working") } } im := Img{ Name: title, Slug: name, Description: descr, Created: tm.UnixNano(), Updated: tm.UnixNano(), Width: img.Bounds().Dx(), Height: img.Bounds().Dy(), Hash: int64(imghash.Average(img)), Type: imgtype, } err = a.Db.Insert(&im) if err != nil { fmt.Println(err) return Img{}, err } return im, nil }