feat(png): add support for generating the images as png
This commit is contained in:
parent
90c34db593
commit
bfa4d5076b
|
|
@ -22,6 +22,8 @@ go run cmd/app/main.go --mermaid=./mermaidcli/node_modules/.bin/mmdc --in=./in -
|
||||||
|
|
||||||
### Diagram creation
|
### Diagram creation
|
||||||
|
|
||||||
|
Use the query param 'type' to change between 'png' and 'svg' defaults to 'svg'.
|
||||||
|
|
||||||
#### POST
|
#### POST
|
||||||
|
|
||||||
Send a CURL request to generate a diagram via `POST`:
|
Send a CURL request to generate a diagram via `POST`:
|
||||||
|
|
|
||||||
|
|
@ -10,11 +10,12 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
// NewDiagram returns a new diagram.
|
// NewDiagram returns a new diagram.
|
||||||
func NewDiagram(description []byte) *Diagram {
|
func NewDiagram(description []byte, imgType string) *Diagram {
|
||||||
return &Diagram{
|
return &Diagram{
|
||||||
description: []byte(strings.TrimSpace(string(description))),
|
description: []byte(strings.TrimSpace(string(description))),
|
||||||
lastTouched: time.Now(),
|
lastTouched: time.Now(),
|
||||||
mu: &sync.RWMutex{},
|
mu: &sync.RWMutex{},
|
||||||
|
imgType: imgType,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -30,6 +31,8 @@ type Diagram struct {
|
||||||
mu *sync.RWMutex
|
mu *sync.RWMutex
|
||||||
// lastTouched is the time that the diagram was last used.
|
// lastTouched is the time that the diagram was last used.
|
||||||
lastTouched time.Time
|
lastTouched time.Time
|
||||||
|
// the type of image to generate svg or png
|
||||||
|
imgType string
|
||||||
}
|
}
|
||||||
|
|
||||||
// Touch updates the last touched time of the diagram.
|
// Touch updates the last touched time of the diagram.
|
||||||
|
|
@ -55,7 +58,7 @@ func (d *Diagram) ID() (string, error) {
|
||||||
|
|
||||||
encoded := base64.StdEncoding.EncodeToString(d.description)
|
encoded := base64.StdEncoding.EncodeToString(d.description)
|
||||||
hash := md5.Sum([]byte(encoded))
|
hash := md5.Sum([]byte(encoded))
|
||||||
d.id = hex.EncodeToString(hash[:])
|
d.id = hex.EncodeToString(hash[:]) + d.imgType
|
||||||
|
|
||||||
return d.id, nil
|
return d.id, nil
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -79,7 +79,7 @@ func (c cachingGenerator) generate(diagram *Diagram) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
inPath := fmt.Sprintf("%s/%s.mmd", c.inPath, id)
|
inPath := fmt.Sprintf("%s/%s.mmd", c.inPath, id)
|
||||||
outPath := fmt.Sprintf("%s/%s.svg", c.outPath, id)
|
outPath := fmt.Sprintf("%s/%s.%s", c.outPath, id, diagram.imgType)
|
||||||
|
|
||||||
if err := ioutil.WriteFile(inPath, diagram.description, 0644); err != nil {
|
if err := ioutil.WriteFile(inPath, diagram.description, 0644); err != nil {
|
||||||
return fmt.Errorf("could not write to input file [%s]: %w", inPath, err)
|
return fmt.Errorf("could not write to input file [%s]: %w", inPath, err)
|
||||||
|
|
|
||||||
|
|
@ -22,8 +22,12 @@ func writeJSON(rw http.ResponseWriter, value interface{}, status int) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func writeSVG(rw http.ResponseWriter, data []byte, status int) {
|
func writeImage(rw http.ResponseWriter, data []byte, status int, imgType string) {
|
||||||
rw.Header().Set("Content-Type", "image/svg+xml")
|
if imgType == "png" {
|
||||||
|
rw.Header().Set("Content-Type", "image/png")
|
||||||
|
} else {
|
||||||
|
rw.Header().Set("Content-Type", "image/svg+xml")
|
||||||
|
}
|
||||||
rw.WriteHeader(status)
|
rw.WriteHeader(status)
|
||||||
if _, err := rw.Write(data); err != nil {
|
if _, err := rw.Write(data); err != nil {
|
||||||
panic("could not write bytes to response: " + err.Error())
|
panic("could not write bytes to response: " + err.Error())
|
||||||
|
|
@ -41,7 +45,7 @@ func writeErr(rw http.ResponseWriter, err error, status int) {
|
||||||
// URLParam is the URL parameter getDiagramFromGET uses to look for data.
|
// URLParam is the URL parameter getDiagramFromGET uses to look for data.
|
||||||
const URLParam = "data"
|
const URLParam = "data"
|
||||||
|
|
||||||
func getDiagramFromGET(rw http.ResponseWriter, r *http.Request) *Diagram {
|
func getDiagramFromGET(rw http.ResponseWriter, r *http.Request, imgType string) *Diagram {
|
||||||
if r.Method != http.MethodGet {
|
if r.Method != http.MethodGet {
|
||||||
writeErr(rw, fmt.Errorf("expected HTTP method GET"), http.StatusBadRequest)
|
writeErr(rw, fmt.Errorf("expected HTTP method GET"), http.StatusBadRequest)
|
||||||
return nil
|
return nil
|
||||||
|
|
@ -59,11 +63,11 @@ func getDiagramFromGET(rw http.ResponseWriter, r *http.Request) *Diagram {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create a diagram from the description
|
// Create a diagram from the description
|
||||||
d := NewDiagram([]byte(data))
|
d := NewDiagram([]byte(data), imgType)
|
||||||
return d
|
return d
|
||||||
}
|
}
|
||||||
|
|
||||||
func getDiagramFromPOST(rw http.ResponseWriter, r *http.Request) *Diagram {
|
func getDiagramFromPOST(rw http.ResponseWriter, r *http.Request, imgType string) *Diagram {
|
||||||
if r.Method != http.MethodPost {
|
if r.Method != http.MethodPost {
|
||||||
writeErr(rw, fmt.Errorf("expected HTTP method POST"), http.StatusBadRequest)
|
writeErr(rw, fmt.Errorf("expected HTTP method POST"), http.StatusBadRequest)
|
||||||
return nil
|
return nil
|
||||||
|
|
@ -76,7 +80,7 @@ func getDiagramFromPOST(rw http.ResponseWriter, r *http.Request) *Diagram {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create a diagram from the description
|
// Create a diagram from the description
|
||||||
d := NewDiagram(bytes)
|
d := NewDiagram(bytes, imgType)
|
||||||
return d
|
return d
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -85,11 +89,21 @@ func generateHTTPHandler(generator Generator) func(rw http.ResponseWriter, r *ht
|
||||||
return func(rw http.ResponseWriter, r *http.Request) {
|
return func(rw http.ResponseWriter, r *http.Request) {
|
||||||
var diagram *Diagram
|
var diagram *Diagram
|
||||||
|
|
||||||
|
var imgType = r.URL.Query().Get("type")
|
||||||
|
if imgType == "" {
|
||||||
|
imgType = "svg"
|
||||||
|
}
|
||||||
|
|
||||||
|
if imgType != "png" && imgType != "svg" {
|
||||||
|
writeErr(rw, fmt.Errorf("unsupported image type (%s) use svg or png", imgType), http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
switch r.Method {
|
switch r.Method {
|
||||||
case http.MethodGet:
|
case http.MethodGet:
|
||||||
diagram = getDiagramFromGET(rw, r)
|
diagram = getDiagramFromGET(rw, r, imgType)
|
||||||
case http.MethodPost:
|
case http.MethodPost:
|
||||||
diagram = getDiagramFromPOST(rw, r)
|
diagram = getDiagramFromPOST(rw, r, imgType)
|
||||||
default:
|
default:
|
||||||
writeErr(rw, fmt.Errorf("unexpected HTTP method %s", r.Method), http.StatusBadRequest)
|
writeErr(rw, fmt.Errorf("unexpected HTTP method %s", r.Method), http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
|
|
@ -112,6 +126,6 @@ func generateHTTPHandler(generator Generator) func(rw http.ResponseWriter, r *ht
|
||||||
writeErr(rw, fmt.Errorf("could not read diagram bytes: %s", err), http.StatusInternalServerError)
|
writeErr(rw, fmt.Errorf("could not read diagram bytes: %s", err), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
writeSVG(rw, diagramBytes, http.StatusOK)
|
writeImage(rw, diagramBytes, http.StatusOK, imgType)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue