Add the quota method.

This commit is contained in:
zicla 2019-04-22 01:19:34 +08:00
parent 8632d7686b
commit f0be23879d
3 changed files with 50 additions and 14 deletions

View File

@ -1,7 +1,9 @@
package rest
import (
"bytes"
"fmt"
"io/ioutil"
"net/http"
"regexp"
)
@ -140,6 +142,19 @@ func (this *DavController) Index(writer http.ResponseWriter, request *http.Reque
fmt.Printf("\n------Body------\n")
//ioutil.ReadAll 不可重复读,第二次读的时候就什么都没有了。
bodyBytes, err := ioutil.ReadAll(request.Body)
if err != nil {
fmt.Println("读取body时出错" + err.Error())
}
fmt.Println(string(bodyBytes))
//关闭之后再重新赋值
err = request.Body.Close()
if err != nil {
panic(err)
}
request.Body = ioutil.NopCloser(bytes.NewBuffer(bodyBytes))
fmt.Println("------------------")
//获取请求者

View File

@ -13,14 +13,14 @@ var WEBDAV_PREFFIX = "/api/dav"
//动态的文件属性
type LiveProp struct {
findFn func(matter *Matter) string
findFn func(user *User, matter *Matter) string
dir bool
}
//所有的动态属性定义及其值的获取方式
var LivePropMap = map[xml.Name]LiveProp{
{Space: "DAV:", Local: "resourcetype"}: {
findFn: func(matter *Matter) string {
findFn: func(user *User, matter *Matter) string {
if matter.Dir {
return `<D:collection xmlns:D="DAV:"/>`
} else {
@ -30,7 +30,7 @@ var LivePropMap = map[xml.Name]LiveProp{
dir: true,
},
{Space: "DAV:", Local: "displayname"}: {
findFn: func(matter *Matter) string {
findFn: func(user *User, matter *Matter) string {
if dav.SlashClean(matter.Name) == "/" {
return ""
} else {
@ -40,13 +40,13 @@ var LivePropMap = map[xml.Name]LiveProp{
dir: true,
},
{Space: "DAV:", Local: "getcontentlength"}: {
findFn: func(matter *Matter) string {
findFn: func(user *User, matter *Matter) string {
return strconv.FormatInt(matter.Size, 10)
},
dir: false,
},
{Space: "DAV:", Local: "getlastmodified"}: {
findFn: func(matter *Matter) string {
findFn: func(user *User, matter *Matter) string {
return matter.UpdateTime.UTC().Format(http.TimeFormat)
},
// http://webdav.org/specs/rfc4918.html#PROPERTY_getlastmodified
@ -67,7 +67,7 @@ var LivePropMap = map[xml.Name]LiveProp{
dir: false,
},
{Space: "DAV:", Local: "getcontenttype"}: {
findFn: func(matter *Matter) string {
findFn: func(user *User, matter *Matter) string {
if matter.Dir {
return ""
} else {
@ -77,7 +77,7 @@ var LivePropMap = map[xml.Name]LiveProp{
dir: false,
},
{Space: "DAV:", Local: "getetag"}: {
findFn: func(matter *Matter) string {
findFn: func(user *User, matter *Matter) string {
return fmt.Sprintf(`"%x%x"`, matter.UpdateTime.UnixNano(), matter.Size)
},
// findETag implements ETag as the concatenated hex values of a file's
@ -90,7 +90,7 @@ var LivePropMap = map[xml.Name]LiveProp{
// active locks on a resource.
{Space: "DAV:", Local: "lockdiscovery"}: {},
{Space: "DAV:", Local: "supportedlock"}: {
findFn: func(matter *Matter) string {
findFn: func(user *User, matter *Matter) string {
return `` +
`<D:lockentry xmlns:D="DAV:">` +
`<D:lockscope><D:exclusive/></D:lockscope>` +
@ -99,4 +99,25 @@ var LivePropMap = map[xml.Name]LiveProp{
},
dir: true,
},
{Space: "DAV:", Local: "quota-available-bytes"}: {
findFn: func(user *User, matter *Matter) string {
var size int64 = 0
if user.SizeLimit >= 0 {
size = user.SizeLimit
} else {
//没有限制默认100G
size = 100 * 1024 * 1024 * 1024
}
return fmt.Sprintf(`%d`, size)
},
dir: true,
},
{Space: "DAV:", Local: "quota-used-bytes"}: {
findFn: func(user *User, matter *Matter) string {
//已使用大小默认0
return fmt.Sprintf(`%d`, 0)
},
dir: true,
},
}

View File

@ -78,7 +78,7 @@ func (this *DavService) makePropstatResponse(href string, pstats []dav.Propstat)
}
//从一个matter中获取其 []dav.Propstat
func (this *DavService) PropstatsFromXmlNames(matter *Matter, xmlNames []xml.Name) []dav.Propstat {
func (this *DavService) PropstatsFromXmlNames(user *User, matter *Matter, xmlNames []xml.Name) []dav.Propstat {
propstats := make([]dav.Propstat, 0)
@ -89,7 +89,7 @@ func (this *DavService) PropstatsFromXmlNames(matter *Matter, xmlNames []xml.Nam
// Otherwise, it must either be a live property or we don't know it.
if liveProp := LivePropMap[xmlName]; liveProp.findFn != nil && (liveProp.dir || !matter.Dir) {
innerXML := liveProp.findFn(matter)
innerXML := liveProp.findFn(user, matter)
properties = append(properties, dav.Property{
XMLName: xmlName,
@ -126,7 +126,7 @@ func (this *DavService) AllPropXmlNames(matter *Matter) []xml.Name {
}
//从一个matter中获取其 []dav.Propstat
func (this *DavService) Propstats(matter *Matter, propfind dav.Propfind) []dav.Propstat {
func (this *DavService) Propstats(user *User, matter *Matter, propfind dav.Propfind) []dav.Propstat {
propstats := make([]dav.Propstat, 0)
if propfind.Propname != nil {
@ -136,10 +136,10 @@ func (this *DavService) Propstats(matter *Matter, propfind dav.Propfind) []dav.P
//TODO: 如果include中还有内容那么包含进去。
xmlNames := this.AllPropXmlNames(matter)
propstats = this.PropstatsFromXmlNames(matter, xmlNames)
propstats = this.PropstatsFromXmlNames(user, matter, xmlNames)
} else {
propstats = this.PropstatsFromXmlNames(matter, propfind.Prop)
propstats = this.PropstatsFromXmlNames(user, matter, propfind.Prop)
}
return propstats
@ -185,7 +185,7 @@ func (this *DavService) HandlePropfind(writer http.ResponseWriter, request *http
fmt.Printf("处理Matter %s\n", matter.Path)
propstats := this.Propstats(matter, propfind)
propstats := this.Propstats(user, matter, propfind)
path := fmt.Sprintf("%s%s", WEBDAV_PREFFIX, matter.Path)
response := this.makePropstatResponse(path, propstats)