From 0d5300abdabea02e1624f7a9f561e9c3ed64a2a0 Mon Sep 17 00:00:00 2001 From: zicla Date: Thu, 18 Apr 2019 23:28:18 +0800 Subject: [PATCH] Keep doing the webdav --- rest/dav/prop.go | 4 ++-- rest/dav/prop_test.go | 2 +- rest/dav/webdav.go | 6 +++--- rest/dav/xml.go | 2 +- rest/dav/xml_test.go | 2 +- rest/dav_service.go | 43 +++++++++++++++++++++++++++++++++++++------ rest/matter_dao.go | 26 +++++++++++++++++++++++++- 7 files changed, 70 insertions(+), 15 deletions(-) diff --git a/rest/dav/prop.go b/rest/dav/prop.go index 77125ad..a1173f6 100644 --- a/rest/dav/prop.go +++ b/rest/dav/prop.go @@ -214,7 +214,7 @@ func props(ctx context.Context, fs FileSystem, ls LockSystem, name string, pname } // Propnames returns the property names defined for resource name. -func propnames(ctx context.Context, fs FileSystem, ls LockSystem, name string) ([]xml.Name, error) { +func Propnames(ctx context.Context, fs FileSystem, ls LockSystem, name string) ([]xml.Name, error) { f, err := fs.OpenFile(ctx, name, os.O_RDONLY, 0) if err != nil { return nil, err @@ -255,7 +255,7 @@ func propnames(ctx context.Context, fs FileSystem, ls LockSystem, name string) ( // // See http://www.webdav.org/specs/rfc4918.html#METHOD_PROPFIND func allprop(ctx context.Context, fs FileSystem, ls LockSystem, name string, include []xml.Name) ([]Propstat, error) { - pnames, err := propnames(ctx, fs, ls, name) + pnames, err := Propnames(ctx, fs, ls, name) if err != nil { return nil, err } diff --git a/rest/dav/prop_test.go b/rest/dav/prop_test.go index 808e3c1..c816d2c 100644 --- a/rest/dav/prop_test.go +++ b/rest/dav/prop_test.go @@ -522,7 +522,7 @@ func TestMemPS(t *testing.T) { var propstats []Propstat switch op.op { case "propname": - pnames, err := propnames(ctx, fs, ls, op.name) + pnames, err := Propnames(ctx, fs, ls, op.name) if err != nil { t.Errorf("%s: got error %v, want nil", desc, err) continue diff --git a/rest/dav/webdav.go b/rest/dav/webdav.go index a8ceedd..99cb234 100644 --- a/rest/dav/webdav.go +++ b/rest/dav/webdav.go @@ -543,7 +543,7 @@ func (this *Handler) handlePropfind(writer http.ResponseWriter, request *http.Re reqPath, info.Name(), info.IsDir(), info.Mode(), info.ModTime(), info.Size()) var propstats []Propstat if pf.Propname != nil { - pnames, err := propnames(ctx, this.FileSystem, this.LockSystem, reqPath) + pnames, err := Propnames(ctx, this.FileSystem, this.LockSystem, reqPath) if err != nil { return err } @@ -570,7 +570,7 @@ func (this *Handler) handlePropfind(writer http.ResponseWriter, request *http.Re } walkErr := walkFS(ctx, this.FileSystem, depth, reqPath, fileInfo, walkFn) - closeErr := multiStatusWriter.close() + closeErr := multiStatusWriter.Close() if walkErr != nil { return http.StatusInternalServerError, walkErr } @@ -609,7 +609,7 @@ func (this *Handler) handleProppatch(w http.ResponseWriter, r *http.Request) (st } mw := MultiStatusWriter{Writer: w} writeErr := mw.Write(makePropstatResponse(r.URL.Path, pstats)) - closeErr := mw.close() + closeErr := mw.Close() if writeErr != nil { return http.StatusInternalServerError, writeErr } diff --git a/rest/dav/xml.go b/rest/dav/xml.go index fa1adf0..375dab0 100644 --- a/rest/dav/xml.go +++ b/rest/dav/xml.go @@ -377,7 +377,7 @@ func (this *MultiStatusWriter) writeHeader() error { // an error if the multistatus response could not be completed. If both the // return value and field Encoder of w are nil, then no multistatus response has // been written. -func (this *MultiStatusWriter) close() error { +func (this *MultiStatusWriter) Close() error { if this.Encoder == nil { return nil } diff --git a/rest/dav/xml_test.go b/rest/dav/xml_test.go index 1fe6328..7b574fb 100644 --- a/rest/dav/xml_test.go +++ b/rest/dav/xml_test.go @@ -582,7 +582,7 @@ loop: continue loop } } - if err := w.close(); err != tc.wantErr { + if err := w.Close(); err != tc.wantErr { t.Errorf("%s: got close error %v, want %v", tc.desc, err, tc.wantErr) continue diff --git a/rest/dav_service.go b/rest/dav_service.go index 66c729d..eb5a958 100644 --- a/rest/dav_service.go +++ b/rest/dav_service.go @@ -1,6 +1,7 @@ package rest import ( + "encoding/xml" "fmt" "net/http" "net/url" @@ -50,9 +51,18 @@ func (this *DavService) makePropstatResponse(href string, pstats []dav.Propstat) return &resp } +//从一个matter中获取其propnames,每个propname都是一个xml标签。 +func (this *DavService) PropNames(matter *Matter) []xml.Name { + + return nil + +} + //处理 方法 func (this *DavService) HandlePropfind(writer http.ResponseWriter, request *http.Request, subPath string) { + fmt.Printf("列出文件/文件夹 %s\n", subPath) + //获取请求者 user := this.checkUser(writer, request) @@ -63,16 +73,37 @@ func (this *DavService) HandlePropfind(writer http.ResponseWriter, request *http propfind, _, err := dav.ReadPropfind(request.Body) this.PanicError(err) + //寻找符合条件的matter. + matters := this.matterDao.ListByUserUuidAndPath(user.Uuid, subPath) + if len(matters) == 0 { + this.PanicNotFound("%s不存在", subPath) + } + //准备一个输出结果的Writer multiStatusWriter := dav.MultiStatusWriter{Writer: writer} - var propstats []dav.Propstat - propstats = append(propstats, dav.Propstat{ - ResponseDescription: "有点问题", - }) - response := this.makePropstatResponse("/eyeblue/ready/go", propstats) + for _, matter := range matters { - err = multiStatusWriter.Write(response) + fmt.Printf("开始分析 %s\n", matter.Name) + + var propstats []dav.Propstat + var props = make([]dav.Property, 0) + props = append(props, dav.Property{ + XMLName: xml.Name{Space: "DAV:"}, + }) + propstats = append(propstats, dav.Propstat{ + Props: props, + ResponseDescription: "有点问题", + }) + + response := this.makePropstatResponse("/eyeblue/ready/go", propstats) + + err = multiStatusWriter.Write(response) + this.PanicError(err) + } + + //闭合 + err = multiStatusWriter.Close() this.PanicError(err) fmt.Printf("%v %v \n", matter.Name, propfind.Prop) diff --git a/rest/matter_dao.go b/rest/matter_dao.go index 550b2fc..72ec197 100644 --- a/rest/matter_dao.go +++ b/rest/matter_dao.go @@ -132,6 +132,31 @@ func (this *MatterDao) ListByUserUuidAndPuuidAndDirAndName(userUuid string, puui return matters } +//获取某个用户的某个文件夹下的某个名字的文件(或文件夹)列表 +func (this *MatterDao) ListByUserUuidAndPath(userUuid string, path string) []*Matter { + + var wp = &WherePair{} + + if userUuid == "" { + this.PanicBadRequest("userUuid必填!") + } + + if path == "" { + this.PanicBadRequest("path必填!") + } + + wp = wp.And(&WherePair{Query: "user_uuid = ?", Args: []interface{}{userUuid}}) + + wp = wp.And(&WherePair{Query: "path = ?", Args: []interface{}{path}}) + + var matters []*Matter + db := CONTEXT.DB.Model(&Matter{}).Where(wp.Query, wp.Args...).Find(&matters) + + this.PanicError(db.Error) + + return matters +} + //获取某个文件夹下所有的文件和子文件 func (this *MatterDao) List(puuid string, userUuid string, sortArray []OrderPair) []*Matter { var matters []*Matter @@ -291,7 +316,6 @@ func (this *MatterDao) checkByUserUuidAndPath(userUuid string, path string) *Mat var matter = &Matter{} db := CONTEXT.DB.Model(&Matter{}).Where(wp.Query, wp.Args...).First(matter) - if db.Error != nil { if db.Error.Error() == DB_ERROR_NOT_FOUND { this.PanicNotFound("%s 不存在", path)