mirror of
https://github.com/mudler/LocalAI.git
synced 2025-05-28 14:35:00 +00:00
parent
2a45a99737
commit
60db5957d3
15 changed files with 644 additions and 194 deletions
56
pkg/utils/untar.go
Normal file
56
pkg/utils/untar.go
Normal file
|
@ -0,0 +1,56 @@
|
|||
package utils
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/mholt/archiver/v3"
|
||||
)
|
||||
|
||||
func IsArchive(file string) bool {
|
||||
uaIface, err := archiver.ByExtension(file)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
_, ok := uaIface.(archiver.Unarchiver)
|
||||
return ok
|
||||
}
|
||||
|
||||
func ExtractArchive(archive, dst string) error {
|
||||
uaIface, err := archiver.ByExtension(archive)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
un, ok := uaIface.(archiver.Unarchiver)
|
||||
if !ok {
|
||||
return fmt.Errorf("format specified by source filename is not an archive format: %s (%T)", archive, uaIface)
|
||||
}
|
||||
|
||||
mytar := &archiver.Tar{
|
||||
OverwriteExisting: true,
|
||||
MkdirAll: true,
|
||||
ImplicitTopLevelFolder: false,
|
||||
ContinueOnError: true,
|
||||
}
|
||||
|
||||
switch v := uaIface.(type) {
|
||||
case *archiver.Tar:
|
||||
uaIface = mytar
|
||||
case *archiver.TarBrotli:
|
||||
v.Tar = mytar
|
||||
case *archiver.TarBz2:
|
||||
v.Tar = mytar
|
||||
case *archiver.TarGz:
|
||||
v.Tar = mytar
|
||||
case *archiver.TarLz4:
|
||||
v.Tar = mytar
|
||||
case *archiver.TarSz:
|
||||
v.Tar = mytar
|
||||
case *archiver.TarXz:
|
||||
v.Tar = mytar
|
||||
case *archiver.TarZstd:
|
||||
v.Tar = mytar
|
||||
}
|
||||
return un.Unarchive(archive, dst)
|
||||
}
|
37
pkg/utils/uri.go
Normal file
37
pkg/utils/uri.go
Normal file
|
@ -0,0 +1,37 @@
|
|||
package utils
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func GetURI(url string, f func(i []byte) error) error {
|
||||
if strings.HasPrefix(url, "file://") {
|
||||
rawURL := strings.TrimPrefix(url, "file://")
|
||||
// Read the response body
|
||||
body, err := ioutil.ReadFile(rawURL)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Unmarshal YAML data into a struct
|
||||
return f(body)
|
||||
}
|
||||
|
||||
// Send a GET request to the URL
|
||||
response, err := http.Get(url)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer response.Body.Close()
|
||||
|
||||
// Read the response body
|
||||
body, err := ioutil.ReadAll(response.Body)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Unmarshal YAML data into a struct
|
||||
return f(body)
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue