feat: ✨ initialize project structure with frontend dependencies and backend protocols
This commit is contained in:
@@ -0,0 +1,97 @@
|
||||
package local
|
||||
|
||||
import (
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"time"
|
||||
|
||||
"goexplore/internal/explorer"
|
||||
)
|
||||
|
||||
type LocalExplorer struct {
|
||||
basePath string
|
||||
}
|
||||
|
||||
func New() *LocalExplorer {
|
||||
basePath := "/"
|
||||
if runtime.GOOS == "windows" {
|
||||
basePath = "C:\\"
|
||||
}
|
||||
return &LocalExplorer{basePath: basePath}
|
||||
}
|
||||
|
||||
func (e *LocalExplorer) Connect() error { return nil }
|
||||
func (e *LocalExplorer) Disconnect() error { return nil }
|
||||
|
||||
func (e *LocalExplorer) ListDir(path string) ([]explorer.FileEntry, error) {
|
||||
if path == "" {
|
||||
path = e.basePath
|
||||
}
|
||||
entries, err := os.ReadDir(path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var result []explorer.FileEntry
|
||||
for _, entry := range entries {
|
||||
info, err := entry.Info()
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
result = append(result, explorer.FileEntry{
|
||||
Name: entry.Name(),
|
||||
Path: filepath.ToSlash(filepath.Join(path, entry.Name())),
|
||||
Size: info.Size(),
|
||||
Modified: info.ModTime().Format(time.RFC3339),
|
||||
IsDir: entry.IsDir(),
|
||||
Permissions: info.Mode().String(),
|
||||
})
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (e *LocalExplorer) Stat(path string) (explorer.FileEntry, error) {
|
||||
if path == "" {
|
||||
path = e.basePath
|
||||
}
|
||||
info, err := os.Stat(path)
|
||||
if err != nil {
|
||||
return explorer.FileEntry{}, err
|
||||
}
|
||||
return explorer.FileEntry{
|
||||
Name: info.Name(),
|
||||
Path: filepath.ToSlash(path),
|
||||
Size: info.Size(),
|
||||
Modified: info.ModTime().Format(time.RFC3339),
|
||||
IsDir: info.IsDir(),
|
||||
Permissions: info.Mode().String(),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (e *LocalExplorer) MkDir(path string) error {
|
||||
return os.Mkdir(path, 0755)
|
||||
}
|
||||
|
||||
func (e *LocalExplorer) Delete(path string) error {
|
||||
return os.RemoveAll(path)
|
||||
}
|
||||
|
||||
func (e *LocalExplorer) Rename(src, dst string) error {
|
||||
return os.Rename(src, dst)
|
||||
}
|
||||
|
||||
func (e *LocalExplorer) ReadFile(path string) (io.ReadCloser, error) {
|
||||
return os.Open(path)
|
||||
}
|
||||
|
||||
func (e *LocalExplorer) WriteFile(path string, r io.Reader, size int64) error {
|
||||
f, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer f.Close()
|
||||
_, err = io.Copy(f, r)
|
||||
return err
|
||||
}
|
||||
Reference in New Issue
Block a user