diff --git a/rest/dav/xml.go b/rest/dav/xml.go index 9281ff6..c87c82e 100644 --- a/rest/dav/xml.go +++ b/rest/dav/xml.go @@ -50,7 +50,7 @@ type Owner struct { } func ReadLockInfo(r io.Reader) (li LockInfo, status int, err error) { - c := &CountingReader{r: r} + c := &CountingReader{reader: r} if err = ixml.NewDecoder(c).Decode(&li); err != nil { if err == io.EOF { if c.n == 0 { @@ -72,12 +72,12 @@ func ReadLockInfo(r io.Reader) (li LockInfo, status int, err error) { //这是一个带字节计数器的Reader,可以知道总共读取了多少个字节。 type CountingReader struct { - n int - r io.Reader + n int + reader io.Reader } func (c *CountingReader) Read(p []byte) (int, error) { - n, err := c.r.Read(p) + n, err := c.reader.Read(p) c.n += n return n, err } @@ -178,8 +178,8 @@ type Propfind struct { } //从request中读出需要的属性。比如:getcontentlength 大小 creationdate 创建时间 -func ReadPropfind(r io.Reader) (propfind Propfind, status int, err error) { - c := CountingReader{r: r} +func ReadPropfind(reader io.Reader) (propfind Propfind, status int, err error) { + c := CountingReader{reader: reader} if err = ixml.NewDecoder(&c).Decode(&propfind); err != nil { if err == io.EOF { if c.n == 0 { diff --git a/rest/dav_controller.go b/rest/dav_controller.go index e9f96b4..e1036d9 100644 --- a/rest/dav_controller.go +++ b/rest/dav_controller.go @@ -2,7 +2,6 @@ package rest import ( "fmt" - "io/ioutil" "net/http" "regexp" ) @@ -139,11 +138,7 @@ func (this *DavController) Index(writer http.ResponseWriter, request *http.Reque } fmt.Printf("\n------Body:------\n") - body, err := ioutil.ReadAll(request.Body) - if err != nil { - fmt.Println("读取body时出错" + err.Error()) - } - fmt.Println(string(body)) + //ioutil.ReadAll 不可重复读,第二次读的时候就什么都没有了。 fmt.Println("------------------") @@ -152,6 +147,7 @@ func (this *DavController) Index(writer http.ResponseWriter, request *http.Reque method := request.Method if method == "PROPFIND" { + //列出文件夹或者目录详情 this.davService.HandlePropfind(writer, request, user, subPath) @@ -159,6 +155,10 @@ func (this *DavController) Index(writer http.ResponseWriter, request *http.Reque //请求文件详情(下载) this.davService.HandleGet(writer, request, user, subPath) + } else if method == "PUT" { + //上传文件 + this.davService.HandlePut(writer, request, user, subPath) + } else if method == "DELETE" { //删除文件 this.davService.HandleDelete(writer, request, user, subPath) diff --git a/rest/dav_service.go b/rest/dav_service.go index 95757bd..ab4fe88 100644 --- a/rest/dav_service.go +++ b/rest/dav_service.go @@ -226,6 +226,26 @@ func (this *DavService) HandleGet(writer http.ResponseWriter, request *http.Requ } +//上传文件 +func (this *DavService) HandlePut(writer http.ResponseWriter, request *http.Request, user *User, subPath string) { + + fmt.Printf("PUT %s\n", subPath) + + filename := GetFilenameOfPath(subPath) + dirPath := GetDirOfPath(subPath) + + //寻找符合条件的matter. + var matter *Matter + //如果是空或者/就是请求根目录 + if dirPath == "" || dirPath == "/" { + matter = NewRootMatter(user) + } else { + matter = this.matterDao.checkByUserUuidAndPath(user.Uuid, dirPath) + } + + this.matterService.Upload(request.Body, user, matter.Uuid, filename, true, false) + +} //删除文件 func (this *DavService) HandleDelete(writer http.ResponseWriter, request *http.Request, user *User, subPath string) { diff --git a/rest/matter_service.go b/rest/matter_service.go index 6e66c60..0f526b0 100644 --- a/rest/matter_service.go +++ b/rest/matter_service.go @@ -133,7 +133,7 @@ func (this *MatterService) Detail(uuid string) *Matter { //开始上传文件 //上传文件. alien表明文件是否是应用使用的文件。 -func (this *MatterService) Upload(file multipart.File, user *User, puuid string, filename string, privacy bool, alien bool) *Matter { +func (this *MatterService) Upload(file io.Reader, user *User, puuid string, filename string, privacy bool, alien bool) *Matter { //文件名不能太长。 if len(filename) > 200 { @@ -172,7 +172,7 @@ func (this *MatterService) Upload(file multipart.File, user *User, puuid string, fileRelativePath := dirRelativePath + "/" + filename //创建父文件夹 - MakeDirAll(fileAbsolutePath) + MakeDirAll(dirAbsolutePath) //如果文件已经存在了,那么直接覆盖。 exist, err := PathExists(fileAbsolutePath) @@ -194,6 +194,8 @@ func (this *MatterService) Upload(file multipart.File, user *User, puuid string, written, err := io.Copy(distFile, file) this.PanicError(err) + this.logger.Info("上传文件%s大小为%v", filename, HumanFileSize(written)) + //判断用户自身上传大小的限制。 if user.SizeLimit >= 0 { if written > user.SizeLimit { diff --git a/rest/util_path.go b/rest/util_path.go index 8a2ba12..b635cc2 100644 --- a/rest/util_path.go +++ b/rest/util_path.go @@ -106,6 +106,20 @@ func GetDirOfPath(fullPath string) string { return fullPath[:index] } +//获取到一个Path 中的文件名,eg /var/www/xx.log -> xx.log +func GetFilenameOfPath(fullPath string) string { + + index1 := strings.LastIndex(fullPath, "/") + //可能是windows的环境 + index2 := strings.LastIndex(fullPath, "\\") + index := index1 + if index2 > index1 { + index = index2 + } + + return fullPath[index+1:] +} + //尝试删除空文件夹 true表示删掉了一个空文件夹,false表示没有删掉任何东西 func DeleteEmptyDir(dirPath string) bool { dir, err := ioutil.ReadDir(dirPath)