diff --git a/code/rest/dav_service.go b/code/rest/dav_service.go index 26ecf9b..f207273 100644 --- a/code/rest/dav_service.go +++ b/code/rest/dav_service.go @@ -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" @@ -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) { @@ -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) { diff --git a/code/rest/matter_model.go b/code/rest/matter_model.go index 9cddc19..9bc5ffb 100644 --- a/code/rest/matter_model.go +++ b/code/rest/matter_model.go @@ -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" @@ -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) +} diff --git a/code/tool/webdav/webdav.go b/code/tool/webdav/webdav.go index ea11636..5162df0 100644 --- a/code/tool/webdav/webdav.go +++ b/code/tool/webdav/webdav.go @@ -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 } diff --git a/code/tool/webdav/xml.go b/code/tool/webdav/xml.go index 1a856bc..bb6ae48 100644 --- a/code/tool/webdav/xml.go +++ b/code/tool/webdav/xml.go @@ -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 diff --git a/code/tool/webdav/xml_test.go b/code/tool/webdav/xml_test.go index 804cc2a..783c069 100644 --- a/code/tool/webdav/xml_test.go +++ b/code/tool/webdav/xml_test.go @@ -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)