259 lines
7.2 KiB
Go
259 lines
7.2 KiB
Go
// Copyright 2014 The Go Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package webdav
|
|
|
|
import (
|
|
"context"
|
|
model "github.com/HFO4/cloudreve/models"
|
|
"github.com/HFO4/cloudreve/pkg/filesystem"
|
|
"io"
|
|
"net/http"
|
|
"os"
|
|
"path"
|
|
"path/filepath"
|
|
"strings"
|
|
)
|
|
|
|
// slashClean is equivalent to but slightly more efficient than
|
|
// path.Clean("/" + name).
|
|
func slashClean(name string) string {
|
|
if name == "" || name[0] != '/' {
|
|
name = "/" + name
|
|
}
|
|
return path.Clean(name)
|
|
}
|
|
|
|
// A FileSystem implements access to a collection of named files. The elements
|
|
// in a file path are separated by slash ('/', U+002F) characters, regardless
|
|
// of host operating system convention.
|
|
//
|
|
// Each method has the same semantics as the os package's function of the same
|
|
// name.
|
|
//
|
|
// Note that the os.Rename documentation says that "OS-specific restrictions
|
|
// might apply". In particular, whether or not renaming a file or directory
|
|
// overwriting another existing file or directory is an error is OS-dependent.
|
|
type FileSystem interface {
|
|
Mkdir(ctx context.Context, name string, perm os.FileMode) error
|
|
OpenFile(ctx context.Context, name string, flag int, perm os.FileMode) (File, error)
|
|
RemoveAll(ctx context.Context, name string) error
|
|
Rename(ctx context.Context, oldName, newName string) error
|
|
Stat(ctx context.Context, name string) (os.FileInfo, error)
|
|
}
|
|
|
|
// A File is returned by a FileSystem's OpenFile method and can be served by a
|
|
// Handler.
|
|
//
|
|
// A File may optionally implement the DeadPropsHolder interface, if it can
|
|
// load and save dead properties.
|
|
type File interface {
|
|
http.File
|
|
io.Writer
|
|
}
|
|
|
|
// A AdapterFS implements FileSystem using the native file system restricted to a
|
|
// specific directory tree.
|
|
//
|
|
// While the FileSystem.OpenFile method takes '/'-separated paths, a AdapterFS's
|
|
// string value is a filename on the native file system, not a URL, so it is
|
|
// separated by filepath.Separator, which isn't necessarily '/'.
|
|
//
|
|
// An empty AdapterFS is treated as ".".
|
|
type AdapterFS string
|
|
|
|
func (d AdapterFS) resolve(name string) string {
|
|
// This implementation is based on AdapterFS.Open's code in the standard net/http package.
|
|
if filepath.Separator != '/' && strings.IndexRune(name, filepath.Separator) >= 0 ||
|
|
strings.Contains(name, "\x00") {
|
|
return ""
|
|
}
|
|
dir := string(d)
|
|
if dir == "" {
|
|
dir = "."
|
|
}
|
|
return filepath.Join(dir, filepath.FromSlash(slashClean(name)))
|
|
}
|
|
|
|
func (d AdapterFS) Mkdir(ctx context.Context, name string, perm os.FileMode) error {
|
|
if name = d.resolve(name); name == "" {
|
|
return os.ErrNotExist
|
|
}
|
|
return os.Mkdir(name, perm)
|
|
}
|
|
|
|
func (d AdapterFS) OpenFile(ctx context.Context, name string, flag int, perm os.FileMode) (File, error) {
|
|
if name = d.resolve(name); name == "" {
|
|
return nil, os.ErrNotExist
|
|
}
|
|
f, err := os.OpenFile(name, flag, perm)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return f, nil
|
|
}
|
|
|
|
func (d AdapterFS) RemoveAll(ctx context.Context, name string) error {
|
|
if name = d.resolve(name); name == "" {
|
|
return os.ErrNotExist
|
|
}
|
|
if name == filepath.Clean(string(d)) {
|
|
// Prohibit removing the virtual root directory.
|
|
return os.ErrInvalid
|
|
}
|
|
return os.RemoveAll(name)
|
|
}
|
|
|
|
func (d AdapterFS) Rename(ctx context.Context, oldName, newName string) error {
|
|
if oldName = d.resolve(oldName); oldName == "" {
|
|
return os.ErrNotExist
|
|
}
|
|
if newName = d.resolve(newName); newName == "" {
|
|
return os.ErrNotExist
|
|
}
|
|
if root := filepath.Clean(string(d)); root == oldName || root == newName {
|
|
// Prohibit renaming from or to the virtual root directory.
|
|
return os.ErrInvalid
|
|
}
|
|
return os.Rename(oldName, newName)
|
|
}
|
|
|
|
func (d AdapterFS) Stat(ctx context.Context, name string) (os.FileInfo, error) {
|
|
if name = d.resolve(name); name == "" {
|
|
return nil, os.ErrNotExist
|
|
}
|
|
return os.Stat(name)
|
|
}
|
|
|
|
// moveFiles moves files and/or directories from src to dst.
|
|
//
|
|
// See section 9.9.4 for when various HTTP status codes apply.
|
|
func moveFiles(ctx context.Context, fs FileSystem, src, dst string, overwrite bool) (status int, err error) {
|
|
created := false
|
|
if _, err := fs.Stat(ctx, dst); err != nil {
|
|
if !os.IsNotExist(err) {
|
|
return http.StatusForbidden, err
|
|
}
|
|
created = true
|
|
} else if overwrite {
|
|
// Section 9.9.3 says that "If a resource exists at the destination
|
|
// and the Overwrite header is "T", then prior to performing the move,
|
|
// the server must perform a DELETE with "Depth: infinity" on the
|
|
// destination resource.
|
|
if err := fs.RemoveAll(ctx, dst); err != nil {
|
|
return http.StatusForbidden, err
|
|
}
|
|
} else {
|
|
return http.StatusPreconditionFailed, os.ErrExist
|
|
}
|
|
if err := fs.Rename(ctx, src, dst); err != nil {
|
|
return http.StatusForbidden, err
|
|
}
|
|
if created {
|
|
return http.StatusCreated, nil
|
|
}
|
|
return http.StatusNoContent, nil
|
|
}
|
|
|
|
func copyProps(dst, src File) error {
|
|
d, ok := dst.(DeadPropsHolder)
|
|
if !ok {
|
|
return nil
|
|
}
|
|
s, ok := src.(DeadPropsHolder)
|
|
if !ok {
|
|
return nil
|
|
}
|
|
m, err := s.DeadProps()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
props := make([]Property, 0, len(m))
|
|
for _, prop := range m {
|
|
props = append(props, prop)
|
|
}
|
|
_, err = d.Patch([]Proppatch{{Props: props}})
|
|
return err
|
|
}
|
|
|
|
// copyFiles copies files and/or directories from src to dst.
|
|
//
|
|
// See section 9.8.5 for when various HTTP status codes apply.
|
|
func copyFiles(ctx context.Context, fs *filesystem.FileSystem, src FileInfo, dst string, overwrite bool, depth int, recursion int) (status int, err error) {
|
|
if recursion == 1000 {
|
|
return http.StatusInternalServerError, errRecursionTooDeep
|
|
}
|
|
recursion++
|
|
|
|
if src.IsDir() {
|
|
err := fs.Copy(
|
|
ctx,
|
|
[]uint{src.(*model.Folder).ID},
|
|
[]uint{}, src.(*model.Folder).Position,
|
|
path.Dir(dst),
|
|
)
|
|
if err != nil {
|
|
return http.StatusInternalServerError, err
|
|
}
|
|
} else {
|
|
err := fs.Copy(ctx, []uint{src.(*model.File).ID}, []uint{}, src.(*model.File).Position, dst)
|
|
if err != nil {
|
|
return http.StatusInternalServerError, err
|
|
}
|
|
}
|
|
|
|
return http.StatusNoContent, nil
|
|
}
|
|
|
|
// walkFS traverses filesystem fs starting at name up to depth levels.
|
|
//
|
|
// Allowed values for depth are 0, 1 or infiniteDepth. For each visited node,
|
|
// walkFS calls walkFn. If a visited file system node is a directory and
|
|
// walkFn returns filepath.SkipDir, walkFS will skip traversal of this node.
|
|
func walkFS(
|
|
ctx context.Context,
|
|
fs *filesystem.FileSystem,
|
|
depth int,
|
|
name string,
|
|
info FileInfo,
|
|
walkFn func(reqPath string, info FileInfo, err error) error) error {
|
|
// This implementation is based on Walk's code in the standard path/filepath package.
|
|
err := walkFn(name, info, nil)
|
|
if err != nil {
|
|
if info.IsDir() && err == filepath.SkipDir {
|
|
return nil
|
|
}
|
|
return err
|
|
}
|
|
if !info.IsDir() || depth == 0 {
|
|
return nil
|
|
}
|
|
if depth == 1 {
|
|
depth = 0
|
|
}
|
|
|
|
dirs, _ := info.(*model.Folder).GetChildFolder()
|
|
files, _ := info.(*model.Folder).GetChildFiles()
|
|
|
|
for _, fileInfo := range files {
|
|
filename := path.Join(name, fileInfo.Name)
|
|
err = walkFS(ctx, fs, depth, filename, &fileInfo, walkFn)
|
|
if err != nil {
|
|
if !fileInfo.IsDir() || err != filepath.SkipDir {
|
|
return err
|
|
}
|
|
}
|
|
}
|
|
|
|
for _, fileInfo := range dirs {
|
|
filename := path.Join(name, fileInfo.Name)
|
|
err = walkFS(ctx, fs, depth, filename, &fileInfo, walkFn)
|
|
if err != nil {
|
|
if !fileInfo.IsDir() || err != filepath.SkipDir {
|
|
return err
|
|
}
|
|
}
|
|
}
|
|
return nil
|
|
}
|