Finish the PUT method of webdav.
This commit is contained in:
@ -50,7 +50,7 @@ type Owner struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func ReadLockInfo(r io.Reader) (li LockInfo, status int, err error) {
|
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 = ixml.NewDecoder(c).Decode(&li); err != nil {
|
||||||
if err == io.EOF {
|
if err == io.EOF {
|
||||||
if c.n == 0 {
|
if c.n == 0 {
|
||||||
@ -73,11 +73,11 @@ func ReadLockInfo(r io.Reader) (li LockInfo, status int, err error) {
|
|||||||
//这是一个带字节计数器的Reader,可以知道总共读取了多少个字节。
|
//这是一个带字节计数器的Reader,可以知道总共读取了多少个字节。
|
||||||
type CountingReader struct {
|
type CountingReader struct {
|
||||||
n int
|
n int
|
||||||
r io.Reader
|
reader io.Reader
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *CountingReader) Read(p []byte) (int, error) {
|
func (c *CountingReader) Read(p []byte) (int, error) {
|
||||||
n, err := c.r.Read(p)
|
n, err := c.reader.Read(p)
|
||||||
c.n += n
|
c.n += n
|
||||||
return n, err
|
return n, err
|
||||||
}
|
}
|
||||||
@ -178,8 +178,8 @@ type Propfind struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//从request中读出需要的属性。比如:getcontentlength 大小 creationdate 创建时间
|
//从request中读出需要的属性。比如:getcontentlength 大小 creationdate 创建时间
|
||||||
func ReadPropfind(r io.Reader) (propfind Propfind, status int, err error) {
|
func ReadPropfind(reader io.Reader) (propfind Propfind, status int, err error) {
|
||||||
c := CountingReader{r: r}
|
c := CountingReader{reader: reader}
|
||||||
if err = ixml.NewDecoder(&c).Decode(&propfind); err != nil {
|
if err = ixml.NewDecoder(&c).Decode(&propfind); err != nil {
|
||||||
if err == io.EOF {
|
if err == io.EOF {
|
||||||
if c.n == 0 {
|
if c.n == 0 {
|
||||||
|
@ -2,7 +2,6 @@ package rest
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"regexp"
|
"regexp"
|
||||||
)
|
)
|
||||||
@ -139,11 +138,7 @@ func (this *DavController) Index(writer http.ResponseWriter, request *http.Reque
|
|||||||
}
|
}
|
||||||
|
|
||||||
fmt.Printf("\n------Body:------\n")
|
fmt.Printf("\n------Body:------\n")
|
||||||
body, err := ioutil.ReadAll(request.Body)
|
//ioutil.ReadAll 不可重复读,第二次读的时候就什么都没有了。
|
||||||
if err != nil {
|
|
||||||
fmt.Println("读取body时出错" + err.Error())
|
|
||||||
}
|
|
||||||
fmt.Println(string(body))
|
|
||||||
|
|
||||||
fmt.Println("------------------")
|
fmt.Println("------------------")
|
||||||
|
|
||||||
@ -152,6 +147,7 @@ func (this *DavController) Index(writer http.ResponseWriter, request *http.Reque
|
|||||||
|
|
||||||
method := request.Method
|
method := request.Method
|
||||||
if method == "PROPFIND" {
|
if method == "PROPFIND" {
|
||||||
|
|
||||||
//列出文件夹或者目录详情
|
//列出文件夹或者目录详情
|
||||||
this.davService.HandlePropfind(writer, request, user, subPath)
|
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)
|
this.davService.HandleGet(writer, request, user, subPath)
|
||||||
|
|
||||||
|
} else if method == "PUT" {
|
||||||
|
//上传文件
|
||||||
|
this.davService.HandlePut(writer, request, user, subPath)
|
||||||
|
|
||||||
} else if method == "DELETE" {
|
} else if method == "DELETE" {
|
||||||
//删除文件
|
//删除文件
|
||||||
this.davService.HandleDelete(writer, request, user, subPath)
|
this.davService.HandleDelete(writer, request, user, subPath)
|
||||||
|
@ -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) {
|
func (this *DavService) HandleDelete(writer http.ResponseWriter, request *http.Request, user *User, subPath string) {
|
||||||
|
@ -133,7 +133,7 @@ func (this *MatterService) Detail(uuid string) *Matter {
|
|||||||
|
|
||||||
//开始上传文件
|
//开始上传文件
|
||||||
//上传文件. alien表明文件是否是应用使用的文件。
|
//上传文件. 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 {
|
if len(filename) > 200 {
|
||||||
@ -172,7 +172,7 @@ func (this *MatterService) Upload(file multipart.File, user *User, puuid string,
|
|||||||
fileRelativePath := dirRelativePath + "/" + filename
|
fileRelativePath := dirRelativePath + "/" + filename
|
||||||
|
|
||||||
//创建父文件夹
|
//创建父文件夹
|
||||||
MakeDirAll(fileAbsolutePath)
|
MakeDirAll(dirAbsolutePath)
|
||||||
|
|
||||||
//如果文件已经存在了,那么直接覆盖。
|
//如果文件已经存在了,那么直接覆盖。
|
||||||
exist, err := PathExists(fileAbsolutePath)
|
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)
|
written, err := io.Copy(distFile, file)
|
||||||
this.PanicError(err)
|
this.PanicError(err)
|
||||||
|
|
||||||
|
this.logger.Info("上传文件%s大小为%v", filename, HumanFileSize(written))
|
||||||
|
|
||||||
//判断用户自身上传大小的限制。
|
//判断用户自身上传大小的限制。
|
||||||
if user.SizeLimit >= 0 {
|
if user.SizeLimit >= 0 {
|
||||||
if written > user.SizeLimit {
|
if written > user.SizeLimit {
|
||||||
|
@ -106,6 +106,20 @@ func GetDirOfPath(fullPath string) string {
|
|||||||
return fullPath[:index]
|
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表示没有删掉任何东西
|
//尝试删除空文件夹 true表示删掉了一个空文件夹,false表示没有删掉任何东西
|
||||||
func DeleteEmptyDir(dirPath string) bool {
|
func DeleteEmptyDir(dirPath string) bool {
|
||||||
dir, err := ioutil.ReadDir(dirPath)
|
dir, err := ioutil.ReadDir(dirPath)
|
||||||
|
Reference in New Issue
Block a user