From e1c9a2a00fd555f33dae7f97b7900a9d636166b3 Mon Sep 17 00:00:00 2001 From: Michael Yang Date: Fri, 5 Apr 2024 09:30:09 -0700 Subject: [PATCH] no blob create if already exists --- api/client.go | 14 +------------- server/routes.go | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 13 deletions(-) diff --git a/api/client.go b/api/client.go index a37a4cbb..92dcd22b 100644 --- a/api/client.go +++ b/api/client.go @@ -5,7 +5,6 @@ import ( "bytes" "context" "encoding/json" - "errors" "fmt" "io" "net" @@ -301,18 +300,7 @@ func (c *Client) Embeddings(ctx context.Context, req *EmbeddingRequest) (*Embedd } func (c *Client) CreateBlob(ctx context.Context, digest string, r io.Reader) error { - if err := c.do(ctx, http.MethodHead, fmt.Sprintf("/api/blobs/%s", digest), nil, nil); err != nil { - var statusError StatusError - if !errors.As(err, &statusError) || statusError.StatusCode != http.StatusNotFound { - return err - } - - if err := c.do(ctx, http.MethodPost, fmt.Sprintf("/api/blobs/%s", digest), r, nil); err != nil { - return err - } - } - - return nil + return c.do(ctx, http.MethodPost, fmt.Sprintf("/api/blobs/%s", digest), r, nil) } func (c *Client) Version(ctx context.Context) (string, error) { diff --git a/server/routes.go b/server/routes.go index c05f1942..8beee349 100644 --- a/server/routes.go +++ b/server/routes.go @@ -913,6 +913,24 @@ func HeadBlobHandler(c *gin.Context) { } func CreateBlobHandler(c *gin.Context) { + path, err := GetBlobsPath(c.Param("digest")) + if err != nil { + c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": err.Error()}) + return + } + + _, err = os.Stat(path) + switch { + case errors.Is(err, os.ErrNotExist): + // noop + case err != nil: + c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) + return + default: + c.Status(http.StatusOK) + return + } + layer, err := NewLayer(c.Request.Body, "") if err != nil { c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"error": err.Error()})