Finish the prototype of Move method.

This commit is contained in:
zicla
2019-04-22 21:32:49 +08:00
parent 437306c064
commit 99710eb670
4 changed files with 165 additions and 42 deletions

View File

@ -314,6 +314,105 @@ func (this *DavService) HandleOptions(w http.ResponseWriter, r *http.Request, us
} }
//移动或者重命名
func (this *DavService) HandleMove(w http.ResponseWriter, r *http.Request, user *User, subPath string) {
fmt.Printf("MOVE %s\n", subPath)
//解析出目标路径。
destinationUrl := r.Header.Get("Destination")
if destinationUrl == "" {
this.PanicBadRequest("Header Destination必填")
}
u, err := url.Parse(destinationUrl)
this.PanicError(err)
if u.Host != r.Host {
this.PanicBadRequest("Destination Host不一致")
}
//解析出Overwrite。
overwriteStr := r.Header.Get("Overwrite")
overwrite := false
if overwriteStr == "T" {
overwrite = true
}
destinationPath := u.Path
destinationName := GetFilenameOfPath(destinationPath)
destinationDirPath := GetDirOfPath(destinationPath)
dirPath := GetDirOfPath(subPath)
//如果前后一致,那么相当于没有改变
if destinationPath == subPath {
return
}
//源matter.
var srcMatter *Matter
//如果是空或者/就是请求根目录
if subPath == "" || subPath == "/" {
this.PanicBadRequest("你不能移动根目录!")
} else {
srcMatter = this.matterDao.checkByUserUuidAndPath(user.Uuid, subPath)
}
//目标matter
destMatter := this.matterDao.findByUserUuidAndPath(user.Uuid, destinationPath)
//如果目标matter存在了。
if destMatter != nil {
//如果目标matter还存在了。
if overwrite {
//要求覆盖。那么删除。
this.matterDao.Delete(destMatter)
} else {
this.PanicBadRequest("%s已经存在操作失败", destinationName)
}
}
//移动到新目录中去。
//目标文件夹matter
destDirMatter := this.matterDao.checkByUserUuidAndPath(user.Uuid, destinationDirPath)
this.matterService.Move(srcMatter, destDirMatter)
}
//复制文件/文件夹
func (this *DavService) HandleCopy(w http.ResponseWriter, r *http.Request, user *User, subPath string) {
fmt.Printf("COPY %s\n", subPath)
//解析出目标路径。
destinationUrl := r.Header.Get("Destination")
if destinationUrl == "" {
this.PanicBadRequest("Header Destination必填")
}
u, err := url.Parse(destinationUrl)
this.PanicError(err)
if u.Host != r.Host {
this.PanicBadRequest("Destination Host不一致")
}
//
//destinationPath := u.Path
//
//
////寻找符合条件的matter.
//var matter *Matter
////如果是空或者/就是请求根目录
//if subPath == "" || subPath == "/" {
// matter = NewRootMatter(user)
//} else {
// matter = this.matterDao.checkByUserUuidAndPath(user.Uuid, subPath)
//}
}
//处理所有的请求 //处理所有的请求
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) {
@ -343,6 +442,16 @@ func (this *DavService) HandleDav(writer http.ResponseWriter, request *http.Requ
//TODO: 创建文件夹 //TODO: 创建文件夹
this.HandleMkcol(writer, request, user, subPath) this.HandleMkcol(writer, request, user, subPath)
} else if method == "COPY" {
//复制文件/文件夹
this.HandleCopy(writer, request, user, subPath)
} else if method == "MOVE" {
//复制文件/文件夹
this.HandleMove(writer, request, user, subPath)
} else if method == "PROPFIND" { } else if method == "PROPFIND" {
//列出文件夹或者目录详情 //列出文件夹或者目录详情

View File

@ -2,7 +2,6 @@ package rest
import ( import (
"net/http" "net/http"
"regexp"
"strconv" "strconv"
"strings" "strings"
) )
@ -334,44 +333,24 @@ func (this *MatterController) DeleteBatch(writer http.ResponseWriter, request *h
return this.Success("删除成功!") return this.Success("删除成功!")
} }
//重命名一个文件或一个文件夹 //重命名一个文件或一个文件夹
func (this *MatterController) Rename(writer http.ResponseWriter, request *http.Request) *WebResult { func (this *MatterController) Rename(writer http.ResponseWriter, request *http.Request) *WebResult {
uuid := request.FormValue("uuid") uuid := request.FormValue("uuid")
name := request.FormValue("name") name := request.FormValue("name")
//验证参数。 user := this.checkUser(writer, request)
if name == "" {
this.PanicBadRequest("name参数必填")
}
if m, _ := regexp.MatchString(`[<>|*?/\\]`, name); m {
this.PanicBadRequest(`名称中不能包含以下特殊符号:< > | * ? / \`)
}
if len(name) > 200 {
panic("name长度不能超过200")
}
//找出该文件或者文件夹 //找出该文件或者文件夹
matter := this.matterDao.CheckByUuid(uuid) matter := this.matterDao.CheckByUuid(uuid)
user := this.checkUser(writer, request)
if user.Role != USER_ROLE_ADMINISTRATOR && matter.UserUuid != user.Uuid { if user.Role != USER_ROLE_ADMINISTRATOR && matter.UserUuid != user.Uuid {
this.PanicUnauthorized("没有权限") this.PanicUnauthorized("没有权限")
} }
if name == matter.Name {
this.PanicBadRequest("新名称和旧名称一样,操作失败!")
}
//判断同级文件夹中是否有同名的文件 this.matterService.Rename(matter, name, user)
count := this.matterDao.CountByUserUuidAndPuuidAndDirAndName(user.Uuid, matter.Puuid, matter.Dir, name)
if count > 0 {
this.PanicBadRequest("【" + name + "】已经存在了,请使用其他名称。")
}
this.matterService.Rename(matter, name)
return this.Success(matter) return this.Success(matter)
} }

View File

@ -284,7 +284,7 @@ func (this *MatterDao) SizeBetweenTime(startTime time.Time, endTime time.Time) i
} }
//根据userUuid和path来查找 //根据userUuid和path来查找
func (this *MatterDao) checkByUserUuidAndPath(userUuid string, path string) *Matter { func (this *MatterDao) findByUserUuidAndPath(userUuid string, path string) *Matter {
var wp = &WherePair{Query: "user_uuid = ? AND path = ?", Args: []interface{}{userUuid, path}} var wp = &WherePair{Query: "user_uuid = ? AND path = ?", Args: []interface{}{userUuid, path}}
@ -293,7 +293,7 @@ func (this *MatterDao) checkByUserUuidAndPath(userUuid string, path string) *Mat
if db.Error != nil { if db.Error != nil {
if db.Error.Error() == DB_ERROR_NOT_FOUND { if db.Error.Error() == DB_ERROR_NOT_FOUND {
this.PanicNotFound("%s 不存在", path) return nil
} else { } else {
this.PanicError(db.Error) this.PanicError(db.Error)
} }
@ -302,6 +302,17 @@ func (this *MatterDao) checkByUserUuidAndPath(userUuid string, path string) *Mat
return matter return matter
} }
//根据userUuid和path来查找
func (this *MatterDao) checkByUserUuidAndPath(userUuid string, path string) *Matter {
matter := this.findByUserUuidAndPath(userUuid,path)
if matter == nil {
this.PanicNotFound("%s 不存在", path)
}
return matter
}
//执行清理操作 //执行清理操作
func (this *MatterDao) Cleanup() { func (this *MatterDao) Cleanup() {
this.logger.Info("[MatterDao]执行清理清除数据库中所有Matter记录。删除磁盘中所有Matter文件。") this.logger.Info("[MatterDao]执行清理清除数据库中所有Matter记录。删除磁盘中所有Matter文件。")

View File

@ -788,15 +788,39 @@ func (this *MatterService) Move(srcMatter *Matter, destMatter *Matter) {
return return
} }
//将一个srcMatter 重命名为 name
func (this *MatterService) Rename(srcMatter *Matter, name string) {
if srcMatter.Dir { //将一个matter 重命名为 name
func (this *MatterService) Rename(matter *Matter, name string,user *User) {
//验证参数。
if name == "" {
this.PanicBadRequest("name参数必填")
}
if m, _ := regexp.MatchString(`[<>|*?/\\]`, name); m {
this.PanicBadRequest(`名称中不能包含以下特殊符号:< > | * ? / \`)
}
if len(name) > 200 {
panic("name长度不能超过200")
}
if name == matter.Name {
this.PanicBadRequest("新名称和旧名称一样,操作失败!")
}
//判断同级文件夹中是否有同名的文件
count := this.matterDao.CountByUserUuidAndPuuidAndDirAndName(user.Uuid, matter.Puuid, matter.Dir, name)
if count > 0 {
this.PanicBadRequest("【" + name + "】已经存在了,请使用其他名称。")
}
if matter.Dir {
//如果源是文件夹 //如果源是文件夹
oldAbsolutePath := srcMatter.AbsolutePath() oldAbsolutePath := matter.AbsolutePath()
absoluteDirPath := GetDirOfPath(oldAbsolutePath) absoluteDirPath := GetDirOfPath(oldAbsolutePath)
relativeDirPath := GetDirOfPath(srcMatter.Path) relativeDirPath := GetDirOfPath(matter.Path)
newAbsolutePath := absoluteDirPath + "/" + name newAbsolutePath := absoluteDirPath + "/" + name
//物理文件一口气移动 //物理文件一口气移动
@ -804,22 +828,22 @@ func (this *MatterService) Rename(srcMatter *Matter, name string) {
this.PanicError(err) this.PanicError(err)
//修改数据库中信息 //修改数据库中信息
srcMatter.Name = name matter.Name = name
srcMatter.Path = relativeDirPath + "/" + name matter.Path = relativeDirPath + "/" + name
srcMatter = this.matterDao.Save(srcMatter) matter = this.matterDao.Save(matter)
//调整该文件夹下文件的Path. //调整该文件夹下文件的Path.
matters := this.matterDao.List(srcMatter.Uuid, srcMatter.UserUuid, nil) matters := this.matterDao.List(matter.Uuid, matter.UserUuid, nil)
for _, m := range matters { for _, m := range matters {
this.adjustPath(m, srcMatter) this.adjustPath(m, matter)
} }
} else { } else {
//如果源是普通文件 //如果源是普通文件
oldAbsolutePath := srcMatter.AbsolutePath() oldAbsolutePath := matter.AbsolutePath()
absoluteDirPath := GetDirOfPath(oldAbsolutePath) absoluteDirPath := GetDirOfPath(oldAbsolutePath)
relativeDirPath := GetDirOfPath(srcMatter.Path) relativeDirPath := GetDirOfPath(matter.Path)
newAbsolutePath := absoluteDirPath + "/" + name newAbsolutePath := absoluteDirPath + "/" + name
//物理文件进行移动 //物理文件进行移动
@ -827,12 +851,12 @@ func (this *MatterService) Rename(srcMatter *Matter, name string) {
this.PanicError(err) this.PanicError(err)
//删除对应的缓存。 //删除对应的缓存。
this.imageCacheDao.DeleteByMatterUuid(srcMatter.Uuid) this.imageCacheDao.DeleteByMatterUuid(matter.Uuid)
//修改数据库中信息 //修改数据库中信息
srcMatter.Name = name matter.Name = name
srcMatter.Path = relativeDirPath + "/" + name matter.Path = relativeDirPath + "/" + name
srcMatter = this.matterDao.Save(srcMatter) matter = this.matterDao.Save(matter)
} }