Unverified Commit 9304f0e7 authored by Michael Yang's avatar Michael Yang Committed by GitHub
Browse files

Merge pull request #443 from jmorganca/mxyng/fix-list-models

windows: fix filepath bugs
parents 6578b2f8 1c8fd627
...@@ -88,10 +88,10 @@ type PushRequest struct { ...@@ -88,10 +88,10 @@ type PushRequest struct {
} }
type ListResponse struct { type ListResponse struct {
Models []ListResponseModel `json:"models"` Models []ModelResponse `json:"models"`
} }
type ListResponseModel struct { type ModelResponse struct {
Name string `json:"name"` Name string `json:"name"`
ModifiedAt time.Time `json:"modified_at"` ModifiedAt time.Time `json:"modified_at"`
Size int `json:"size"` Size int `json:"size"`
......
...@@ -235,8 +235,8 @@ func GetModel(name string) (*Model, error) { ...@@ -235,8 +235,8 @@ func GetModel(name string) (*Model, error) {
func filenameWithPath(path, f string) (string, error) { func filenameWithPath(path, f string) (string, error) {
// if filePath starts with ~/, replace it with the user's home directory. // if filePath starts with ~/, replace it with the user's home directory.
if strings.HasPrefix(f, "~/") { if strings.HasPrefix(f, fmt.Sprintf("~%s", string(os.PathSeparator))) {
parts := strings.Split(f, "/") parts := strings.Split(f, string(os.PathSeparator))
home, err := os.UserHomeDir() home, err := os.UserHomeDir()
if err != nil { if err != nil {
return "", fmt.Errorf("failed to open file: %v", err) 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 ...@@ -374,20 +374,9 @@ func CreateModel(ctx context.Context, name string, path string, fn func(resp api
case "adapter": case "adapter":
fn(api.ProgressResponse{Status: fmt.Sprintf("creating model %s layer", c.Name)}) fn(api.ProgressResponse{Status: fmt.Sprintf("creating model %s layer", c.Name)})
fp := c.Args fp, err := filenameWithPath(path, c.Args)
if strings.HasPrefix(fp, "~/") { if err != nil {
parts := strings.Split(fp, "/") return err
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)
} }
// create a model from this specified file // create a model from this specified file
...@@ -859,38 +848,38 @@ func DeleteModel(name string) error { ...@@ -859,38 +848,38 @@ func DeleteModel(name string) error {
if err != nil { if err != nil {
return err return err
} }
err = filepath.Walk(fp, func(path string, info os.FileInfo, err error) error {
if err != nil { walkFunc := func(path string, info os.FileInfo, _ error) error {
return err if info.IsDir() {
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 dir, file := filepath.Split(path)
if mp.GetFullTagname() == fmp.GetFullTagname() { dir = strings.Trim(strings.TrimPrefix(dir, fp), string(os.PathSeparator))
return nil tag := strings.Join([]string{dir, file}, ":")
} fmp := ParseModelPath(tag)
// save (i.e. delete from the deleteMap) any files used in other manifests // skip the manifest we're trying to delete
manifest, _, err := GetManifest(fmp) if mp.GetFullTagname() == fmp.GetFullTagname() {
if err != nil { return nil
log.Printf("skipping file: %s", fp) }
return nil
} // save (i.e. delete from the deleteMap) any files used in other manifests
for _, layer := range manifest.Layers { manifest, _, err := GetManifest(fmp)
delete(deleteMap, layer.Digest) if err != nil {
} log.Printf("skipping file: %s", fp)
delete(deleteMap, manifest.Config.Digest) return nil
} }
for _, layer := range manifest.Layers {
delete(deleteMap, layer.Digest)
}
delete(deleteMap, manifest.Config.Digest)
return nil return nil
}) }
if err != nil {
if err := filepath.Walk(fp, walkFunc); err != nil {
return err return err
} }
......
...@@ -46,7 +46,7 @@ func ParseModelPath(name string) ModelPath { ...@@ -46,7 +46,7 @@ func ParseModelPath(name string) ModelPath {
name = after name = after
} }
parts := strings.Split(name, "/") parts := strings.Split(name, string(os.PathSeparator))
switch len(parts) { switch len(parts) {
case 3: case 3:
mp.Registry = parts[0] mp.Registry = parts[0]
......
...@@ -3,7 +3,6 @@ package server ...@@ -3,7 +3,6 @@ package server
import ( import (
"context" "context"
"encoding/json" "encoding/json"
"errors"
"fmt" "fmt"
"io" "io"
"log" "log"
...@@ -365,32 +364,18 @@ func DeleteModelHandler(c *gin.Context) { ...@@ -365,32 +364,18 @@ func DeleteModelHandler(c *gin.Context) {
} }
func ListModelsHandler(c *gin.Context) { func ListModelsHandler(c *gin.Context) {
var models []api.ListResponseModel var models []api.ModelResponse
fp, err := GetManifestPath() fp, err := GetManifestPath()
if err != nil { if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return return
} }
err = filepath.Walk(fp, func(path string, info os.FileInfo, err error) error {
if err != nil { walkFunc := func(path string, info os.FileInfo, _ error) error {
if errors.Is(err, os.ErrNotExist) {
log.Printf("manifest file does not exist: %s", fp)
return nil
}
return err
}
if !info.IsDir() { if !info.IsDir() {
fi, err := os.Stat(path) dir, file := filepath.Split(path)
if err != nil { dir = strings.Trim(strings.TrimPrefix(dir, fp), string(os.PathSeparator))
log.Printf("skipping file: %s", fp) tag := strings.Join([]string{dir, file}, ":")
return nil
}
path := path[len(fp)+1:]
slashIndex := strings.LastIndex(path, "/")
if slashIndex == -1 {
return nil
}
tag := path[:slashIndex] + ":" + path[slashIndex+1:]
mp := ParseModelPath(tag) mp := ParseModelPath(tag)
manifest, digest, err := GetManifest(mp) manifest, digest, err := GetManifest(mp)
...@@ -398,17 +383,19 @@ func ListModelsHandler(c *gin.Context) { ...@@ -398,17 +383,19 @@ func ListModelsHandler(c *gin.Context) {
log.Printf("skipping file: %s", fp) log.Printf("skipping file: %s", fp)
return nil return nil
} }
model := api.ListResponseModel{
models = append(models, api.ModelResponse{
Name: mp.GetShortTagname(), Name: mp.GetShortTagname(),
Size: manifest.GetTotalSize(), Size: manifest.GetTotalSize(),
Digest: digest, Digest: digest,
ModifiedAt: fi.ModTime(), ModifiedAt: info.ModTime(),
} })
models = append(models, model)
} }
return nil return nil
}) }
if err != nil {
if err := filepath.Walk(fp, walkFunc); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return return
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment