mirror of
https://github.com/wavetermdev/backup.git
synced 2026-08-05 13:57:07 -07:00
create block using a blockdef. better controller control. preview that takes a file. atom caching per block. lots of updates
This commit is contained in:
@@ -7,13 +7,44 @@ import (
|
||||
"fmt"
|
||||
|
||||
"github.com/wavetermdev/thenextwave/pkg/blockcontroller"
|
||||
"github.com/wavetermdev/thenextwave/pkg/util/utilfn"
|
||||
)
|
||||
|
||||
type BlockService struct{}
|
||||
|
||||
func (bs *BlockService) StartBlock(blockId string) error {
|
||||
blockcontroller.StartBlockController(blockId)
|
||||
return nil
|
||||
func (bs *BlockService) CreateBlock(bdefMap map[string]any, rtOptsMap map[string]any) (map[string]any, error) {
|
||||
var bdef blockcontroller.BlockDef
|
||||
err := utilfn.JsonMapToStruct(bdefMap, &bdef)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error unmarshalling BlockDef: %w", err)
|
||||
}
|
||||
var rtOpts blockcontroller.RuntimeOpts
|
||||
err = utilfn.JsonMapToStruct(rtOptsMap, &rtOpts)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error unmarshalling RuntimeOpts: %w", err)
|
||||
}
|
||||
blockData, err := blockcontroller.CreateBlock(&bdef, &rtOpts)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error creating block: %w", err)
|
||||
}
|
||||
rtnMap, err := utilfn.StructToJsonMap(blockData)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error marshalling BlockData: %w", err)
|
||||
}
|
||||
return rtnMap, nil
|
||||
}
|
||||
|
||||
func (bs *BlockService) GetBlockData(blockId string) (map[string]any, error) {
|
||||
blockData := blockcontroller.GetBlockData(blockId)
|
||||
if blockData == nil {
|
||||
return nil, nil
|
||||
}
|
||||
rtnMap, err := utilfn.StructToJsonMap(blockData)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error marshalling BlockData: %w", err)
|
||||
}
|
||||
return rtnMap, nil
|
||||
|
||||
}
|
||||
|
||||
func (bs *BlockService) SendCommand(blockId string, cmdMap map[string]any) error {
|
||||
|
||||
@@ -7,19 +7,64 @@ import (
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
"os"
|
||||
"time"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/wavetermdev/thenextwave/pkg/util/utilfn"
|
||||
"github.com/wavetermdev/thenextwave/pkg/wavebase"
|
||||
)
|
||||
|
||||
type FileService struct{}
|
||||
|
||||
func (fs *FileService) ReadFile(path string) (string, error) {
|
||||
path = wavebase.ExpandHomeDir(path)
|
||||
barr, err := os.ReadFile(path)
|
||||
type FileInfo struct {
|
||||
Path string `json:"path"` // cleaned path
|
||||
NotFound bool `json:"notfound,omitempty"`
|
||||
Size int64 `json:"size"`
|
||||
Mode os.FileMode `json:"mode"`
|
||||
ModTime int64 `json:"modtime"`
|
||||
IsDir bool `json:"isdir,omitempty"`
|
||||
MimeType string `json:"mimetype,omitempty"`
|
||||
}
|
||||
|
||||
type FullFile struct {
|
||||
Info *FileInfo `json:"info"`
|
||||
Data64 string `json:"data64,omitempty"` // base64 encoded
|
||||
}
|
||||
|
||||
func (fs *FileService) StatFile(path string) (*FileInfo, error) {
|
||||
cleanedPath := filepath.Clean(wavebase.ExpandHomeDir(path))
|
||||
finfo, err := os.Stat(cleanedPath)
|
||||
if os.IsNotExist(err) {
|
||||
return &FileInfo{Path: wavebase.ReplaceHomeDir(path), NotFound: true}, nil
|
||||
}
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot stat file %q: %w", path, err)
|
||||
}
|
||||
mimeType := utilfn.DetectMimeType(path)
|
||||
return &FileInfo{
|
||||
Path: wavebase.ReplaceHomeDir(path),
|
||||
Size: finfo.Size(),
|
||||
Mode: finfo.Mode(),
|
||||
ModTime: finfo.ModTime().UnixMilli(),
|
||||
IsDir: finfo.IsDir(),
|
||||
MimeType: mimeType,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (fs *FileService) ReadFile(path string) (*FullFile, error) {
|
||||
finfo, err := fs.StatFile(path)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot stat file %q: %w", path, err)
|
||||
}
|
||||
if finfo.NotFound {
|
||||
return &FullFile{Info: finfo}, nil
|
||||
}
|
||||
cleanedPath := filepath.Clean(wavebase.ExpandHomeDir(path))
|
||||
barr, err := os.ReadFile(cleanedPath)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("cannot read file %q: %w", path, err)
|
||||
return nil, fmt.Errorf("cannot read file %q: %w", path, err)
|
||||
}
|
||||
time.Sleep(2 * time.Second)
|
||||
return base64.StdEncoding.EncodeToString(barr), nil
|
||||
return &FullFile{
|
||||
Info: finfo,
|
||||
Data64: base64.StdEncoding.EncodeToString(barr),
|
||||
}, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user