// ServeTile returns a tile with appropriate Content-Type set. func (d *Data) ServeTile(uuid dvid.UUID, ctx storage.Context, w http.ResponseWriter, r *http.Request, parts []string) error { if d.Levels == nil || len(d.Levels) == 0 { return ErrNoMetadataSet } tileReq, err := d.parseTileReq(r, parts) queryValues := r.URL.Query() noblanksStr := dvid.InstanceName(queryValues.Get("noblanks")) var noblanks bool if noblanksStr == "true" { noblanks = true } var formatStr string if len(parts) >= 8 { formatStr = parts[7] } data, err := d.getTileData(ctx, tileReq) if err != nil { server.BadRequest(w, r, err) return err } if data == nil { if noblanks { http.NotFound(w, r) return nil } img, err := d.getBlankTileImage(uuid, tileReq) if err != nil { return err } return dvid.WriteImageHttp(w, img, formatStr) } switch d.Encoding { case LZ4: var img dvid.Image if err := img.Deserialize(data); err != nil { return err } data, err = img.GetPNG() w.Header().Set("Content-type", "image/png") case PNG: w.Header().Set("Content-type", "image/png") case JPG: w.Header().Set("Content-type", "image/jpeg") } if err != nil { server.BadRequest(w, r, err) return err } if _, err = w.Write(data); err != nil { return err } return nil }
// ServeTile returns a tile with appropriate Content-Type set. func (d *Data) ServeTile(uuid dvid.UUID, ctx storage.Context, w http.ResponseWriter, r *http.Request, parts []string) error { if len(parts) < 7 { return fmt.Errorf("'tile' request must be following by plane, scale level, and tile coordinate") } planeStr, scalingStr, coordStr := parts[4], parts[5], parts[6] queryValues := r.URL.Query() noblanksStr := dvid.InstanceName(queryValues.Get("noblanks")) var noblanks bool if noblanksStr == "true" { noblanks = true } var formatStr string if len(parts) >= 8 { formatStr = parts[7] } // Construct the index for this tile plane := dvid.DataShapeString(planeStr) shape, err := plane.DataShape() if err != nil { err = fmt.Errorf("Illegal tile plane: %s (%v)", planeStr, err) server.BadRequest(w, r, err) return err } scaling, err := strconv.ParseUint(scalingStr, 10, 8) if err != nil { err = fmt.Errorf("Illegal tile scale: %s (%v)", scalingStr, err) server.BadRequest(w, r, err) return err } tileCoord, err := dvid.StringToPoint(coordStr, "_") if err != nil { err = fmt.Errorf("Illegal tile coordinate: %s (%v)", coordStr, err) server.BadRequest(w, r, err) return err } if tileCoord.NumDims() != 3 { err = fmt.Errorf("Expected 3d tile coordinate, got %s", coordStr) server.BadRequest(w, r, err) return err } indexZYX := dvid.IndexZYX{tileCoord.Value(0), tileCoord.Value(1), tileCoord.Value(2)} data, err := d.getTileData(ctx, shape, Scaling(scaling), indexZYX) if err != nil { server.BadRequest(w, r, err) return err } if data == nil { if noblanks { http.NotFound(w, r) return nil } img, err := d.getBlankTileImage(uuid, shape, Scaling(scaling)) if err != nil { return err } return dvid.WriteImageHttp(w, img, formatStr) } switch d.Encoding { case LZ4: var img dvid.Image if err := img.Deserialize(data); err != nil { return err } data, err = img.GetPNG() w.Header().Set("Content-type", "image/png") case PNG: w.Header().Set("Content-type", "image/png") case JPG: w.Header().Set("Content-type", "image/jpeg") } if err != nil { server.BadRequest(w, r, err) return err } if _, err = w.Write(data); err != nil { return err } return nil }