Skip to content

Commit

Permalink
Finish the prop set feature.
Browse files Browse the repository at this point in the history
  • Loading branch information
zicla committed Mar 25, 2020
1 parent 1f047e2 commit 4bfae38
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 9 deletions.
55 changes: 49 additions & 6 deletions code/rest/dav_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/eyebluecn/tank/code/tool/dav/xml"
"github.com/eyebluecn/tank/code/tool/result"
"github.com/eyebluecn/tank/code/tool/util"
"github.com/eyebluecn/tank/code/tool/webdav"
"io/ioutil"
"net/http"
"net/url"
Expand Down Expand Up @@ -196,6 +197,54 @@ func (this *DavService) HandlePropfind(writer http.ResponseWriter, request *http

}

//change the file's property
func (this *DavService) HandleProppatch(writer http.ResponseWriter, request *http.Request, user *User, subPath string) {

fmt.Printf("PROPPATCH %s\n", subPath)

matter := this.matterDao.checkByUserUuidAndPath(user.Uuid, subPath)

patches, status, err := webdav.ReadProppatch(request.Body)
this.PanicError(err)

fmt.Println("status:%v", status)

//prepare a multiStatusWriter.
multiStatusWriter := &dav.MultiStatusWriter{Writer: writer}

propstats := make([]dav.Propstat, 0)
propMap := matter.FetchPropMap()
for _, patch := range patches {
propStat := dav.Propstat{Status: http.StatusOK}
if patch.Remove {

} else {
for _, prop := range patch.Props {
propMap[prop.XMLName.Local] = string(prop.InnerXML)

propStat.Props = append(propStat.Props, dav.Property{XMLName: xml.Name{Space: prop.XMLName.Space, Local: prop.XMLName.Local}})

}
}

propstats = append(propstats, propStat)

}
matter.SetPropMap(propMap)
// update the matter
this.matterDao.Save(matter)

visitPath := fmt.Sprintf("%s%s", WEBDAV_PREFIX, matter.Path)
response := this.makePropstatResponse(visitPath, propstats)

err1 := multiStatusWriter.Write(response)
this.PanicError(err1)

err2 := multiStatusWriter.Close()
this.PanicError(err2)

}

//handle download
func (this *DavService) HandleGetHeadPost(writer http.ResponseWriter, request *http.Request, user *User, subPath string) {

Expand Down Expand Up @@ -454,12 +503,6 @@ func (this *DavService) HandleUnlock(writer http.ResponseWriter, request *http.R
panic(result.BadRequest("not support UNLOCK yet."))
}

//change the file's property
func (this *DavService) HandleProppatch(writer http.ResponseWriter, request *http.Request, user *User, subPath string) {

panic(result.BadRequest("not support PROPPATCH yet."))
}

//hanle all the request.
func (this *DavService) HandleDav(writer http.ResponseWriter, request *http.Request, user *User, subPath string) {

Expand Down
29 changes: 29 additions & 0 deletions code/rest/matter_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/eyebluecn/tank/code/tool/i18n"
"github.com/eyebluecn/tank/code/tool/result"
"github.com/eyebluecn/tank/code/tool/util"
jsoniter "github.com/json-iterator/go"
"net/http"
"regexp"
"strings"
Expand Down Expand Up @@ -114,3 +115,31 @@ func CheckMatterName(request *http.Request, name string) string {
}
return name
}

//fetch the props
func (this *Matter) FetchPropMap() map[string]string {

m := make(map[string]string)
json := this.Prop
if json == "" {
json = EMPTY_JSON_MAP
}

err := jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal([]byte(json), &m)
if err != nil {
panic(err)
}

return m
}

//fetch the props
func (this *Matter) SetPropMap(propMap map[string]string) {

b, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(propMap)
if err != nil {
panic(err)
}

this.Prop = string(b)
}
2 changes: 1 addition & 1 deletion code/tool/webdav/webdav.go
Original file line number Diff line number Diff line change
Expand Up @@ -593,7 +593,7 @@ func (h *Handler) handleProppatch(w http.ResponseWriter, r *http.Request) (statu
}
return http.StatusMethodNotAllowed, err
}
patches, status, err := readProppatch(r.Body)
patches, status, err := ReadProppatch(r.Body)
if err != nil {
return status, err
}
Expand Down
2 changes: 1 addition & 1 deletion code/tool/webdav/xml.go
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@ type propertyupdate struct {
SetRemove []setRemove `xml:",any"`
}

func readProppatch(r io.Reader) (patches []Proppatch, status int, err error) {
func ReadProppatch(r io.Reader) (patches []Proppatch, status int, err error) {
var pu propertyupdate
if err = ixml.NewDecoder(r).Decode(&pu); err != nil {
return nil, http.StatusBadRequest, err
Expand Down
2 changes: 1 addition & 1 deletion code/tool/webdav/xml_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -705,7 +705,7 @@ func TestReadProppatch(t *testing.T) {
}}

for _, tc := range testCases {
pp, status, err := readProppatch(strings.NewReader(tc.input))
pp, status, err := ReadProppatch(strings.NewReader(tc.input))
if tc.wantStatus != 0 {
if err == nil {
t.Errorf("%s: got nil error, want non-nil", tc.desc)
Expand Down

0 comments on commit 4bfae38

Please sign in to comment.