File size: 1,572 Bytes
7def60a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package assets

import (
	"embed"
	"fmt"
	"io/fs"
	"os"
	"path/filepath"

	"github.com/mudler/LocalAI/pkg/library"
)

func ResolvePath(dir string, paths ...string) string {
	return filepath.Join(append([]string{dir, "backend-assets"}, paths...)...)
}

func ExtractFiles(content embed.FS, extractDir string) error {
	// Create the target directory if it doesn't exist
	err := os.MkdirAll(extractDir, 0750)
	if err != nil {
		return fmt.Errorf("failed to create directory: %v", err)
	}

	// Walk through the embedded FS and extract files
	err = fs.WalkDir(content, ".", func(path string, d fs.DirEntry, err error) error {
		if err != nil {
			return err
		}

		// Reconstruct the directory structure in the target directory
		targetFile := filepath.Join(extractDir, path)
		if d.IsDir() {
			// Create the directory in the target directory
			err := os.MkdirAll(targetFile, 0750)
			if err != nil {
				return fmt.Errorf("failed to create directory: %v", err)
			}
			return nil
		}

		// Read the file from the embedded FS
		fileData, err := content.ReadFile(path)
		if err != nil {
			return fmt.Errorf("failed to read file: %v", err)
		}

		// Create the file in the target directory
		err = os.WriteFile(targetFile, fileData, 0700)
		if err != nil {
			return fmt.Errorf("failed to write file: %v", err)
		}

		return nil
	})

	// If there is a lib directory, set LD_LIBRARY_PATH to include it
	// we might use this mechanism to carry over e.g. Nvidia CUDA libraries
	// from the embedded FS to the target directory
	library.LoadExtractedLibs(extractDir)

	return err
}