✨ Implement WebDAV server

This commit is contained in:
2020-10-18 11:17:29 +02:00
parent 356ddbc167
commit 47a708de0e
4 changed files with 111 additions and 78 deletions
+3 -10
View File
@@ -44,8 +44,7 @@ import (
)
type DirectoryMapping struct {
VirtualName string
RealName string
DataDirName string
}
// slashClean is equivalent to but slightly more efficient than
@@ -61,23 +60,17 @@ type BaseDir string
func (d BaseDir) resolve(ctx context.Context, name string) string {
directoryMapping := ctx.Value("mapping").(*DirectoryMapping)
prefix := directoryMapping.VirtualName + "/"
if !strings.HasPrefix(name, prefix) {
// something does not add up
return ""
}
nameWithoutPrefix := strings.TrimPrefix(name, prefix)
// This implementation is based on Dir.Open's code in the standard net/http package.
if filepath.Separator != '/' && strings.IndexRune(name, filepath.Separator) >= 0 ||
strings.Contains(nameWithoutPrefix, "\x00") {
strings.Contains(name, "\x00") {
return ""
}
dir := string(d)
if dir == "" {
dir = "."
}
return filepath.Join(dir, directoryMapping.RealName, filepath.FromSlash(slashClean(nameWithoutPrefix)))
return filepath.Join(dir, directoryMapping.DataDirName, filepath.FromSlash(slashClean(name)))
}
func (d BaseDir) Mkdir(ctx context.Context, name string, perm os.FileMode) error {