changed imgmap to img for better readability

This commit is contained in:
Antoni Sawicki 2019-11-03 00:56:04 -07:00
parent 4dee5ea8d9
commit 2f2e99eb85

14
wrp.go
View File

@ -36,7 +36,7 @@ var (
srv http.Server srv http.Server
ctx context.Context ctx context.Context
cancel context.CancelFunc cancel context.CancelFunc
imgmap = make(map[string]bytes.Buffer) img = make(map[string]bytes.Buffer)
ismap = make(map[string]wrpReq) ismap = make(map[string]wrpReq)
nodel bool nodel bool
imgtype string imgtype string
@ -152,14 +152,14 @@ func mapServer(out http.ResponseWriter, req *http.Request) {
func imgServer(out http.ResponseWriter, req *http.Request) { func imgServer(out http.ResponseWriter, req *http.Request) {
log.Printf("%s IMG Request for %s\n", req.RemoteAddr, req.URL.Path) log.Printf("%s IMG Request for %s\n", req.RemoteAddr, req.URL.Path)
imgbuf, ok := imgmap[req.URL.Path] imgbuf, ok := img[req.URL.Path]
if !ok || imgbuf.Bytes() == nil { if !ok || imgbuf.Bytes() == nil {
fmt.Fprintf(out, "Unable to find image %s\n", req.URL.Path) fmt.Fprintf(out, "Unable to find image %s\n", req.URL.Path)
log.Printf("%s Unable to find image %s\n", req.RemoteAddr, req.URL.Path) log.Printf("%s Unable to find image %s\n", req.RemoteAddr, req.URL.Path)
return return
} }
if !nodel { if !nodel {
defer delete(imgmap, req.URL.Path) defer delete(img, req.URL.Path)
} }
if strings.HasPrefix(req.URL.Path, ".gif") { if strings.HasPrefix(req.URL.Path, ".gif") {
out.Header().Set("Content-Type", "image/gif") out.Header().Set("Content-Type", "image/gif")
@ -244,24 +244,24 @@ func (w wrpReq) capture(c string, out http.ResponseWriter) {
mappath := fmt.Sprintf("/map/%04d.map", seq) mappath := fmt.Sprintf("/map/%04d.map", seq)
ismap[mappath] = w ismap[mappath] = w
if imgtype == "gif" { if imgtype == "gif" {
img, err := png.Decode(bytes.NewReader(pngcap)) i, err := png.Decode(bytes.NewReader(pngcap))
if err != nil { if err != nil {
log.Printf("%s Failed to decode screenshot: %s\n", c, err) log.Printf("%s Failed to decode screenshot: %s\n", c, err)
fmt.Fprintf(out, "<BR>Unable to decode page screenshot:<BR>%s<BR>\n", err) fmt.Fprintf(out, "<BR>Unable to decode page screenshot:<BR>%s<BR>\n", err)
return return
} }
var gifbuf bytes.Buffer var gifbuf bytes.Buffer
err = gif.Encode(&gifbuf, img, &gif.Options{NumColors: int(w.C), Quantizer: quantize.MedianCutQuantizer{}}) err = gif.Encode(&gifbuf, i, &gif.Options{NumColors: int(w.C), Quantizer: quantize.MedianCutQuantizer{}})
if err != nil { if err != nil {
log.Printf("%s Failed to encode GIF: %s\n", c, err) log.Printf("%s Failed to encode GIF: %s\n", c, err)
fmt.Fprintf(out, "<BR>Unable to encode GIF:<BR>%s<BR>\n", err) fmt.Fprintf(out, "<BR>Unable to encode GIF:<BR>%s<BR>\n", err)
return return
} }
imgmap[imgpath] = gifbuf img[imgpath] = gifbuf
log.Printf("%s Encoded GIF image: %s, Size: %dKB, Colors: %d\n", c, imgpath, len(gifbuf.Bytes())/1024, w.C) log.Printf("%s Encoded GIF image: %s, Size: %dKB, Colors: %d\n", c, imgpath, len(gifbuf.Bytes())/1024, w.C)
} else if imgtype == "png" { } else if imgtype == "png" {
pngbuf := bytes.NewBuffer(pngcap) pngbuf := bytes.NewBuffer(pngcap)
imgmap[imgpath] = *pngbuf img[imgpath] = *pngbuf
log.Printf("%s Got PNG image: %s, Size: %dKB\n", c, imgpath, len(pngbuf.Bytes())/1024) log.Printf("%s Got PNG image: %s, Size: %dKB\n", c, imgpath, len(pngbuf.Bytes())/1024)
} }
fmt.Fprintf(out, "<A HREF=\"%s\"><IMG SRC=\"%s\" BORDER=\"0\" ISMAP></A>", mappath, imgpath) fmt.Fprintf(out, "<A HREF=\"%s\"><IMG SRC=\"%s\" BORDER=\"0\" ISMAP></A>", mappath, imgpath)