72 lines
1.5 KiB
Go
72 lines
1.5 KiB
Go
package fs
|
|
|
|
import (
|
|
"errors"
|
|
"git.giftfish.de/ston1th/docstore/pkg/core"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
)
|
|
|
|
func (fs *Filesystem) GetFile(path string) (f *os.File, fi os.FileInfo, err error) {
|
|
path = strings.TrimPrefix(filepath.Clean(path), core.RawPrefix)
|
|
p := filepath.Join(fs.Base, path)
|
|
f, err = os.Open(p)
|
|
if err != nil {
|
|
return
|
|
}
|
|
fi, err = f.Stat()
|
|
if err != nil {
|
|
return
|
|
}
|
|
if !fi.Mode().IsRegular() {
|
|
err = os.ErrNotExist
|
|
}
|
|
return
|
|
}
|
|
|
|
func (fs *Filesystem) Mode(path string) (mode FileMode, err error) {
|
|
path = filepath.Clean(path)
|
|
p := filepath.Join(fs.Base, path)
|
|
fi, err := os.Stat(p)
|
|
if err != nil {
|
|
return
|
|
}
|
|
m := fi.Mode()
|
|
if m.IsDir() {
|
|
mode = IsDir
|
|
} else if m.IsRegular() {
|
|
mode = IsFile
|
|
}
|
|
return
|
|
}
|
|
|
|
func (fs *Filesystem) Move(path, dest, name string) (string, error) {
|
|
path = filepath.Clean(path)
|
|
if name == "" {
|
|
_, name = filepath.Split(path)
|
|
}
|
|
newfile := filepath.Clean(filepath.Join(dest, name))
|
|
return newfile, os.Rename(filepath.Join(fs.Base, path), filepath.Join(fs.Base, newfile))
|
|
}
|
|
|
|
func (fs *Filesystem) Delete(path string) (removed []string, err error) {
|
|
path = filepath.Clean(path)
|
|
fullpath := filepath.Join(fs.Base, path)
|
|
if fullpath == fs.Base {
|
|
return nil, errors.New("the root path can not be deleted")
|
|
}
|
|
fi, err := os.Stat(fullpath)
|
|
if err != nil {
|
|
return
|
|
}
|
|
m := fi.Mode()
|
|
if m.IsDir() {
|
|
err = os.RemoveAll(fullpath)
|
|
removed = fs.RecursiveFiles(path)
|
|
} else if m.IsRegular() {
|
|
err = os.Remove(fullpath)
|
|
removed = []string{path}
|
|
}
|
|
return
|
|
}
|