This repository has been archived by the owner on May 18, 2021. It is now read-only.
/
blobapi.go
85 lines (65 loc) · 1.58 KB
/
blobapi.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package blobs
import (
"fmt"
"io"
"io/ioutil"
"github.com/gin-gonic/gin"
"github.com/sirupsen/logrus"
)
const headerContentType = "Content-Type"
var log = logrus.WithField("logger", "blob_store")
// Server encapsulates the blob server
type Server struct {
store Store
Engine *gin.Engine
}
// NewFromEngine creates a new blob service from an existing gin engine
func NewFromEngine(store Store, engine *gin.Engine) *Server {
server := &Server{
store: store,
Engine: engine,
}
createBlobAPI(server)
return server
}
func (s *Server) createBlob(c *gin.Context) {
contentType := c.GetHeader(headerContentType)
if len(contentType) == 0 {
c.AbortWithError(400, fmt.Errorf("Missing %s header", headerContentType))
return
}
prefix := c.Param("prefix")
blob, err := s.store.Create(prefix, contentType, c.Request.Body)
if err != nil {
c.AbortWithError(500, err)
return
}
c.JSON(200, blob)
}
func (s *Server) getBlob(c *gin.Context) {
prefix := c.Param("prefix")
blobID := c.Param("blobId")
r, err := s.store.Read(prefix, blobID)
if err != nil {
if err == ErrBlobNotFound {
c.AbortWithError(404, err)
return
}
log.WithError(err).Error("Error querying blobstore")
c.AbortWithError(500, err)
return
}
// avoid internal buffering
if _, err = ioutil.ReadAll(io.TeeReader(r, c.Writer)); err != nil {
log.WithError(err).Error("Error writing blobstore response")
c.AbortWithError(500, err)
return
}
}
func createBlobAPI(s *Server) {
blobs := s.Engine.Group("/blobs")
{
blobs.POST("/:prefix", s.createBlob)
blobs.GET("/:prefix/:blobId", s.getBlob)
}
}