From cbd98a2e3708152be3cfc69fcc6434f887404e28 Mon Sep 17 00:00:00 2001 From: Josh Yan Date: Wed, 3 Jul 2024 16:43:40 -0700 Subject: [PATCH] still works --- api/client.go | 6 +----- cmd/cmd.go | 18 +++++++++++------- server/routes.go | 16 +++------------- 3 files changed, 15 insertions(+), 25 deletions(-) diff --git a/api/client.go b/api/client.go index 2b8bde1be..3c47ac39b 100644 --- a/api/client.go +++ b/api/client.go @@ -367,11 +367,7 @@ func (c *Client) Embeddings(ctx context.Context, req *EmbeddingRequest) (*Embedd // CreateBlob creates a blob from a file on the server. digest is the // expected SHA256 digest of the file, and r represents the file. -func (c *Client) CreateBlob(ctx context.Context, digest string, local bool, r io.Reader) error { - headers := make(http.Header) - if local { - headers.Set("X-Redirect-Create", "1") - } +func (c *Client) CreateBlob(ctx context.Context, digest string, r io.Reader) error { return c.do(ctx, http.MethodPost, fmt.Sprintf("/api/blobs/%s", digest), r, nil) } diff --git a/cmd/cmd.go b/cmd/cmd.go index 932182157..b94c1a284 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -262,6 +262,8 @@ func tempZipFiles(path string) (string, error) { return tempfile.Name(), nil } +var ErrBlobExists = errors.New("blob exists") + func createBlob(cmd *cobra.Command, client *api.Client, path string) (string, error) { bin, err := os.Open(path) if err != nil { @@ -308,13 +310,13 @@ func createBlob(cmd *cobra.Command, client *api.Client, path string) (string, er } */ if client.IsLocal() { config, err := getLocalPath(cmd.Context(), digest) - if err != nil { - return "", err + + if errors.Is(err, ErrBlobExists) { + return digest, nil } - if config == nil { - fmt.Println("config is nil") - return digest, nil + if err != nil { + return "", err } fmt.Println("HI") @@ -330,7 +332,7 @@ func createBlob(cmd *cobra.Command, client *api.Client, path string) (string, er } fmt.Println("DEFAULT") - if err = client.CreateBlob(cmd.Context(), digest, false, bin); err != nil { + if err = client.CreateBlob(cmd.Context(), digest, bin); err != nil { return "", err } return digest, nil @@ -380,11 +382,13 @@ func getLocalPath(ctx context.Context, digest string) (*api.ServerConfig, error) fmt.Println("error unmarshalling response data") return nil, err } + + return &respData, nil } fmt.Println("!!!!!!!!!!") fmt.Println(respData) - return &respData, nil + return nil, ErrBlobExists } func createBlobLocal(path string, dest string) error { diff --git a/server/routes.go b/server/routes.go index b9e7c1c85..6d58f09ed 100644 --- a/server/routes.go +++ b/server/routes.go @@ -940,20 +940,10 @@ func (s *Server) CreateBlobHandler(c *gin.Context) { c.Status(http.StatusOK) return } - fmt.Println("HEIAHOEIHFOAHAEFHAO") - fmt.Println(c.GetHeader("X-Redirect-Create")) - if c.GetHeader("X-Redirect-Create") == "1" { - response := api.ServerConfig{ModelDir: path} - fmt.Println("Hit redirect") - resp, err := json.Marshal(response) - fmt.Println("marshalled response") - if err != nil { - c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) - return - } - c.Header("loc", string(resp)) - fmt.Println("!!!!!!!!!", string(resp)) + if c.GetHeader("X-Redirect-Create") == "1" { + c.Header("Location", path) + fmt.Println("!!!!!!!!!", string(path)) c.Status(http.StatusTemporaryRedirect) return }