add the first version of dav

This commit is contained in:
zicla
2019-04-12 19:52:35 +08:00
parent d3a2ba9837
commit e0b78932a7
5 changed files with 103 additions and 105 deletions

View File

@ -2,6 +2,8 @@ package rest
import (
"net/http"
"os"
"tank/rest/dav"
)
//@Service
@ -47,74 +49,70 @@ func parseDepth(s string) int {
return invalidDepth
}
//处理 方法
func (this *DavService) HandlePropfind(w http.ResponseWriter, r *http.Request) {
//basePath := "/Users/fusu/d/group/golang/src/tank/tmp/dav"
//
//reqPath := r.URL.Path
//
//ctx := r.Context()
//
//fi, err := os.Stat(basePath + reqPath)
//if err != nil {
// this.PanicError(err)
//}
//
//depth := infiniteDepth
//if hdr := r.Header.Get("Depth"); hdr != "" {
// depth = parseDepth(hdr)
// if depth == invalidDepth {
// this.PanicBadRequest("Depth指定错误")
// }
//}
//
//pf, status, err := readPropfind(r.Body)
//if err != nil {
// return status, err
//}
//
//mw := multistatusWriter{w: w}
//
//walkFn := func(reqPath string, info os.FileInfo, err error) error {
// if err != nil {
// return err
// }
// var pstats []Propstat
// if pf.Propname != nil {
// pnames, err := propnames(ctx, h.FileSystem, h.LockSystem, reqPath)
// if err != nil {
// return err
// }
// pstat := Propstat{Status: http.StatusOK}
// for _, xmlname := range pnames {
// pstat.Props = append(pstat.Props, Property{XMLName: xmlname})
// }
// pstats = append(pstats, pstat)
// } else if pf.Allprop != nil {
// pstats, err = allprop(ctx, h.FileSystem, h.LockSystem, reqPath, pf.Prop)
// } else {
// pstats, err = props(ctx, h.FileSystem, h.LockSystem, reqPath, pf.Prop)
// }
// if err != nil {
// return err
// }
// href := path.Join(h.Prefix, reqPath)
// if info.IsDir() {
// href += "/"
// }
// return mw.write(makePropstatResponse(href, pstats))
//}
//
//walkErr := walkFS(ctx, h.FileSystem, depth, reqPath, fi, walkFn)
//closeErr := mw.close()
//if walkErr != nil {
// return http.StatusInternalServerError, walkErr
//}
//if closeErr != nil {
// return http.StatusInternalServerError, closeErr
//}
//return 0, nil
basePath := "/Users/fusu/d/group/golang/src/tank/tmp/dav"
fileSystem := dav.Dir("/Users/fusu/d/group/golang/src/tank/tmp/dav")
lockSystem := dav.NewMemLS()
reqPath := r.URL.Path
ctx := r.Context()
fi, err := os.Stat(basePath + reqPath)
if err != nil {
this.PanicError(err)
}
depth := infiniteDepth
if hdr := r.Header.Get("Depth"); hdr != "" {
depth = parseDepth(hdr)
if depth == invalidDepth {
this.PanicBadRequest("Depth指定错误")
}
}
pf, _, err := dav.ReadPropfind(r.Body)
this.PanicError(err)
mw := dav.MultiStatusWriter{Writer: w}
walkFn := func(reqPath string, info os.FileInfo, err error) error {
if err != nil {
return err
}
var pstats []dav.Propstat
if pf.Propname != nil {
pnames, err := dav.Propnames(ctx, fileSystem, lockSystem, reqPath)
if err != nil {
return err
}
pstat := dav.Propstat{Status: http.StatusOK}
for _, xmlname := range pnames {
pstat.Props = append(pstat.Props, dav.Property{XMLName: xmlname})
}
pstats = append(pstats, pstat)
} else if pf.Allprop != nil {
pstats, err = dav.Allprop(ctx, fileSystem, lockSystem, reqPath, pf.Prop)
} else {
pstats, err = dav.Props(ctx, fileSystem, lockSystem, reqPath, pf.Prop)
}
if err != nil {
return err
}
href := reqPath
if info.IsDir() {
href += "/"
}
return mw.Write(dav.MakePropstatResponse(href, pstats))
}
walkErr := dav.WalkFS(ctx, fileSystem, depth, reqPath, fi, walkFn)
closeErr := mw.Close()
this.PanicError(walkErr)
this.PanicError(closeErr)
}