added custom filter options
This commit is contained in:
parent
f6e1813d15
commit
a4c746c7e9
4 changed files with 70 additions and 3 deletions
|
|
@ -16,12 +16,13 @@ const writeFlag = os.O_RDWR | os.O_CREATE | os.O_TRUNC
|
||||||
|
|
||||||
// WriteOnlyOnceFileSystem is a file write only wrapper for a webdav.FileSystem
|
// WriteOnlyOnceFileSystem is a file write only wrapper for a webdav.FileSystem
|
||||||
type WriteOnlyOnceFileSystem struct {
|
type WriteOnlyOnceFileSystem struct {
|
||||||
fs webdav.FileSystem
|
Filters []Filter
|
||||||
|
fs webdav.FileSystem
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewWriteOnlyOnceFileSystem returns a new WriteOnlyOnceFileSystem wrapping a webdav.FileSystem
|
// NewWriteOnlyOnceFileSystem returns a new WriteOnlyOnceFileSystem wrapping a webdav.FileSystem
|
||||||
func NewWriteOnlyOnceFileSystem(fs webdav.FileSystem) *WriteOnlyOnceFileSystem {
|
func NewWriteOnlyOnceFileSystem(fs webdav.FileSystem) *WriteOnlyOnceFileSystem {
|
||||||
return &WriteOnlyOnceFileSystem{fs}
|
return &WriteOnlyOnceFileSystem{fs: fs}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Mkdir is not permitted
|
// Mkdir is not permitted
|
||||||
|
|
@ -38,6 +39,12 @@ func (fs *WriteOnlyOnceFileSystem) OpenFile(ctx context.Context, name string, fl
|
||||||
return nil, ErrWonly
|
return nil, ErrWonly
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
for _, filter := range fs.Filters {
|
||||||
|
err = filter.Filter(name)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
f, err := fs.fs.OpenFile(ctx, name, flag, perm)
|
f, err := fs.fs.OpenFile(ctx, name, flag, perm)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|
@ -45,8 +52,15 @@ func (fs *WriteOnlyOnceFileSystem) OpenFile(ctx context.Context, name string, fl
|
||||||
return &File{f}, nil
|
return &File{f}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// RemoveAll is not permitted
|
// RemoveAll only permits deleting empty files
|
||||||
func (fs *WriteOnlyOnceFileSystem) RemoveAll(ctx context.Context, name string) error {
|
func (fs *WriteOnlyOnceFileSystem) RemoveAll(ctx context.Context, name string) error {
|
||||||
|
fi, err := fs.fs.Stat(ctx, name)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if fi.Mode().IsRegular() && fi.Size() == 0 {
|
||||||
|
return fs.fs.RemoveAll(ctx, name)
|
||||||
|
}
|
||||||
return ErrWonly
|
return ErrWonly
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
44
filter.go
Normal file
44
filter.go
Normal file
|
|
@ -0,0 +1,44 @@
|
||||||
|
// Copyright (C) 2019 Marius Schellenberger
|
||||||
|
|
||||||
|
package authdav
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Matcher func(path string) bool
|
||||||
|
|
||||||
|
type Filter interface {
|
||||||
|
Filter(path string) error
|
||||||
|
}
|
||||||
|
|
||||||
|
var ErrMacOSFilter = errors.New("authdav: MacOS filter: access denied")
|
||||||
|
|
||||||
|
type MacOSFilter struct {
|
||||||
|
match []Matcher
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewMacOSFilter() *MacOSFilter {
|
||||||
|
return &MacOSFilter{
|
||||||
|
match: []Matcher{
|
||||||
|
func(p string) {
|
||||||
|
return p == ".DS_Store"
|
||||||
|
},
|
||||||
|
func(p string) {
|
||||||
|
return strings.HasPrefix(p, "._")
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *MacOSFilter) Filter(path string) error {
|
||||||
|
_, f := filepath.Split(path)
|
||||||
|
for _, m := range f.match {
|
||||||
|
if m(f) {
|
||||||
|
return ErrMacOSFilter
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
4
go.mod
4
go.mod
|
|
@ -1 +1,5 @@
|
||||||
module git.giftfish.de/ston1th/authdav
|
module git.giftfish.de/ston1th/authdav
|
||||||
|
|
||||||
|
go 1.12
|
||||||
|
|
||||||
|
require golang.org/x/net v0.0.0-20190827160401-ba9fcec4b297
|
||||||
|
|
|
||||||
5
go.sum
Normal file
5
go.sum
Normal file
|
|
@ -0,0 +1,5 @@
|
||||||
|
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||||
|
golang.org/x/net v0.0.0-20190827160401-ba9fcec4b297 h1:k7pJ2yAPLPgbskkFdhRCsA77k2fySZ1zf2zCjvQCiIM=
|
||||||
|
golang.org/x/net v0.0.0-20190827160401-ba9fcec4b297/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||||
|
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||||
|
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||||
Loading…
Add table
Add a link
Reference in a new issue