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 ( import (
"errors" "errors"
"git.giftfish.de/ston1th/docstore/pkg/core"
"io" "io"
"os" "os"
"path/filepath" "path/filepath"
"strings"
) )
type Mode int type Mode int
@ -18,8 +16,7 @@ const (
) )
func (fs *Filesystem) GetFile(path string) (f *os.File, fi os.FileInfo, err error) { 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, Clean(path))
p := filepath.Join(fs.Base, path)
f, err = os.Open(p) f, err = os.Open(p)
if err != nil { if err != nil {
return 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) { func (fs *Filesystem) Mode(path string) (mode Mode, err error) {
path = Clean(path) p := filepath.Join(fs.Base, Clean(path))
p := filepath.Join(fs.Base, path)
fi, err := os.Stat(p) fi, err := os.Stat(p)
if err != nil { if err != nil {
return 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) { func (fs *Filesystem) NewFile(r io.Reader, path, orig, name string) (newfile string, err error) {
path = Clean(path) path = Clean(path)
orig = Clean(orig) orig = File(orig)
_, orig = filepath.Split(orig) name = File(name)
name = Clean(name)
_, name = filepath.Split(name)
if name == "" { if name == "" {
name = orig 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 { func (fs *Filesystem) Mkdir(path, name string) error {
path = Clean(path) return os.Mkdir(filepath.Join(fs.Base, Clean(path), Clean(name)), DirMode)
name = Clean(name)
return os.Mkdir(filepath.Join(fs.Base, path, name), DirMode)
} }
func (fs *Filesystem) Move(path, dest, name string) (string, error) { func (fs *Filesystem) Move(path, dest, name string) (string, error) {
path = Clean(path)
if name == "" { 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 { if fullpath == fs.Base {
return "", errors.New("the root path can not be moved") 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() m := fi.Mode()
if m.IsDir() { if m.IsDir() {
err = os.RemoveAll(fullpath)
removed = fs.RecursiveFiles(path) removed = fs.RecursiveFiles(path)
err = os.RemoveAll(fullpath)
} else if m.IsRegular() { } else if m.IsRegular() {
err = os.Remove(fullpath)
removed = []string{path} removed = []string{path}
err = os.Remove(fullpath)
} }
return return
} }

View file

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

View file

@ -7,3 +7,8 @@ const sep = "/"
func Clean(path string) string { func Clean(path string) string {
return filepath.FromSlash(filepath.Clean(sep + path)) 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() 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 { if err != nil {
ctx.Status(http.StatusNotFound) ctx.Status(http.StatusNotFound)
err = ctx.Write([]byte(noSuchFile)) err = ctx.Write([]byte(noSuchFile))
@ -356,6 +357,7 @@ func deleteHandler(ctx *Context) {
if err != nil { if err != nil {
log.Printf("db: delete: %s %s", f, err) log.Printf("db: delete: %s %s", f, err)
} }
log.Debugf("delete %s id: %s from index", f, id)
if id == "" { if id == "" {
continue continue
} }

View file

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