Allow to generate graph in formats supported by dot. #169

This commit is contained in:
Andrey Smirnov
2015-01-13 19:22:24 +03:00
parent 67ce828eeb
commit 2816647809
2 changed files with 13 additions and 5 deletions

View File

@@ -6,17 +6,20 @@ import (
"github.com/gin-gonic/gin"
"github.com/smira/aptly/deb"
"io"
"mime"
"os"
"os/exec"
)
// GET /api/graph
// GET /api/graph.:ext
func apiGraph(c *gin.Context) {
var (
err error
output []byte
)
ext := c.Params.ByName("ext")
factory := context.CollectionFactory()
factory.RemoteRepoCollection().RLock()
@@ -24,7 +27,7 @@ func apiGraph(c *gin.Context) {
factory.LocalRepoCollection().RLock()
defer factory.LocalRepoCollection().RUnlock()
factory.SnapshotCollection().RLock()
defer factory.LocalRepoCollection().RUnlock()
defer factory.SnapshotCollection().RUnlock()
factory.PublishedRepoCollection().RLock()
defer factory.PublishedRepoCollection().RUnlock()
@@ -36,7 +39,7 @@ func apiGraph(c *gin.Context) {
buf := bytes.NewBufferString(graph.String())
command := exec.Command("dot", "-Tpng")
command := exec.Command("dot", "-T"+ext)
command.Stderr = os.Stderr
stdin, err := command.StdinPipe()
@@ -63,5 +66,10 @@ func apiGraph(c *gin.Context) {
return
}
c.Data(200, "image/png", output)
mimeType := mime.TypeByExtension("." + ext)
if mimeType == "" {
mimeType = "application/octet-stream"
}
c.Data(200, mimeType, output)
}

View File

@@ -53,7 +53,7 @@ func Router(c *ctx.AptlyContext) http.Handler {
}
{
root.GET("/graph", apiGraph)
root.GET("/graph.:ext", apiGraph)
}
return router