From 0f541a03677863026ffa17367359a7f0bbfb6afe Mon Sep 17 00:00:00 2001 From: Michael Yang Date: Wed, 30 Aug 2023 11:10:27 -0400 Subject: [PATCH 1/4] s/ListResponseModel/ModelResponse/ --- api/types.go | 4 ++-- server/routes.go | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/api/types.go b/api/types.go index 4f46dcfc..8c97a792 100644 --- a/api/types.go +++ b/api/types.go @@ -88,10 +88,10 @@ type PushRequest struct { } type ListResponse struct { - Models []ListResponseModel `json:"models"` + Models []ModelResponse `json:"models"` } -type ListResponseModel struct { +type ModelResponse struct { Name string `json:"name"` ModifiedAt time.Time `json:"modified_at"` Size int `json:"size"` diff --git a/server/routes.go b/server/routes.go index 0519cbf1..39e623ff 100644 --- a/server/routes.go +++ b/server/routes.go @@ -365,7 +365,7 @@ func DeleteModelHandler(c *gin.Context) { } func ListModelsHandler(c *gin.Context) { - var models []api.ListResponseModel + var models []api.ModelResponse fp, err := GetManifestPath() if err != nil { c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) @@ -398,7 +398,7 @@ func ListModelsHandler(c *gin.Context) { log.Printf("skipping file: %s", fp) return nil } - model := api.ListResponseModel{ + model := api.ModelResponse{ Name: mp.GetShortTagname(), Size: manifest.GetTotalSize(), Digest: digest, From eeb40a672c80f0cc06b08faaebec67b954202e4f Mon Sep 17 00:00:00 2001 From: Michael Yang Date: Wed, 30 Aug 2023 14:14:12 -0400 Subject: [PATCH 2/4] fix list models for windows --- server/modelpath.go | 2 +- server/routes.go | 39 +++++++++++++-------------------------- 2 files changed, 14 insertions(+), 27 deletions(-) diff --git a/server/modelpath.go b/server/modelpath.go index aa83e7a6..88edad99 100644 --- a/server/modelpath.go +++ b/server/modelpath.go @@ -46,7 +46,7 @@ func ParseModelPath(name string) ModelPath { name = after } - parts := strings.Split(name, "/") + parts := strings.Split(name, string(os.PathSeparator)) switch len(parts) { case 3: mp.Registry = parts[0] diff --git a/server/routes.go b/server/routes.go index 39e623ff..f0762416 100644 --- a/server/routes.go +++ b/server/routes.go @@ -3,7 +3,6 @@ package server import ( "context" "encoding/json" - "errors" "fmt" "io" "log" @@ -371,26 +370,12 @@ func ListModelsHandler(c *gin.Context) { c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) return } - err = filepath.Walk(fp, func(path string, info os.FileInfo, err error) error { - if err != nil { - if errors.Is(err, os.ErrNotExist) { - log.Printf("manifest file does not exist: %s", fp) - return nil - } - return err - } + + walkFunc := func(path string, info os.FileInfo, _ error) error { if !info.IsDir() { - fi, err := os.Stat(path) - if err != nil { - log.Printf("skipping file: %s", fp) - return nil - } - path := path[len(fp)+1:] - slashIndex := strings.LastIndex(path, "/") - if slashIndex == -1 { - return nil - } - tag := path[:slashIndex] + ":" + path[slashIndex+1:] + dir, file := filepath.Split(path) + dir = strings.Trim(strings.TrimPrefix(dir, fp), string(os.PathSeparator)) + tag := strings.Join([]string{dir, file}, ":") mp := ParseModelPath(tag) manifest, digest, err := GetManifest(mp) @@ -398,17 +383,19 @@ func ListModelsHandler(c *gin.Context) { log.Printf("skipping file: %s", fp) return nil } - model := api.ModelResponse{ + + models = append(models, api.ModelResponse{ Name: mp.GetShortTagname(), Size: manifest.GetTotalSize(), Digest: digest, - ModifiedAt: fi.ModTime(), - } - models = append(models, model) + ModifiedAt: info.ModTime(), + }) } + return nil - }) - if err != nil { + } + + if err := filepath.Walk(fp, walkFunc); err != nil { c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) return } From ae950b00f19ccef2c02523cc9519de535500d9b3 Mon Sep 17 00:00:00 2001 From: Michael Yang Date: Wed, 30 Aug 2023 14:31:12 -0400 Subject: [PATCH 3/4] windows: fix delete --- server/images.go | 54 ++++++++++++++++++++++++------------------------ 1 file changed, 27 insertions(+), 27 deletions(-) diff --git a/server/images.go b/server/images.go index fad939f9..cb03cc2b 100644 --- a/server/images.go +++ b/server/images.go @@ -859,38 +859,38 @@ func DeleteModel(name string) error { if err != nil { return err } - err = filepath.Walk(fp, func(path string, info os.FileInfo, err error) error { + + walkFunc := func(path string, info os.FileInfo, _ error) error { + if info.IsDir() { + return nil + } + + dir, file := filepath.Split(path) + dir = strings.Trim(strings.TrimPrefix(dir, fp), string(os.PathSeparator)) + tag := strings.Join([]string{dir, file}, ":") + fmp := ParseModelPath(tag) + + // skip the manifest we're trying to delete + if mp.GetFullTagname() == fmp.GetFullTagname() { + return nil + } + + // save (i.e. delete from the deleteMap) any files used in other manifests + manifest, _, err := GetManifest(fmp) if err != nil { - return err + log.Printf("skipping file: %s", fp) + return nil } - if !info.IsDir() { - path := path[len(fp)+1:] - slashIndex := strings.LastIndex(path, "/") - if slashIndex == -1 { - return nil - } - tag := path[:slashIndex] + ":" + path[slashIndex+1:] - fmp := ParseModelPath(tag) - // skip the manifest we're trying to delete - if mp.GetFullTagname() == fmp.GetFullTagname() { - return nil - } - - // save (i.e. delete from the deleteMap) any files used in other manifests - manifest, _, err := GetManifest(fmp) - if err != nil { - log.Printf("skipping file: %s", fp) - return nil - } - for _, layer := range manifest.Layers { - delete(deleteMap, layer.Digest) - } - delete(deleteMap, manifest.Config.Digest) + for _, layer := range manifest.Layers { + delete(deleteMap, layer.Digest) } + + delete(deleteMap, manifest.Config.Digest) return nil - }) - if err != nil { + } + + if err := filepath.Walk(fp, walkFunc); err != nil { return err } From 1c8fd627ad9e96f58bad346d5f5a13750700f76a Mon Sep 17 00:00:00 2001 From: Michael Yang Date: Wed, 30 Aug 2023 16:01:23 -0400 Subject: [PATCH 4/4] windows: fix create modelfile --- server/images.go | 21 +++++---------------- 1 file changed, 5 insertions(+), 16 deletions(-) diff --git a/server/images.go b/server/images.go index cb03cc2b..40e38256 100644 --- a/server/images.go +++ b/server/images.go @@ -235,8 +235,8 @@ func GetModel(name string) (*Model, error) { func filenameWithPath(path, f string) (string, error) { // if filePath starts with ~/, replace it with the user's home directory. - if strings.HasPrefix(f, "~/") { - parts := strings.Split(f, "/") + if strings.HasPrefix(f, fmt.Sprintf("~%s", string(os.PathSeparator))) { + parts := strings.Split(f, string(os.PathSeparator)) home, err := os.UserHomeDir() if err != nil { return "", fmt.Errorf("failed to open file: %v", err) @@ -374,20 +374,9 @@ func CreateModel(ctx context.Context, name string, path string, fn func(resp api case "adapter": fn(api.ProgressResponse{Status: fmt.Sprintf("creating model %s layer", c.Name)}) - fp := c.Args - if strings.HasPrefix(fp, "~/") { - parts := strings.Split(fp, "/") - home, err := os.UserHomeDir() - if err != nil { - return fmt.Errorf("failed to open file: %v", err) - } - - fp = filepath.Join(home, filepath.Join(parts[1:]...)) - } - - // If filePath is not an absolute path, make it relative to the modelfile path - if !filepath.IsAbs(fp) { - fp = filepath.Join(filepath.Dir(path), fp) + fp, err := filenameWithPath(path, c.Args) + if err != nil { + return err } // create a model from this specified file