Merge pull request #612 from jmorganca/mxyng/prune-empty-directories

prune empty directories
This commit is contained in:
Michael Yang 2023-09-29 11:23:39 -07:00 committed by GitHub
commit 9333b0cc82
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 54 additions and 0 deletions

View file

@ -745,6 +745,15 @@ func RunServer(cmd *cobra.Command, _ []string) error {
if err := server.PruneLayers(); err != nil { if err := server.PruneLayers(); err != nil {
return err return err
} }
manifestsPath, err := server.GetManifestPath()
if err != nil {
return err
}
if err := server.PruneDirectory(manifestsPath); err != nil {
return err
}
} }
return server.Serve(ln, origins) return server.Serve(ln, origins)

View file

@ -1005,6 +1005,39 @@ func PruneLayers() error {
return nil return nil
} }
func PruneDirectory(path string) error {
info, err := os.Lstat(path)
if err != nil {
return err
}
if info.IsDir() && info.Mode()&os.ModeSymlink == 0 {
entries, err := os.ReadDir(path)
if err != nil {
return err
}
for _, entry := range entries {
if err := PruneDirectory(filepath.Join(path, entry.Name())); err != nil {
return err
}
}
entries, err = os.ReadDir(path)
if err != nil {
return err
}
if len(entries) > 0 {
return nil
}
return os.Remove(path)
}
return nil
}
func DeleteModel(name string) error { func DeleteModel(name string) error {
mp := ParseModelPath(name) mp := ParseModelPath(name)
manifest, _, err := GetManifest(mp) manifest, _, err := GetManifest(mp)

View file

@ -378,6 +378,18 @@ func DeleteModelHandler(c *gin.Context) {
} }
return return
} }
manifestsPath, err := GetManifestPath()
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
if err := PruneDirectory(manifestsPath); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, nil) c.JSON(http.StatusOK, nil)
} }