fixed some path issues

This commit is contained in:
ston1th 2019-05-02 14:50:28 +02:00
commit 38c2a72fe0
5 changed files with 22 additions and 28 deletions

View file

@ -2,11 +2,9 @@ package fs
import (
"errors"
"git.giftfish.de/ston1th/docstore/pkg/core"
"io"
"os"
"path/filepath"
"strings"
)
type Mode int
@ -18,8 +16,7 @@ const (
)
func (fs *Filesystem) GetFile(path string) (f *os.File, fi os.FileInfo, err error) {
path = strings.TrimPrefix(Clean(path), core.RawPrefix)
p := filepath.Join(fs.Base, path)
p := filepath.Join(fs.Base, Clean(path))
f, err = os.Open(p)
if err != nil {
return
@ -35,8 +32,7 @@ func (fs *Filesystem) GetFile(path string) (f *os.File, fi os.FileInfo, err erro
}
func (fs *Filesystem) Mode(path string) (mode Mode, err error) {
path = Clean(path)
p := filepath.Join(fs.Base, path)
p := filepath.Join(fs.Base, Clean(path))
fi, err := os.Stat(p)
if err != nil {
return
@ -52,10 +48,8 @@ func (fs *Filesystem) Mode(path string) (mode Mode, err error) {
func (fs *Filesystem) NewFile(r io.Reader, path, orig, name string) (newfile string, err error) {
path = Clean(path)
orig = Clean(orig)
_, orig = filepath.Split(orig)
name = Clean(name)
_, name = filepath.Split(name)
orig = File(orig)
name = File(name)
if name == "" {
name = orig
}
@ -70,17 +64,14 @@ func (fs *Filesystem) NewFile(r io.Reader, path, orig, name string) (newfile str
}
func (fs *Filesystem) Mkdir(path, name string) error {
path = Clean(path)
name = Clean(name)
return os.Mkdir(filepath.Join(fs.Base, path, name), DirMode)
return os.Mkdir(filepath.Join(fs.Base, Clean(path), Clean(name)), DirMode)
}
func (fs *Filesystem) Move(path, dest, name string) (string, error) {
path = Clean(path)
if name == "" {
_, name = filepath.Split(path)
name = File(path)
}
fullpath := filepath.Join(fs.Base, path)
fullpath := filepath.Join(fs.Base, Clean(path))
if fullpath == fs.Base {
return "", errors.New("the root path can not be moved")
}
@ -100,11 +91,11 @@ func (fs *Filesystem) Delete(path string) (removed []string, err error) {
}
m := fi.Mode()
if m.IsDir() {
err = os.RemoveAll(fullpath)
removed = fs.RecursiveFiles(path)
err = os.RemoveAll(fullpath)
} else if m.IsRegular() {
err = os.Remove(fullpath)
removed = []string{path}
err = os.Remove(fullpath)
}
return
}

View file

@ -69,7 +69,6 @@ func (fs *Filesystem) ReadDir(path string) (d *Directory) {
func (fs *Filesystem) Recursive(p string) (paths core.Paths) {
p = Clean(p)
p, _ = filepath.Split(p)
if len(p) >= 2 {
p = p[:len(p)-1]
}
@ -91,14 +90,12 @@ func (fs *Filesystem) Recursive(p string) (paths core.Paths) {
}
func (fs *Filesystem) RecursiveFiles(p string) (files []string) {
p = Clean(p)
fullpath := filepath.Join(fs.Base, p)
filepath.Walk(fullpath, func(path string, info os.FileInfo, err error) error {
filepath.Walk(filepath.Join(fs.Base, Clean(p)), func(path string, info os.FileInfo, err error) error {
if err != nil {
return nil
}
if info.Mode().IsRegular() {
files = append(files, strings.TrimPrefix(path, fullpath))
files = append(files, strings.TrimPrefix(path, fs.Base))
}
return nil
})
@ -106,8 +103,7 @@ func (fs *Filesystem) RecursiveFiles(p string) (files []string) {
}
func Paths(path string) (p core.Paths) {
path = Clean(path)
s := strings.Split(path, string(filepath.Separator))
s := strings.Split(Clean(path), string(filepath.Separator))
for i, v := range s[1:] {
if v == "" {
continue

View file

@ -7,3 +7,8 @@ const sep = "/"
func Clean(path string) string {
return filepath.FromSlash(filepath.Clean(sep + path))
}
func File(path string) string {
_, f := filepath.Split(Clean(path))
return f
}

View file

@ -161,9 +161,10 @@ func registerHandler(ctx *Context) {
}
}
func fileHandler(ctx *Context) {
func rawHandler(ctx *Context) {
defer ctx.Log()
f, fi, err := ctx.Srv.FS.GetFile(ctx.Path())
path := strings.TrimPrefix(ctx.Path(), core.RawPrefix)
f, fi, err := ctx.Srv.FS.GetFile(path)
if err != nil {
ctx.Status(http.StatusNotFound)
err = ctx.Write([]byte(noSuchFile))
@ -356,6 +357,7 @@ func deleteHandler(ctx *Context) {
if err != nil {
log.Printf("db: delete: %s %s", f, err)
}
log.Debugf("delete %s id: %s from index", f, id)
if id == "" {
continue
}

View file

@ -40,7 +40,7 @@ var prefixRoutes = []route{
core.RawPrefix,
jwtHandler(
authHandler(
fileHandler)),
rawHandler)),
[]string{"GET"},
},
{