Finish the method of COPY for webdav.

This commit is contained in:
zicla 2019-04-24 02:02:25 +08:00
parent 35b16ff3e1
commit 8ff928d9d6
3 changed files with 137 additions and 13 deletions

View File

@ -316,10 +316,17 @@ func (this *DavService) HandleOptions(w http.ResponseWriter, r *http.Request, us
} }
//移动或者重命名
func (this *DavService) HandleMove(writer http.ResponseWriter, request *http.Request, user *User, subPath string) {
fmt.Printf("MOVE %s\n", subPath) //移动或者复制的准备工作
func (this *DavService) prepareMoveCopy(
writer http.ResponseWriter,
request *http.Request,
user *User, subPath string) (
srcMatter *Matter,
destDirMatter *Matter,
srcDirPath string,
destinationDirPath string,
destinationName string) {
//解析出目标路径。 //解析出目标路径。
destinationStr := request.Header.Get("Destination") destinationStr := request.Header.Get("Destination")
@ -358,9 +365,9 @@ func (this *DavService) HandleMove(writer http.ResponseWriter, request *http.Req
this.PanicBadRequest("目标前缀必须为:%s", WEBDAV_PREFFIX) this.PanicBadRequest("目标前缀必须为:%s", WEBDAV_PREFFIX)
} }
destinationName := GetFilenameOfPath(destinationPath) destinationName = GetFilenameOfPath(destinationPath)
destinationDirPath := GetDirOfPath(destinationPath) destinationDirPath = GetDirOfPath(destinationPath)
srcDirPath := GetDirOfPath(subPath) srcDirPath = GetDirOfPath(subPath)
overwrite := false overwrite := false
if overwriteStr == "T" { if overwriteStr == "T" {
@ -373,7 +380,6 @@ func (this *DavService) HandleMove(writer http.ResponseWriter, request *http.Req
} }
//源matter. //源matter.
var srcMatter *Matter
//如果是空或者/就是请求根目录 //如果是空或者/就是请求根目录
if subPath == "" || subPath == "/" { if subPath == "" || subPath == "/" {
this.PanicBadRequest("你不能移动根目录!") this.PanicBadRequest("你不能移动根目录!")
@ -385,13 +391,13 @@ func (this *DavService) HandleMove(writer http.ResponseWriter, request *http.Req
destMatter := this.matterDao.findByUserUuidAndPath(user.Uuid, destinationPath) destMatter := this.matterDao.findByUserUuidAndPath(user.Uuid, destinationPath)
//目标文件夹matter //目标文件夹matter
var destDirMatter *Matter
if destinationDirPath == "" || destinationDirPath == "/" { if destinationDirPath == "" || destinationDirPath == "/" {
destDirMatter = NewRootMatter(user) destDirMatter = NewRootMatter(user)
} else { } else {
destDirMatter = this.matterDao.checkByUserUuidAndPath(user.Uuid, destinationDirPath) destDirMatter = this.matterDao.checkByUserUuidAndPath(user.Uuid, destinationDirPath)
} }
//如果目标matter存在了。 //如果目标matter存在了。
if destMatter != nil { if destMatter != nil {
@ -404,6 +410,16 @@ func (this *DavService) HandleMove(writer http.ResponseWriter, request *http.Req
} }
} }
return srcMatter, destDirMatter, srcDirPath, destinationDirPath, destinationName
}
//移动或者重命名
func (this *DavService) HandleMove(writer http.ResponseWriter, request *http.Request, user *User, subPath string) {
fmt.Printf("MOVE %s\n", subPath)
srcMatter, destDirMatter, srcDirPath, destinationDirPath, destinationName := this.prepareMoveCopy(writer, request, user, subPath)
//移动到新目录中去。 //移动到新目录中去。
if destinationDirPath == srcDirPath { if destinationDirPath == srcDirPath {
//文件夹没变化,相当于重命名。 //文件夹没变化,相当于重命名。
@ -416,8 +432,17 @@ func (this *DavService) HandleMove(writer http.ResponseWriter, request *http.Req
} }
//复制文件/文件夹 //复制文件/文件夹
func (this *DavService) HandleCopy(w http.ResponseWriter, r *http.Request, user *User, subPath string) { func (this *DavService) HandleCopy(writer http.ResponseWriter, request *http.Request, user *User, subPath string) {
this.PanicBadRequest("暂不支持COPY方法")
fmt.Printf("COPY %s\n", subPath)
srcMatter, destDirMatter, _, destinationDirPath, destinationName := this.prepareMoveCopy(writer, request, user, subPath)
//复制到新目录中去。
this.matterService.Copy(srcMatter, destDirMatter, destinationName)
this.logger.Info("完成复制 %s => %s", subPath, destinationDirPath)
} }
//处理所有的请求 //处理所有的请求

View File

@ -131,9 +131,8 @@ func (this *MatterService) Detail(uuid string) *Matter {
return matter return matter
} }
//创建文件夹 返回刚刚创建的这个文件夹 //创建文件夹 返回刚刚创建的这个文件夹
func (this *MatterService) CreateDirectory(puuid string, name string,user *User) *Matter { func (this *MatterService) CreateDirectory(puuid string, name string, user *User) *Matter {
name = strings.TrimSpace(name) name = strings.TrimSpace(name)
//验证参数。 //验证参数。
@ -788,9 +787,69 @@ func (this *MatterService) Move(srcMatter *Matter, destMatter *Matter) {
return return
} }
//将一个srcMatter复制到另一个destMatter(必须为文件夹)下名字叫做name
func (this *MatterService) Copy(srcMatter *Matter, destDirMatter *Matter, name string) {
if !destDirMatter.Dir {
this.PanicBadRequest("目标必须为文件夹")
}
if srcMatter.Dir {
//如果源是文件夹
//在目标地址创建新文件夹。
newMatter := &Matter{
Puuid: destDirMatter.Uuid,
UserUuid: srcMatter.UserUuid,
Username: srcMatter.Username,
Dir: srcMatter.Dir,
Alien: srcMatter.Alien,
Name: name,
Md5: "",
Size: srcMatter.Size,
Privacy: srcMatter.Privacy,
Path: destDirMatter.Path + "/" + name,
}
newMatter = this.matterDao.Create(newMatter)
//复制子文件或文件夹
matters := this.matterDao.List(srcMatter.Uuid, srcMatter.UserUuid, nil)
for _, m := range matters {
this.Copy(m, newMatter, m.Name)
}
} else {
//如果源是普通文件
destAbsolutePath := destDirMatter.AbsolutePath() + "/" + name
srcAbsolutePath := srcMatter.AbsolutePath()
//物理文件进行复制
CopyFile(srcAbsolutePath, destAbsolutePath)
//创建新文件的数据库信息。
newMatter := &Matter{
Puuid: destDirMatter.Uuid,
UserUuid: srcMatter.UserUuid,
Username: srcMatter.Username,
Dir: srcMatter.Dir,
Alien: srcMatter.Alien,
Name: name,
Md5: "",
Size: srcMatter.Size,
Privacy: srcMatter.Privacy,
Path: destDirMatter.Path + "/" + name,
}
newMatter = this.matterDao.Create(newMatter)
}
}
//将一个matter 重命名为 name //将一个matter 重命名为 name
func (this *MatterService) Rename(matter *Matter, name string,user *User) { func (this *MatterService) Rename(matter *Matter, name string, user *User) {
//验证参数。 //验证参数。
if name == "" { if name == "" {

View File

@ -3,6 +3,7 @@ package rest
import ( import (
"fmt" "fmt"
"go/build" "go/build"
"io"
"io/ioutil" "io/ioutil"
"os" "os"
"os/user" "os/user"
@ -229,3 +230,42 @@ func GetUserCacheRootDir(username string) (rootDirPath string) {
return rootDirPath return rootDirPath
} }
//复制文件
func CopyFile(srcPath string, destPath string) (nBytes int64) {
srcFileStat, err := os.Stat(srcPath)
if err != nil {
panic(err)
}
if !srcFileStat.Mode().IsRegular() {
panic(fmt.Errorf("%s is not a regular file", srcPath))
}
srcFile, err := os.Open(srcPath)
if err != nil {
panic(err)
}
defer func() {
err = srcFile.Close()
if err != nil {
panic(err)
}
}()
destFile, err := os.Create(destPath)
if err != nil {
panic(err)
}
defer func() {
err = destFile.Close()
if err != nil {
panic(err)
}
}()
nBytes, err = io.Copy(destFile, srcFile)
return nBytes
}