diff --git a/rest/dav_service.go b/rest/dav_service.go index 99968f1..dc9cbe4 100644 --- a/rest/dav_service.go +++ b/rest/dav_service.go @@ -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) { @@ -343,6 +442,16 @@ func (this *DavService) HandleDav(writer http.ResponseWriter, request *http.Requ //TODO: 创建文件夹 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" { //列出文件夹或者目录详情 diff --git a/rest/matter_controller.go b/rest/matter_controller.go index 908510f..c38378f 100644 --- a/rest/matter_controller.go +++ b/rest/matter_controller.go @@ -2,7 +2,6 @@ package rest import ( "net/http" - "regexp" "strconv" "strings" ) @@ -334,44 +333,24 @@ func (this *MatterController) DeleteBatch(writer http.ResponseWriter, request *h return this.Success("删除成功!") } + //重命名一个文件或一个文件夹 func (this *MatterController) Rename(writer http.ResponseWriter, request *http.Request) *WebResult { uuid := request.FormValue("uuid") name := request.FormValue("name") - //验证参数。 - if name == "" { - this.PanicBadRequest("name参数必填") - } - if m, _ := regexp.MatchString(`[<>|*?/\\]`, name); m { - this.PanicBadRequest(`名称中不能包含以下特殊符号:< > | * ? / \`) - } - - if len(name) > 200 { - panic("name长度不能超过200") - } + user := this.checkUser(writer, request) //找出该文件或者文件夹 matter := this.matterDao.CheckByUuid(uuid) - user := this.checkUser(writer, request) if user.Role != USER_ROLE_ADMINISTRATOR && matter.UserUuid != user.Uuid { this.PanicUnauthorized("没有权限") } - if name == matter.Name { - this.PanicBadRequest("新名称和旧名称一样,操作失败!") - } - //判断同级文件夹中是否有同名的文件 - count := this.matterDao.CountByUserUuidAndPuuidAndDirAndName(user.Uuid, matter.Puuid, matter.Dir, name) - - if count > 0 { - this.PanicBadRequest("【" + name + "】已经存在了,请使用其他名称。") - } - - this.matterService.Rename(matter, name) + this.matterService.Rename(matter, name, user) return this.Success(matter) } diff --git a/rest/matter_dao.go b/rest/matter_dao.go index b813275..d675630 100644 --- a/rest/matter_dao.go +++ b/rest/matter_dao.go @@ -284,7 +284,7 @@ func (this *MatterDao) SizeBetweenTime(startTime time.Time, endTime time.Time) i } //根据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}} @@ -293,7 +293,7 @@ func (this *MatterDao) checkByUserUuidAndPath(userUuid string, path string) *Mat if db.Error != nil { if db.Error.Error() == DB_ERROR_NOT_FOUND { - this.PanicNotFound("%s 不存在", path) + return nil } else { this.PanicError(db.Error) } @@ -302,6 +302,17 @@ func (this *MatterDao) checkByUserUuidAndPath(userUuid string, path string) *Mat 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() { this.logger.Info("[MatterDao]执行清理:清除数据库中所有Matter记录。删除磁盘中所有Matter文件。") diff --git a/rest/matter_service.go b/rest/matter_service.go index 78ac589..0442c5b 100644 --- a/rest/matter_service.go +++ b/rest/matter_service.go @@ -788,15 +788,39 @@ func (this *MatterService) Move(srcMatter *Matter, destMatter *Matter) { 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) - relativeDirPath := GetDirOfPath(srcMatter.Path) + relativeDirPath := GetDirOfPath(matter.Path) newAbsolutePath := absoluteDirPath + "/" + name //物理文件一口气移动 @@ -804,22 +828,22 @@ func (this *MatterService) Rename(srcMatter *Matter, name string) { this.PanicError(err) //修改数据库中信息 - srcMatter.Name = name - srcMatter.Path = relativeDirPath + "/" + name - srcMatter = this.matterDao.Save(srcMatter) + matter.Name = name + matter.Path = relativeDirPath + "/" + name + matter = this.matterDao.Save(matter) //调整该文件夹下文件的Path. - matters := this.matterDao.List(srcMatter.Uuid, srcMatter.UserUuid, nil) + matters := this.matterDao.List(matter.Uuid, matter.UserUuid, nil) for _, m := range matters { - this.adjustPath(m, srcMatter) + this.adjustPath(m, matter) } } else { //如果源是普通文件 - oldAbsolutePath := srcMatter.AbsolutePath() + oldAbsolutePath := matter.AbsolutePath() absoluteDirPath := GetDirOfPath(oldAbsolutePath) - relativeDirPath := GetDirOfPath(srcMatter.Path) + relativeDirPath := GetDirOfPath(matter.Path) newAbsolutePath := absoluteDirPath + "/" + name //物理文件进行移动 @@ -827,12 +851,12 @@ func (this *MatterService) Rename(srcMatter *Matter, name string) { this.PanicError(err) //删除对应的缓存。 - this.imageCacheDao.DeleteByMatterUuid(srcMatter.Uuid) + this.imageCacheDao.DeleteByMatterUuid(matter.Uuid) //修改数据库中信息 - srcMatter.Name = name - srcMatter.Path = relativeDirPath + "/" + name - srcMatter = this.matterDao.Save(srcMatter) + matter.Name = name + matter.Path = relativeDirPath + "/" + name + matter = this.matterDao.Save(matter) }