Finish the prop set feature.
This commit is contained in:
		@ -7,6 +7,7 @@ import (
 | 
				
			|||||||
	"github.com/eyebluecn/tank/code/tool/dav/xml"
 | 
						"github.com/eyebluecn/tank/code/tool/dav/xml"
 | 
				
			||||||
	"github.com/eyebluecn/tank/code/tool/result"
 | 
						"github.com/eyebluecn/tank/code/tool/result"
 | 
				
			||||||
	"github.com/eyebluecn/tank/code/tool/util"
 | 
						"github.com/eyebluecn/tank/code/tool/util"
 | 
				
			||||||
 | 
						"github.com/eyebluecn/tank/code/tool/webdav"
 | 
				
			||||||
	"io/ioutil"
 | 
						"io/ioutil"
 | 
				
			||||||
	"net/http"
 | 
						"net/http"
 | 
				
			||||||
	"net/url"
 | 
						"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
 | 
					//handle download
 | 
				
			||||||
func (this *DavService) HandleGetHeadPost(writer http.ResponseWriter, request *http.Request, user *User, subPath string) {
 | 
					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."))
 | 
						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.
 | 
					//hanle all the request.
 | 
				
			||||||
func (this *DavService) HandleDav(writer http.ResponseWriter, request *http.Request, user *User, subPath string) {
 | 
					func (this *DavService) HandleDav(writer http.ResponseWriter, request *http.Request, user *User, subPath string) {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -6,6 +6,7 @@ import (
 | 
				
			|||||||
	"github.com/eyebluecn/tank/code/tool/i18n"
 | 
						"github.com/eyebluecn/tank/code/tool/i18n"
 | 
				
			||||||
	"github.com/eyebluecn/tank/code/tool/result"
 | 
						"github.com/eyebluecn/tank/code/tool/result"
 | 
				
			||||||
	"github.com/eyebluecn/tank/code/tool/util"
 | 
						"github.com/eyebluecn/tank/code/tool/util"
 | 
				
			||||||
 | 
						jsoniter "github.com/json-iterator/go"
 | 
				
			||||||
	"net/http"
 | 
						"net/http"
 | 
				
			||||||
	"regexp"
 | 
						"regexp"
 | 
				
			||||||
	"strings"
 | 
						"strings"
 | 
				
			||||||
@ -114,3 +115,31 @@ func CheckMatterName(request *http.Request, name string) string {
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
	return name
 | 
						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)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
@ -593,7 +593,7 @@ func (h *Handler) handleProppatch(w http.ResponseWriter, r *http.Request) (statu
 | 
				
			|||||||
		}
 | 
							}
 | 
				
			||||||
		return http.StatusMethodNotAllowed, err
 | 
							return http.StatusMethodNotAllowed, err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	patches, status, err := readProppatch(r.Body)
 | 
						patches, status, err := ReadProppatch(r.Body)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		return status, err
 | 
							return status, err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
				
			|||||||
@ -493,7 +493,7 @@ type propertyupdate struct {
 | 
				
			|||||||
	SetRemove []setRemove `xml:",any"`
 | 
						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
 | 
						var pu propertyupdate
 | 
				
			||||||
	if err = ixml.NewDecoder(r).Decode(&pu); err != nil {
 | 
						if err = ixml.NewDecoder(r).Decode(&pu); err != nil {
 | 
				
			||||||
		return nil, http.StatusBadRequest, err
 | 
							return nil, http.StatusBadRequest, err
 | 
				
			||||||
 | 
				
			|||||||
@ -705,7 +705,7 @@ func TestReadProppatch(t *testing.T) {
 | 
				
			|||||||
	}}
 | 
						}}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	for _, tc := range testCases {
 | 
						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 tc.wantStatus != 0 {
 | 
				
			||||||
			if err == nil {
 | 
								if err == nil {
 | 
				
			||||||
				t.Errorf("%s: got nil error, want non-nil", tc.desc)
 | 
									t.Errorf("%s: got nil error, want non-nil", tc.desc)
 | 
				
			||||||
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user