mirror of
https://github.com/hacdias/webdav.git
synced 2026-09-22 03:20:41 +08:00
feat: add multi directories support (#335)
This commit is contained in:
+48
-7
@@ -9,27 +9,68 @@ import (
|
||||
|
||||
var _ webdav.LockSystem = &lockSystem{}
|
||||
|
||||
// LockSystem wraps a [webdav.LockSystem] with a root directory, allowing
|
||||
// to reuse the same [webdav.LockSystem] for multiple users with different base
|
||||
// directories, meaning we can correctly lock the files across different users.
|
||||
// lockSystem wraps a [webdav.LockSystem], mapping virtual request names to the
|
||||
// real backing paths via resolve. This allows reusing the same
|
||||
// [webdav.LockSystem] for multiple users with different base directories,
|
||||
// meaning we can correctly lock the files across different users.
|
||||
type lockSystem struct {
|
||||
webdav.LockSystem
|
||||
directory string
|
||||
resolve func(name string) (string, error)
|
||||
}
|
||||
|
||||
// newLockSystem returns a lockSystem for a single-directory user, resolving
|
||||
// names relative to directory.
|
||||
func newLockSystem(ls webdav.LockSystem, directory string) *lockSystem {
|
||||
return &lockSystem{
|
||||
LockSystem: ls,
|
||||
resolve: func(name string) (string, error) {
|
||||
return filepath.Join(directory, name), nil
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// newMultiDirLockSystem returns a lockSystem for a multi-directory user,
|
||||
// resolving names against the real backing path of each mount.
|
||||
func newMultiDirLockSystem(ls webdav.LockSystem, mounts DirectoryMounts) *lockSystem {
|
||||
return &lockSystem{
|
||||
LockSystem: ls,
|
||||
resolve: func(name string) (string, error) {
|
||||
if cleanName(name) == "/" {
|
||||
return "/", nil
|
||||
}
|
||||
|
||||
mount, rest, err := multiDir{mounts: mounts}.resolve(name)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return mount.filePath(rest), nil
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (l *lockSystem) Confirm(now time.Time, name0, name1 string, conditions ...webdav.Condition) (release func(), err error) {
|
||||
if name0 != "" {
|
||||
name0 = filepath.Join(l.directory, name0)
|
||||
name0, err = l.resolve(name0)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
if name1 != "" {
|
||||
name1 = filepath.Join(l.directory, name1)
|
||||
name1, err = l.resolve(name1)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
return l.LockSystem.Confirm(now, name0, name1, conditions...)
|
||||
}
|
||||
|
||||
func (l *lockSystem) Create(now time.Time, details webdav.LockDetails) (token string, err error) {
|
||||
details.Root = filepath.Join(l.directory, details.Root)
|
||||
details.Root, err = l.resolve(details.Root)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return l.LockSystem.Create(now, details)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user