mirror of
https://github.com/halejohn/Cloudreve.git
synced 2026-01-27 18:11:57 +08:00
Feat: finish WebDAV adaptation
This commit is contained in:
@@ -8,12 +8,9 @@ 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
|
||||
@@ -25,158 +22,45 @@ func slashClean(name string) string {
|
||||
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
|
||||
}
|
||||
func moveFiles(ctx context.Context, fs *filesystem.FileSystem, src FileInfo, dst string, overwrite bool) (status int, err error) {
|
||||
|
||||
var (
|
||||
fileIDs []uint
|
||||
folderIDs []uint
|
||||
)
|
||||
if src.IsDir() {
|
||||
fileIDs = []uint{src.(*model.Folder).ID}
|
||||
} else {
|
||||
return http.StatusPreconditionFailed, os.ErrExist
|
||||
folderIDs = []uint{src.(*model.File).ID}
|
||||
}
|
||||
if err := fs.Rename(ctx, src, dst); err != nil {
|
||||
return http.StatusForbidden, err
|
||||
|
||||
// 判断是否为重命名
|
||||
if src.GetPosition() == path.Dir(dst) {
|
||||
err = fs.Rename(
|
||||
ctx,
|
||||
fileIDs,
|
||||
folderIDs,
|
||||
path.Base(dst),
|
||||
)
|
||||
} else {
|
||||
err = fs.Move(
|
||||
ctx,
|
||||
fileIDs,
|
||||
folderIDs,
|
||||
src.GetPosition(),
|
||||
path.Dir(dst),
|
||||
)
|
||||
}
|
||||
if created {
|
||||
return http.StatusCreated, nil
|
||||
|
||||
if err != nil {
|
||||
return http.StatusInternalServerError, err
|
||||
}
|
||||
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.
|
||||
|
||||
Reference in New Issue
Block a user