Init V4 community edition (#2265)
* Init V4 community edition * Init V4 community edition
This commit is contained in:
@@ -5,16 +5,7 @@
|
||||
package webdav
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
model "github.com/cloudreve/Cloudreve/v3/models"
|
||||
"github.com/cloudreve/Cloudreve/v3/pkg/filesystem"
|
||||
"github.com/cloudreve/Cloudreve/v3/pkg/filesystem/fsctx"
|
||||
)
|
||||
|
||||
// slashClean is equivalent to but slightly more efficient than
|
||||
@@ -25,182 +16,3 @@ func slashClean(name string) string {
|
||||
}
|
||||
return path.Clean(name)
|
||||
}
|
||||
|
||||
// 更新Copy或Move后的修改时间
|
||||
func updateCopyMoveModtime(req *http.Request, fs *filesystem.FileSystem, dst string) error {
|
||||
var modtime time.Time
|
||||
if timeVal := req.Header.Get("X-OC-Mtime"); timeVal != "" {
|
||||
timeUnix, err := strconv.ParseInt(timeVal, 10, 64)
|
||||
if err == nil {
|
||||
modtime = time.Unix(timeUnix, 0)
|
||||
}
|
||||
}
|
||||
|
||||
if modtime.IsZero() {
|
||||
return nil
|
||||
}
|
||||
|
||||
ok, fi := isPathExist(req.Context(), fs, dst)
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
|
||||
if fi.IsDir() {
|
||||
return model.DB.Model(fi.(*model.Folder)).UpdateColumn("updated_at", modtime).Error
|
||||
}
|
||||
return model.DB.Model(fi.(*model.File)).UpdateColumn("updated_at", modtime).Error
|
||||
}
|
||||
|
||||
// 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.FileSystem, src FileInfo, dst string, overwrite bool) (status int, err error) {
|
||||
|
||||
var (
|
||||
fileIDs []uint
|
||||
folderIDs []uint
|
||||
)
|
||||
if src.IsDir() {
|
||||
folderIDs = []uint{src.(*model.Folder).ID}
|
||||
} else {
|
||||
fileIDs = []uint{src.(*model.File).ID}
|
||||
}
|
||||
|
||||
if overwrite {
|
||||
if err := _checkOverwriteFile(ctx, fs, src, dst); err != nil {
|
||||
return http.StatusInternalServerError, err
|
||||
}
|
||||
}
|
||||
|
||||
// 判断是否需要移动
|
||||
if src.GetPosition() != path.Dir(dst) {
|
||||
err = fs.Move(
|
||||
context.WithValue(ctx, fsctx.WebdavDstName, path.Base(dst)),
|
||||
folderIDs,
|
||||
fileIDs,
|
||||
src.GetPosition(),
|
||||
path.Dir(dst),
|
||||
)
|
||||
} else if src.GetName() != path.Base(dst) {
|
||||
// 判断是否需要重命名
|
||||
err = fs.Rename(
|
||||
ctx,
|
||||
folderIDs,
|
||||
fileIDs,
|
||||
path.Base(dst),
|
||||
)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return http.StatusInternalServerError, err
|
||||
}
|
||||
return http.StatusNoContent, nil
|
||||
}
|
||||
|
||||
// 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++
|
||||
|
||||
var (
|
||||
fileIDs []uint
|
||||
folderIDs []uint
|
||||
)
|
||||
|
||||
if overwrite {
|
||||
if err := _checkOverwriteFile(ctx, fs, src, dst); err != nil {
|
||||
return http.StatusInternalServerError, err
|
||||
}
|
||||
}
|
||||
|
||||
if src.IsDir() {
|
||||
folderIDs = []uint{src.(*model.Folder).ID}
|
||||
} else {
|
||||
fileIDs = []uint{src.(*model.File).ID}
|
||||
}
|
||||
|
||||
err = fs.Copy(
|
||||
context.WithValue(ctx, fsctx.WebdavDstName, path.Base(dst)),
|
||||
folderIDs,
|
||||
fileIDs,
|
||||
src.GetPosition(),
|
||||
path.Dir(dst),
|
||||
)
|
||||
if err != nil {
|
||||
return http.StatusInternalServerError, err
|
||||
}
|
||||
|
||||
return http.StatusNoContent, nil
|
||||
}
|
||||
|
||||
// 判断目标 文件/夹 是否已经存在,存在则先删除目标文件/夹
|
||||
func _checkOverwriteFile(ctx context.Context, fs *filesystem.FileSystem, src FileInfo, dst string) error {
|
||||
if src.IsDir() {
|
||||
ok, folder := fs.IsPathExist(dst)
|
||||
if ok {
|
||||
return fs.Delete(ctx, []uint{folder.ID}, []uint{}, false, false)
|
||||
}
|
||||
} else {
|
||||
ok, file := fs.IsFileExist(dst)
|
||||
if ok {
|
||||
return fs.Delete(ctx, []uint{}, []uint{file.ID}, false, false)
|
||||
}
|
||||
}
|
||||
return 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
|
||||
}
|
||||
|
||||
@@ -7,83 +7,168 @@ package webdav
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"encoding/xml"
|
||||
"errors"
|
||||
"fmt"
|
||||
"mime"
|
||||
"github.com/cloudreve/Cloudreve/v4/application/dependency"
|
||||
"github.com/cloudreve/Cloudreve/v4/inventory"
|
||||
"github.com/cloudreve/Cloudreve/v4/inventory/types"
|
||||
"github.com/cloudreve/Cloudreve/v4/pkg/filemanager/fs"
|
||||
"github.com/cloudreve/Cloudreve/v4/pkg/filemanager/manager"
|
||||
"github.com/cloudreve/Cloudreve/v4/pkg/hashid"
|
||||
"github.com/gin-gonic/gin"
|
||||
"net/http"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
model "github.com/cloudreve/Cloudreve/v3/models"
|
||||
"github.com/cloudreve/Cloudreve/v3/pkg/filesystem"
|
||||
)
|
||||
|
||||
type FileDeadProps struct {
|
||||
*model.File
|
||||
}
|
||||
//// 实现 webdav.DeadPropsHolder 接口,不能在models.file里面定义
|
||||
//func (file *FileDeadProps) DeadProps() (map[xml.Name]Property, error) {
|
||||
// return map[xml.Name]Property{
|
||||
// xml.Name{Space: "http://owncloud.org/ns", Local: "checksums"}: {
|
||||
// XMLName: xml.Name{
|
||||
// Space: "http://owncloud.org/ns", Local: "checksums",
|
||||
// },
|
||||
// InnerXML: []byte("<checksum>" + file.MetadataSerialized[model.ChecksumMetadataKey] + "</checksum>"),
|
||||
// },
|
||||
// }, nil
|
||||
//}
|
||||
//
|
||||
//func (file *FileDeadProps) Patch(proppatches []Proppatch) ([]Propstat, error) {
|
||||
// var (
|
||||
// stat Propstat
|
||||
// err error
|
||||
// )
|
||||
// stat.Status = http.StatusOK
|
||||
// for _, patch := range proppatches {
|
||||
// for _, prop := range patch.Props {
|
||||
// stat.Props = append(stat.Props, Property{XMLName: prop.XMLName})
|
||||
// if prop.XMLName.Space == "DAV:" && prop.XMLName.Local == "lastmodified" {
|
||||
// var modtimeUnix int64
|
||||
// modtimeUnix, err = strconv.ParseInt(string(prop.InnerXML), 10, 64)
|
||||
// if err == nil {
|
||||
// err = model.DB.Model(file).UpdateColumn("updated_at", time.Unix(modtimeUnix, 0)).Error
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// return []Propstat{stat}, err
|
||||
//}
|
||||
//
|
||||
//type FolderDeadProps struct {
|
||||
// *model.Folder
|
||||
//}
|
||||
//
|
||||
//func (folder *FolderDeadProps) DeadProps() (map[xml.Name]Property, error) {
|
||||
// return nil, nil
|
||||
//}
|
||||
//
|
||||
//func (folder *FolderDeadProps) Patch(proppatches []Proppatch) ([]Propstat, error) {
|
||||
// var (
|
||||
// stat Propstat
|
||||
// err error
|
||||
// )
|
||||
// stat.Status = http.StatusOK
|
||||
// for _, patch := range proppatches {
|
||||
// for _, prop := range patch.Props {
|
||||
// stat.Props = append(stat.Props, Property{XMLName: prop.XMLName})
|
||||
// if prop.XMLName.Space == "DAV:" && prop.XMLName.Local == "lastmodified" {
|
||||
// var modtimeUnix int64
|
||||
// modtimeUnix, err = strconv.ParseInt(string(prop.InnerXML), 10, 64)
|
||||
// if err == nil {
|
||||
// err = model.DB.Model(folder).UpdateColumn("updated_at", time.Unix(modtimeUnix, 0)).Error
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// return []Propstat{stat}, err
|
||||
//}
|
||||
|
||||
// 实现 webdav.DeadPropsHolder 接口,不能在models.file里面定义
|
||||
func (file *FileDeadProps) DeadProps() (map[xml.Name]Property, error) {
|
||||
return map[xml.Name]Property{
|
||||
xml.Name{Space: "http://owncloud.org/ns", Local: "checksums"}: {
|
||||
XMLName: xml.Name{
|
||||
Space: "http://owncloud.org/ns", Local: "checksums",
|
||||
},
|
||||
InnerXML: []byte("<checksum>" + file.MetadataSerialized[model.ChecksumMetadataKey] + "</checksum>"),
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
const (
|
||||
DeadPropsMetadataPrefix = "dav:"
|
||||
SpaceNameSeparator = "|"
|
||||
)
|
||||
|
||||
func (file *FileDeadProps) Patch(proppatches []Proppatch) ([]Propstat, error) {
|
||||
var (
|
||||
stat Propstat
|
||||
err error
|
||||
)
|
||||
stat.Status = http.StatusOK
|
||||
for _, patch := range proppatches {
|
||||
for _, prop := range patch.Props {
|
||||
stat.Props = append(stat.Props, Property{XMLName: prop.XMLName})
|
||||
if prop.XMLName.Space == "DAV:" && prop.XMLName.Local == "lastmodified" {
|
||||
var modtimeUnix int64
|
||||
modtimeUnix, err = strconv.ParseInt(string(prop.InnerXML), 10, 64)
|
||||
if err == nil {
|
||||
err = model.DB.Model(file.File).UpdateColumn("updated_at", time.Unix(modtimeUnix, 0)).Error
|
||||
}
|
||||
}
|
||||
type (
|
||||
// DeadPropsStore implements DeadPropsHolder interface with metadata based store.
|
||||
metadataDeadProps struct {
|
||||
f fs.File
|
||||
fm manager.FileManager
|
||||
}
|
||||
|
||||
DeadPropsStore struct {
|
||||
Lang string `json:"l,omitempty"`
|
||||
InnerXML []byte `json:"i,omitempty"`
|
||||
}
|
||||
)
|
||||
|
||||
func (m *metadataDeadProps) DeadProps() (map[xml.Name]Property, error) {
|
||||
meta := m.f.Metadata()
|
||||
res := make(map[xml.Name]Property)
|
||||
for k, v := range meta {
|
||||
if !strings.HasPrefix(k, DeadPropsMetadataPrefix) {
|
||||
continue
|
||||
}
|
||||
|
||||
spaceLocal := strings.SplitN(strings.TrimPrefix(k, DeadPropsMetadataPrefix), SpaceNameSeparator, 2)
|
||||
name := xml.Name{spaceLocal[0], spaceLocal[1]}
|
||||
propsStore := &DeadPropsStore{}
|
||||
if err := json.Unmarshal([]byte(v), propsStore); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
res[name] = Property{
|
||||
XMLName: name,
|
||||
InnerXML: propsStore.InnerXML,
|
||||
Lang: propsStore.Lang,
|
||||
}
|
||||
}
|
||||
return []Propstat{stat}, err
|
||||
|
||||
return res, nil
|
||||
}
|
||||
|
||||
type FolderDeadProps struct {
|
||||
*model.Folder
|
||||
}
|
||||
|
||||
func (folder *FolderDeadProps) DeadProps() (map[xml.Name]Property, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (folder *FolderDeadProps) Patch(proppatches []Proppatch) ([]Propstat, error) {
|
||||
var (
|
||||
stat Propstat
|
||||
err error
|
||||
)
|
||||
stat.Status = http.StatusOK
|
||||
func (m *metadataDeadProps) Patch(ctx context.Context, proppatches []Proppatch) ([]Propstat, error) {
|
||||
metadataArgs := make([]fs.MetadataPatch, 0, len(proppatches))
|
||||
pstat := Propstat{Status: http.StatusOK}
|
||||
for _, patch := range proppatches {
|
||||
for _, prop := range patch.Props {
|
||||
stat.Props = append(stat.Props, Property{XMLName: prop.XMLName})
|
||||
if prop.XMLName.Space == "DAV:" && prop.XMLName.Local == "lastmodified" {
|
||||
var modtimeUnix int64
|
||||
modtimeUnix, err = strconv.ParseInt(string(prop.InnerXML), 10, 64)
|
||||
if err == nil {
|
||||
err = model.DB.Model(folder.Folder).UpdateColumn("updated_at", time.Unix(modtimeUnix, 0)).Error
|
||||
}
|
||||
translateFn := func(p Property) (*fs.MetadataPatch, error) {
|
||||
val, err := json.Marshal(&DeadPropsStore{
|
||||
Lang: p.Lang,
|
||||
InnerXML: p.InnerXML,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &fs.MetadataPatch{
|
||||
Key: DeadPropsMetadataPrefix + p.XMLName.Space + SpaceNameSeparator + p.XMLName.Local,
|
||||
Value: string(val),
|
||||
}, nil
|
||||
}
|
||||
if patch.Remove {
|
||||
translateFn = func(p Property) (*fs.MetadataPatch, error) {
|
||||
return &fs.MetadataPatch{
|
||||
Key: DeadPropsMetadataPrefix + p.XMLName.Space + SpaceNameSeparator + p.XMLName.Local,
|
||||
Remove: true,
|
||||
}, nil
|
||||
}
|
||||
}
|
||||
for _, prop := range patch.Props {
|
||||
pstat.Props = append(pstat.Props, Property{XMLName: prop.XMLName})
|
||||
patch, err := translateFn(prop)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
metadataArgs = append(metadataArgs, *patch)
|
||||
}
|
||||
}
|
||||
return []Propstat{stat}, err
|
||||
|
||||
if err := m.fm.PatchMedata(ctx, []*fs.URI{m.f.Uri(false)}, metadataArgs...); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return []Propstat{pstat}, nil
|
||||
}
|
||||
|
||||
type FileInfo interface {
|
||||
@@ -172,14 +257,14 @@ type DeadPropsHolder interface {
|
||||
//
|
||||
// For more details on when various HTTP status codes apply, see
|
||||
// http://www.webdav.org/specs/rfc4918.html#PROPPATCH-status
|
||||
Patch([]Proppatch) ([]Propstat, error)
|
||||
Patch(context.Context, []Proppatch) ([]Propstat, error)
|
||||
}
|
||||
|
||||
// liveProps contains all supported, protected DAV: properties.
|
||||
var liveProps = map[xml.Name]struct {
|
||||
// findFn implements the propfind function of this property. If nil,
|
||||
// it indicates a hidden property.
|
||||
findFn func(context.Context, *filesystem.FileSystem, LockSystem, string, FileInfo) (string, error)
|
||||
findFn func(context.Context, manager.FileManager, fs.File) (string, error)
|
||||
// dir is true if the property applies to directories.
|
||||
dir bool
|
||||
}{
|
||||
@@ -207,8 +292,8 @@ var liveProps = map[xml.Name]struct {
|
||||
dir: true,
|
||||
},
|
||||
{Space: "DAV:", Local: "creationdate"}: {
|
||||
findFn: nil,
|
||||
dir: false,
|
||||
findFn: findCreationDate,
|
||||
dir: true,
|
||||
},
|
||||
{Space: "DAV:", Local: "getcontentlanguage"}: {
|
||||
findFn: nil,
|
||||
@@ -234,6 +319,14 @@ var liveProps = map[xml.Name]struct {
|
||||
findFn: findSupportedLock,
|
||||
dir: true,
|
||||
},
|
||||
{Space: "DAV:", Local: "quota-used-bytes"}: {
|
||||
findFn: findQuotaUsedBytes,
|
||||
dir: true,
|
||||
},
|
||||
{Space: "DAV:", Local: "quota-available-bytes"}: {
|
||||
findFn: findQuotaAvailableBytes,
|
||||
dir: true,
|
||||
},
|
||||
}
|
||||
|
||||
// TODO(nigeltao) merge props and allprop?
|
||||
@@ -242,19 +335,20 @@ var liveProps = map[xml.Name]struct {
|
||||
//
|
||||
// Each Propstat has a unique status and each property name will only be part
|
||||
// of one Propstat element.
|
||||
func props(ctx context.Context, fs *filesystem.FileSystem, ls LockSystem, fi FileInfo, pnames []xml.Name) ([]Propstat, error) {
|
||||
isDir := fi.IsDir()
|
||||
if !isDir {
|
||||
fi = &FileDeadProps{fi.(*model.File)}
|
||||
func props(c *gin.Context, file fs.File, fm manager.FileManager, pnames []xml.Name) ([]Propstat, error) {
|
||||
isDir := file.Type() == types.FileTypeFolder
|
||||
dph := &metadataDeadProps{
|
||||
f: file,
|
||||
fm: fm,
|
||||
}
|
||||
|
||||
var deadProps map[xml.Name]Property
|
||||
if dph, ok := fi.(DeadPropsHolder); ok {
|
||||
var err error
|
||||
deadProps, err = dph.DeadProps()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var (
|
||||
deadProps map[xml.Name]Property
|
||||
err error
|
||||
)
|
||||
deadProps, err = dph.DeadProps()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
pstatOK := Propstat{Status: http.StatusOK}
|
||||
@@ -267,8 +361,14 @@ func props(ctx context.Context, fs *filesystem.FileSystem, ls LockSystem, fi Fil
|
||||
}
|
||||
// Otherwise, it must either be a live property or we don't know it.
|
||||
if prop := liveProps[pn]; prop.findFn != nil && (prop.dir || !isDir) {
|
||||
innerXML, err := prop.findFn(ctx, fs, ls, fi.GetName(), fi)
|
||||
innerXML, err := prop.findFn(c, fm, file)
|
||||
if err != nil {
|
||||
if errors.Is(err, ErrNotImplemented) {
|
||||
pstatNotFound.Props = append(pstatNotFound.Props, Property{
|
||||
XMLName: pn,
|
||||
})
|
||||
continue
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
pstatOK.Props = append(pstatOK.Props, Property{
|
||||
@@ -285,21 +385,18 @@ func props(ctx context.Context, fs *filesystem.FileSystem, ls LockSystem, fi Fil
|
||||
}
|
||||
|
||||
// Propnames returns the property names defined for resource name.
|
||||
func propnames(ctx context.Context, fs *filesystem.FileSystem, ls LockSystem, fi FileInfo) ([]xml.Name, error) {
|
||||
isDir := fi.IsDir()
|
||||
if !isDir {
|
||||
fi = &FileDeadProps{fi.(*model.File)}
|
||||
}
|
||||
|
||||
func propnames(c *gin.Context, file fs.File, fm manager.FileManager) ([]xml.Name, error) {
|
||||
var deadProps map[xml.Name]Property
|
||||
if dph, ok := fi.(DeadPropsHolder); ok {
|
||||
var err error
|
||||
deadProps, err = dph.DeadProps()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
dph := &metadataDeadProps{
|
||||
f: file,
|
||||
fm: fm,
|
||||
}
|
||||
deadProps, err := dph.DeadProps()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
isDir := file.Type() == types.FileTypeFolder
|
||||
pnames := make([]xml.Name, 0, len(liveProps)+len(deadProps))
|
||||
for pn, prop := range liveProps {
|
||||
if prop.findFn != nil && (prop.dir || !isDir) {
|
||||
@@ -320,8 +417,8 @@ func propnames(ctx context.Context, fs *filesystem.FileSystem, ls LockSystem, fi
|
||||
// returned if they are named in 'include'.
|
||||
//
|
||||
// See http://www.webdav.org/specs/rfc4918.html#METHOD_PROPFIND
|
||||
func allprop(ctx context.Context, fs *filesystem.FileSystem, ls LockSystem, info FileInfo, include []xml.Name) ([]Propstat, error) {
|
||||
pnames, err := propnames(ctx, fs, ls, info)
|
||||
func allprop(c *gin.Context, file fs.File, fm manager.FileManager, include []xml.Name) ([]Propstat, error) {
|
||||
pnames, err := propnames(c, file, fm)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -335,12 +432,12 @@ func allprop(ctx context.Context, fs *filesystem.FileSystem, ls LockSystem, info
|
||||
pnames = append(pnames, pn)
|
||||
}
|
||||
}
|
||||
return props(ctx, fs, ls, info, pnames)
|
||||
return props(c, file, fm, pnames)
|
||||
}
|
||||
|
||||
// Patch patches the properties of resource name. The return values are
|
||||
// constrained in the same manner as DeadPropsHolder.Patch.
|
||||
func patch(ctx context.Context, fs *filesystem.FileSystem, ls LockSystem, name string, patches []Proppatch) ([]Propstat, error) {
|
||||
func patch(c context.Context, file fs.File, fm manager.FileManager, patches []Proppatch) ([]Propstat, error) {
|
||||
conflict := false
|
||||
loop:
|
||||
for _, patch := range patches {
|
||||
@@ -372,37 +469,24 @@ loop:
|
||||
}
|
||||
|
||||
// very unlikely to be false
|
||||
exist, info := isPathExist(ctx, fs, name)
|
||||
if exist {
|
||||
var dph DeadPropsHolder
|
||||
if info.IsDir() {
|
||||
dph = &FolderDeadProps{info.(*model.Folder)}
|
||||
} else {
|
||||
dph = &FileDeadProps{info.(*model.File)}
|
||||
}
|
||||
ret, err := dph.Patch(patches)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// http://www.webdav.org/specs/rfc4918.html#ELEMENT_propstat says that
|
||||
// "The contents of the prop XML element must only list the names of
|
||||
// properties to which the result in the status element applies."
|
||||
for _, pstat := range ret {
|
||||
for i, p := range pstat.Props {
|
||||
pstat.Props[i] = Property{XMLName: p.XMLName}
|
||||
}
|
||||
}
|
||||
return ret, nil
|
||||
dph := &metadataDeadProps{
|
||||
f: file,
|
||||
fm: fm,
|
||||
}
|
||||
// The file doesn't implement the optional DeadPropsHolder interface, so
|
||||
// all patches are forbidden.
|
||||
pstat := Propstat{Status: http.StatusOK}
|
||||
for _, patch := range patches {
|
||||
for _, p := range patch.Props {
|
||||
pstat.Props = append(pstat.Props, Property{XMLName: p.XMLName})
|
||||
|
||||
ret, err := dph.Patch(c, patches)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// http://www.webdav.org/specs/rfc4918.html#ELEMENT_propstat says that
|
||||
// "The contents of the prop XML element must only list the names of
|
||||
// properties to which the result in the status element applies."
|
||||
for _, pstat := range ret {
|
||||
for i, p := range pstat.Props {
|
||||
pstat.Props[i] = Property{XMLName: p.XMLName}
|
||||
}
|
||||
}
|
||||
return []Propstat{pstat}, nil
|
||||
return ret, nil
|
||||
}
|
||||
|
||||
func escapeXML(s string) string {
|
||||
@@ -425,103 +509,68 @@ func escapeXML(s string) string {
|
||||
return s
|
||||
}
|
||||
|
||||
func findResourceType(ctx context.Context, fs *filesystem.FileSystem, ls LockSystem, name string, fi FileInfo) (string, error) {
|
||||
if fi.IsDir() {
|
||||
// ErrNotImplemented should be returned by optional interfaces if they
|
||||
// want the original implementation to be used.
|
||||
var ErrNotImplemented = errors.New("not implemented")
|
||||
|
||||
func findResourceType(ctx context.Context, fm manager.FileManager, file fs.File) (string, error) {
|
||||
if file.Type() == types.FileTypeFolder {
|
||||
return `<D:collection xmlns:D="DAV:"/>`, nil
|
||||
}
|
||||
return "", nil
|
||||
}
|
||||
|
||||
func findDisplayName(ctx context.Context, fs *filesystem.FileSystem, ls LockSystem, name string, fi FileInfo) (string, error) {
|
||||
if slashClean(name) == "/" {
|
||||
// Hide the real name of a possibly prefixed root directory.
|
||||
return "", nil
|
||||
func findDisplayName(ctx context.Context, fm manager.FileManager, file fs.File) (string, error) {
|
||||
return escapeXML(file.DisplayName()), nil
|
||||
}
|
||||
|
||||
func findContentLength(ctx context.Context, fm manager.FileManager, file fs.File) (string, error) {
|
||||
return strconv.FormatInt(file.Size(), 10), nil
|
||||
}
|
||||
|
||||
func findLastModified(ctx context.Context, fm manager.FileManager, file fs.File) (string, error) {
|
||||
return file.UpdatedAt().UTC().Format(http.TimeFormat), nil
|
||||
}
|
||||
|
||||
func findCreationDate(ctx context.Context, fm manager.FileManager, file fs.File) (string, error) {
|
||||
return file.CreatedAt().UTC().Format(http.TimeFormat), nil
|
||||
}
|
||||
|
||||
func findContentType(ctx context.Context, fm manager.FileManager, file fs.File) (string, error) {
|
||||
d := dependency.FromContext(ctx)
|
||||
return d.MimeDetector(ctx).TypeByName(file.DisplayName()), nil
|
||||
}
|
||||
|
||||
func findETag(ctx context.Context, fm manager.FileManager, file fs.File) (string, error) {
|
||||
hasher := dependency.FromContext(ctx).HashIDEncoder()
|
||||
return fmt.Sprintf(`"%s"`, hashid.EncodeEntityID(hasher, file.PrimaryEntityID())), nil
|
||||
}
|
||||
|
||||
func findQuotaUsedBytes(ctx context.Context, fm manager.FileManager, file fs.File) (string, error) {
|
||||
requester := inventory.UserFromContext(ctx)
|
||||
if file.Owner().ID != requester.ID {
|
||||
return "", ErrNotImplemented
|
||||
}
|
||||
return escapeXML(fi.GetName()), nil
|
||||
capacity, err := fm.Capacity(ctx)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return strconv.FormatInt(capacity.Used, 10), nil
|
||||
}
|
||||
|
||||
func findContentLength(ctx context.Context, fs *filesystem.FileSystem, ls LockSystem, name string, fi FileInfo) (string, error) {
|
||||
return strconv.FormatUint(fi.GetSize(), 10), nil
|
||||
func findQuotaAvailableBytes(ctx context.Context, fm manager.FileManager, file fs.File) (string, error) {
|
||||
requester := inventory.UserFromContext(ctx)
|
||||
if file.Owner().ID != requester.ID {
|
||||
return "", ErrNotImplemented
|
||||
}
|
||||
capacity, err := fm.Capacity(ctx)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return strconv.FormatInt(capacity.Total-capacity.Used, 10), nil
|
||||
}
|
||||
|
||||
func findLastModified(ctx context.Context, fs *filesystem.FileSystem, ls LockSystem, name string, fi FileInfo) (string, error) {
|
||||
return fi.ModTime().UTC().Format(http.TimeFormat), nil
|
||||
}
|
||||
|
||||
// ErrNotImplemented should be returned by optional interfaces if they
|
||||
// want the original implementation to be used.
|
||||
var ErrNotImplemented = errors.New("not implemented")
|
||||
|
||||
// ContentTyper is an optional interface for the os.FileInfo
|
||||
// objects returned by the FileSystem.
|
||||
//
|
||||
// If this interface is defined then it will be used to read the
|
||||
// content type from the object.
|
||||
//
|
||||
// If this interface is not defined the file will be opened and the
|
||||
// content type will be guessed from the initial contents of the file.
|
||||
type ContentTyper interface {
|
||||
// ContentType returns the content type for the file.
|
||||
//
|
||||
// If this returns error ErrNotImplemented then the error will
|
||||
// be ignored and the base implementation will be used
|
||||
// instead.
|
||||
ContentType(ctx context.Context) (string, error)
|
||||
}
|
||||
|
||||
func findContentType(ctx context.Context, fs *filesystem.FileSystem, ls LockSystem, name string, fi FileInfo) (string, error) {
|
||||
//if do, ok := fi.(ContentTyper); ok {
|
||||
// ctype, err := do.ContentType(ctx)
|
||||
// if err != ErrNotImplemented {
|
||||
// return ctype, err
|
||||
// }
|
||||
//}
|
||||
//f, err := fs.OpenFile(ctx, name, os.O_RDONLY, 0)
|
||||
//if err != nil {
|
||||
// return "", err
|
||||
//}
|
||||
//defer f.Close()
|
||||
//// This implementation is based on serveContent's code in the standard net/http package.
|
||||
//ctype := mime.TypeByExtension(filepath.Ext(name))
|
||||
//if ctype != "" {
|
||||
// return ctype, nil
|
||||
//}
|
||||
//// Read a chunk to decide between utf-8 text and binary.
|
||||
//var buf [512]byte
|
||||
//n, err := io.ReadFull(f, buf[:])
|
||||
//if err != nil && err != io.EOF && err != io.ErrUnexpectedEOF {
|
||||
// return "", err
|
||||
//}
|
||||
//ctype = http.DetectContentType(buf[:n])
|
||||
//// Rewind file.
|
||||
//_, err = f.Seek(0, os.SEEK_SET)
|
||||
//return ctype, err
|
||||
return mime.TypeByExtension(filepath.Ext(name)), nil
|
||||
}
|
||||
|
||||
// ETager is an optional interface for the os.FileInfo objects
|
||||
// returned by the FileSystem.
|
||||
//
|
||||
// If this interface is defined then it will be used to read the ETag
|
||||
// for the object.
|
||||
//
|
||||
// If this interface is not defined an ETag will be computed using the
|
||||
// ModTime() and the Size() methods of the os.FileInfo object.
|
||||
type ETager interface {
|
||||
// ETag returns an ETag for the file. This should be of the
|
||||
// form "value" or W/"value"
|
||||
//
|
||||
// If this returns error ErrNotImplemented then the error will
|
||||
// be ignored and the base implementation will be used
|
||||
// instead.
|
||||
ETag(ctx context.Context) (string, error)
|
||||
}
|
||||
|
||||
func findETag(ctx context.Context, fs *filesystem.FileSystem, ls LockSystem, reqPath string, fi FileInfo) (string, error) {
|
||||
return fmt.Sprintf(`"%x%x"`, fi.ModTime().UnixNano(), fi.GetSize()), nil
|
||||
}
|
||||
|
||||
func findSupportedLock(ctx context.Context, fs *filesystem.FileSystem, ls LockSystem, name string, fi FileInfo) (string, error) {
|
||||
func findSupportedLock(ctx context.Context, fm manager.FileManager, file fs.File) (string, error) {
|
||||
return `` +
|
||||
`<D:lockentry xmlns:D="DAV:">` +
|
||||
`<D:lockscope><D:exclusive/></D:lockscope>` +
|
||||
|
||||
1266
pkg/webdav/webdav.go
1266
pkg/webdav/webdav.go
File diff suppressed because it is too large
Load Diff
@@ -11,6 +11,7 @@ import (
|
||||
"bytes"
|
||||
"encoding/xml"
|
||||
"fmt"
|
||||
"github.com/cloudreve/Cloudreve/v4/pkg/filemanager/lock"
|
||||
"io"
|
||||
"net/http"
|
||||
"time"
|
||||
@@ -32,7 +33,7 @@ import (
|
||||
// In the long term, this package should use the standard library's version
|
||||
// only, and the internal fork deleted, once
|
||||
// https://github.com/golang/go/issues/13400 is resolved.
|
||||
ixml "github.com/cloudreve/Cloudreve/v3/pkg/webdav/internal/xml"
|
||||
ixml "github.com/cloudreve/Cloudreve/v4/pkg/webdav/internal/xml"
|
||||
)
|
||||
|
||||
// http://www.webdav.org/specs/rfc4918.html#ELEMENT_lockinfo
|
||||
@@ -81,7 +82,7 @@ func (c *countingReader) Read(p []byte) (int, error) {
|
||||
return n, err
|
||||
}
|
||||
|
||||
func writeLockInfo(w io.Writer, token string, ld LockDetails) (int, error) {
|
||||
func writeLockInfo(w io.Writer, token string, ld lock.LockDetails) (int, error) {
|
||||
depth := "infinity"
|
||||
if ld.ZeroDepth {
|
||||
depth = "0"
|
||||
@@ -95,9 +96,9 @@ func writeLockInfo(w io.Writer, token string, ld LockDetails) (int, error) {
|
||||
" <D:owner>%s</D:owner>\n"+
|
||||
" <D:timeout>Second-%d</D:timeout>\n"+
|
||||
" <D:locktoken><D:href>%s</D:href></D:locktoken>\n"+
|
||||
" <D:lockroot><D:href>%s</D:href></D:lockroot>\n"+
|
||||
" <D:lockroot><D:href>/%s</D:href></D:lockroot>\n"+
|
||||
"</D:activelock></D:lockdiscovery></D:prop>",
|
||||
depth, ld.OwnerXML, timeout, escape(token), escape(ld.Root),
|
||||
depth, ld.Owner.Application.InnerXML, timeout, escape(token), escape(ld.Root),
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user