mirror of
https://github.com/hanzoai/ai.git
synced 2026-11-20 14:13:23 +00:00
Casibase's upstream '// Copyright <yr> The OpenAgent Authors. All Rights Reserved.' (and legacy 'The Casibase Authors' on older files) was stripped from every derived source file and replaced with a Hanzo-only line. This re-adds it as a '// Portions Copyright ...' line beneath the Hanzo line on the 573 casibase-derived files, using each file's real upstream year/holder. Net-new Hanzo files are untouched. Nothing deleted. LICENSE + NOTICE were already correct.
97 lines
2.6 KiB
Go
97 lines
2.6 KiB
Go
// Copyright 2023-2025 Hanzo AI Inc. All Rights Reserved.
|
|
// Portions Copyright 2023 The OpenAgent Authors. All Rights Reserved.
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
package storage
|
|
|
|
import (
|
|
"bytes"
|
|
"io"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/hanzoai/ai/util"
|
|
)
|
|
|
|
type LocalFileSystemStorageProvider struct {
|
|
path string
|
|
}
|
|
|
|
func NewLocalFileSystemStorageProvider(path string) (*LocalFileSystemStorageProvider, error) {
|
|
path = strings.ReplaceAll(path, "\\", "/")
|
|
return &LocalFileSystemStorageProvider{path: path}, nil
|
|
}
|
|
|
|
func (p *LocalFileSystemStorageProvider) ListObjects(prefix string) ([]*Object, error) {
|
|
objects := []*Object{}
|
|
fullPath := filepath.Join(p.path, prefix)
|
|
fullPath = strings.ReplaceAll(fullPath, "\\", "/")
|
|
util.EnsureFolderExists(fullPath)
|
|
|
|
filepath.Walk(fullPath, func(path string, info os.FileInfo, err error) error {
|
|
if path == fullPath {
|
|
return nil
|
|
}
|
|
|
|
base := filepath.Base(path)
|
|
if info.IsDir() && (strings.HasPrefix(base, ".") || base == "node_modules") {
|
|
return filepath.SkipDir
|
|
}
|
|
|
|
if err == nil && !info.IsDir() {
|
|
modTime := info.ModTime()
|
|
path = strings.ReplaceAll(path, "\\", "/")
|
|
relativePath := strings.TrimPrefix(path, p.path)
|
|
relativePath = strings.TrimPrefix(relativePath, "/")
|
|
|
|
objects = append(objects, &Object{
|
|
Key: relativePath,
|
|
LastModified: modTime.Format(time.RFC3339),
|
|
Size: info.Size(),
|
|
Url: path,
|
|
})
|
|
}
|
|
return nil
|
|
})
|
|
|
|
return objects, nil
|
|
}
|
|
|
|
func (p *LocalFileSystemStorageProvider) PutObject(user string, parent string, key string, fileBuffer *bytes.Buffer) (string, error) {
|
|
fullPath := filepath.Join(p.path, key)
|
|
err := os.MkdirAll(filepath.Dir(fullPath), os.ModePerm)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
dst, err := os.Create(filepath.Clean(fullPath))
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
defer dst.Close()
|
|
|
|
_, err = io.Copy(dst, fileBuffer)
|
|
return fullPath, err
|
|
}
|
|
|
|
func (p *LocalFileSystemStorageProvider) DeleteObject(key string) error {
|
|
fullPath := filepath.Join(p.path, key)
|
|
if strings.HasSuffix(key, "_hidden.ini") {
|
|
fullPath = filepath.Dir(fullPath)
|
|
}
|
|
return os.RemoveAll(fullPath)
|
|
}
|