From 2666d3c206f31e2ee3cf6cc8ef6643816edac75b Mon Sep 17 00:00:00 2001 From: Michael Yang Date: Wed, 12 Jul 2023 19:07:15 -0700 Subject: [PATCH] fix pull race --- server/models.go | 14 +------------- server/routes.go | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 13 deletions(-) diff --git a/server/models.go b/server/models.go index b3eca189..99f57f55 100644 --- a/server/models.go +++ b/server/models.go @@ -76,22 +76,10 @@ func saveModel(model *Model, fn func(total, completed int64)) error { return fmt.Errorf("failed to download model: %w", err) } - // check if completed file exists - fi, err := os.Stat(model.FullName()) - switch { - case errors.Is(err, os.ErrNotExist): - // noop, file doesn't exist so create it - case err != nil: - return fmt.Errorf("stat: %w", err) - default: - fn(fi.Size(), fi.Size()) - return nil - } - var size int64 // completed file doesn't exist, check partial file - fi, err = os.Stat(model.TempFile()) + fi, err := os.Stat(model.TempFile()) switch { case errors.Is(err, os.ErrNotExist): // noop, file doesn't exist so create it diff --git a/server/routes.go b/server/routes.go index 2d7badb5..d93a79fc 100644 --- a/server/routes.go +++ b/server/routes.go @@ -105,6 +105,24 @@ func pull(c *gin.Context) { return } + // check if completed file exists + fi, err := os.Stat(remote.FullName()) + switch { + case errors.Is(err, os.ErrNotExist): + // noop, file doesn't exist so create it + case err != nil: + c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) + return + default: + c.JSON(http.StatusOK, api.PullProgress{ + Total: fi.Size(), + Completed: fi.Size(), + Percent: 100, + }) + + return + } + ch := make(chan any) go stream(c, ch)