mirror of
https://github.com/mudler/LocalAI.git
synced 2026-10-28 09:08:54 -04:00
fix(downloader): make file:// installs reachable again
DownloadFileWithContext already has a branch that copies from a local
file, but it could never run. Before reaching it the function decides
whether the destination is fetchable with
} else if !os.IsNotExist(err) || !URI(url).LooksLikeHTTPURL() {
and LooksLikeHTTPURL is http(s) only, so any URI resolving to a local
path is rejected there. Falling through requires the destination to be
missing AND the source to be an HTTP URL, which a file:// source never
is -- leaving the local-source branch below unreachable.
A first import always has a missing destination, so importing
file:///path/to/model.gguf always failed, with an error that listed
file:// among the supported schemes (#11701).
Name the local-source condition once as URI.hasLocalSource and use it
both to admit the destination and to pick the source, so the two cannot
drift apart again.
Signed-off-by: Tai An <antai12232931@outlook.com>
This commit is contained in:
1 parent
8aeea4cdde
commit
9901103aac
2 files changed
+64
-2
No files matched your search
@@ -0,0 +1,48 @@
|
||||
package downloader_test
|
||||
|
||||
import (
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
. "github.com/mudler/LocalAI/pkg/downloader"
|
||||
. "github.com/onsi/ginkgo/v2"
|
||||
. "github.com/onsi/gomega"
|
||||
)
|
||||
|
||||
var _ = Describe("file:// sources", func() {
|
||||
var noProgress = func(string, string, string, float64) {}
|
||||
|
||||
It("copies a file:// source into a destination that does not exist yet", func() {
|
||||
// the layout from issue #11701: the source sits in a nested directory,
|
||||
// the destination is the flat models dir, so it cannot already exist
|
||||
srcDir := GinkgoT().TempDir()
|
||||
nested := filepath.Join(srcDir, "InternScience", "Agents-A1-4B-Q8_0-GGUF")
|
||||
Expect(os.MkdirAll(nested, 0o755)).To(Succeed())
|
||||
srcPath := filepath.Join(nested, "Agents-A1-4B-Q8_0.gguf")
|
||||
payload := []byte("GGUF-not-really-but-enough-bytes")
|
||||
Expect(os.WriteFile(srcPath, payload, 0o600)).To(Succeed())
|
||||
|
||||
sum := sha256.Sum256(payload)
|
||||
destPath := filepath.Join(GinkgoT().TempDir(), "Agents-A1-4B-Q8_0.gguf")
|
||||
|
||||
uri := URI("file://" + srcPath)
|
||||
Expect(uri.DownloadFile(destPath, hex.EncodeToString(sum[:]), 1, 1, noProgress)).To(Succeed())
|
||||
|
||||
got, err := os.ReadFile(destPath)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(got).To(Equal(payload))
|
||||
})
|
||||
|
||||
It("reports the missing source when a file:// source does not exist", func() {
|
||||
destPath := filepath.Join(GinkgoT().TempDir(), "absent.gguf")
|
||||
missing := filepath.Join(GinkgoT().TempDir(), "absent.gguf")
|
||||
|
||||
err := URI("file://"+missing).DownloadFile(destPath, "", 1, 1, noProgress)
|
||||
Expect(err).To(HaveOccurred())
|
||||
// the error must name the source that could not be read, not claim the
|
||||
// destination is an unrecognized URL
|
||||
Expect(err.Error()).To(ContainSubstring(missing))
|
||||
})
|
||||
})
|
||||
+16
-2
@@ -272,6 +272,20 @@ func (u URI) LooksLikeURL() bool {
|
||||
strings.HasPrefix(string(u), GithubURI2)
|
||||
}
|
||||
|
||||
// hasLocalSource reports whether the URI names a local file to copy from
|
||||
// rather than a URL to fetch. DownloadFileWithContext both decides whether the
|
||||
// destination is reachable and picks its source with this, so that the two
|
||||
// cannot drift apart: they did, and every "file://" install failed because the
|
||||
// reachability check admitted http(s) only, leaving the local-source branch
|
||||
// unreachable for any destination that did not already exist.
|
||||
func (u URI) hasLocalSource() bool {
|
||||
if strings.HasPrefix(string(u), LocalPrefix) {
|
||||
return true
|
||||
}
|
||||
_, err := os.Stat(u.ResolveURL())
|
||||
return err == nil
|
||||
}
|
||||
|
||||
func (u URI) LooksLikeHTTPURL() bool {
|
||||
return strings.HasPrefix(string(u), HTTPPrefix) ||
|
||||
strings.HasPrefix(string(u), HTTPSPrefix)
|
||||
@@ -643,7 +657,7 @@ func (uri URI) DownloadFileWithContext(ctx context.Context, filePath, sha string
|
||||
return nil
|
||||
}
|
||||
}
|
||||
} else if !os.IsNotExist(err) || !URI(url).LooksLikeHTTPURL() {
|
||||
} else if !os.IsNotExist(err) || !(URI(url).LooksLikeHTTPURL() || uri.hasLocalSource()) {
|
||||
// Error occurred while checking file existence
|
||||
return fmt.Errorf("could not fetch %q: local file does not exist (%v) and %q is not a recognized downloadable URL (supported schemes: %s)", filePath, err, url, strings.Join([]string{HTTPPrefix, HTTPSPrefix, LocalPrefix, HuggingFacePrefix, HuggingFacePrefix1, OllamaPrefix, OCIPrefix, OCIFilePrefix, GithubURI2}, ", "))
|
||||
}
|
||||
@@ -720,7 +734,7 @@ func (uri URI) DownloadFileWithContext(ctx context.Context, filePath, sha string
|
||||
|
||||
var source io.ReadCloser
|
||||
var contentLength int64
|
||||
if _, e := os.Stat(uri.ResolveURL()); strings.HasPrefix(string(uri), LocalPrefix) || e == nil {
|
||||
if uri.hasLocalSource() {
|
||||
file, err := os.Open(uri.ResolveURL())
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to open file %q: %v", uri.ResolveURL(), err)
|
||||
|
||||
Reference in new issue
Block a user