func Hash(s *nstuff.Host) { s.Print(`<!doctype html> <html lang="vn"> <head> <meta charset="utf-8"> <title>Image Index Uploader</title> <script> function submit() { document.getElementById("form").submit(); } `) file, header, err := s.File("file") if err == nil { if err == nil { filetype := header.Header.Get("Content-Type") if filetype == "image/jpeg" || filetype == "image/png" || filetype == "image/gif" { m, _, err := tipimage.Decode(file) if err == nil { h := md5.New() io.Copy(h, file) checksum := base64.URLEncoding.EncodeToString(h.Sum(nil)) phash, p := PHash(m) FindImage(s, checksum, phash, p) } else { s.Print("error=invalid_image&err_detail=", err.Error()) } } } else { s.Print("error=invalid_input") } } s.Print(` </script> </head> <body> <form id="form" method="post" enctype="multipart/form-data" action="hash"> <input id="uploader" type="file" name="file" onchange="submit();" /> </form> </body> </html> `) }
func (i *ImageIndex) FetchImage(info imageInfo, pageID string) { i.wg.Add(1) _, err := i.s.Conn.Storage("Image").NewQuery().KeysOnly(). Filter("Location", model.EQ, info.src).GetFirst(nil) if err != model.ErrNotFound { i.s.Log("Error: %s\n", "galleyes: indexed image") i.wg.Done() return } resp, err := i.s.Client.Get(info.src) if err != nil { i.s.Log("Error: %s\n", err.Error()) i.wg.Done() return } mime := resp.Header.Get("Content-Type") if mime != "image/png" && mime != "image/jpeg" && mime != "image/gif" { i.s.Log("Error: %s - %s - %s\n", "galleyes: not supported image format", mime, info.src) i.wg.Done() return } data, err := ioutil.ReadAll(resp.Body) if err != nil { i.s.Log("Error: %s\n", err.Error()) i.wg.Done() return } // caculate PHash var buff bytes.Buffer _, err = buff.Write(data) if err != nil { i.s.Log("Error: %s\n", err.Error()) i.wg.Done() return } m, _, err := tipimage.Decode(&buff) if err != nil { i.s.Log("Error: %s\n", err.Error()) i.wg.Done() return } hash, part := PHash(m) // caculate md5 checksum h := md5.New() _, err = h.Write(data) if err != nil { i.s.Log("Error: %s\n", err.Error()) i.wg.Done() return } checksum := h.Sum(nil) // save the original image w, err := blobstore.Create(i.s.Context, mime) if err != nil { i.s.Log("Error: %s\n", err.Error()) i.wg.Done() return } w.Write(data) _, err = w.Write(data) if err != nil { i.s.Log("Error: %s\n", err.Error()) i.wg.Done() return } err = w.Close() if err != nil { i.s.Log("Error: %s\n", err.Error()) i.wg.Done() return } key, err := w.Key() if err != nil { i.s.Log("Error: %s\n", err.Error()) i.wg.Done() return } link, err := imgs.ServingURL(i.s.Context, key, nil) if err != nil { i.s.Log("Error: %s\n", err.Error()) i.wg.Done() return } // assign value img := Image{} img.PageID = pageID img.SavedID = string(key) img.SavedLocation = link.String() img.Location = info.src img.Description = info.alt img.CheckSum = base64.URLEncoding.EncodeToString(checksum) img.PHash = hash img.Part0 = part[0] img.Part1 = part[1] img.Part2 = part[2] img.Part3 = part[3] img.Part4 = part[4] img.Part5 = part[5] img.Part6 = part[6] img.Part7 = part[7] _, err = i.s.Conn.Storage("Image").Put(&img) if err != nil { i.s.Log("Error: %s\n", err.Error()) i.wg.Done() return } i.wg.Done() }