Pass all the move tests.

This commit is contained in:
zicla
2020-03-12 01:57:08 +08:00
parent eb89807cf1
commit 4611defe36
4 changed files with 34 additions and 48 deletions

View File

@ -272,13 +272,13 @@ func (this *DavService) HandleMkcol(writer http.ResponseWriter, request *http.Re
} }
//check whether col exists. (RFC2518:8.3.1) //check whether col exists. (RFC2518:8.3.1)
dbMatter := this.matterDao.FindByUserUuidAndPuuidAndDirAndName(user.Uuid, dirMatter.Uuid, true, thisDirName) dbMatter := this.matterDao.FindByUserUuidAndPuuidAndDirAndName(user.Uuid, dirMatter.Uuid, TRUE, thisDirName)
if dbMatter != nil { if dbMatter != nil {
panic(result.CustomWebResult(result.METHOD_NOT_ALLOWED, fmt.Sprintf("%s already exists", dirPath))) panic(result.CustomWebResult(result.METHOD_NOT_ALLOWED, fmt.Sprintf("%s already exists", dirPath)))
} }
//check whether file exists. (RFC2518:8.3.1) //check whether file exists. (RFC2518:8.3.1)
fileMatter := this.matterDao.FindByUserUuidAndPuuidAndDirAndName(user.Uuid, dirMatter.Uuid, false, thisDirName) fileMatter := this.matterDao.FindByUserUuidAndPuuidAndDirAndName(user.Uuid, dirMatter.Uuid, FALSE, thisDirName)
if fileMatter != nil { if fileMatter != nil {
panic(result.CustomWebResult(result.METHOD_NOT_ALLOWED, fmt.Sprintf("%s file already exists", dirPath))) panic(result.CustomWebResult(result.METHOD_NOT_ALLOWED, fmt.Sprintf("%s file already exists", dirPath)))
} }
@ -399,15 +399,24 @@ func (this *DavService) HandleMove(writer http.ResponseWriter, request *http.Req
fmt.Printf("MOVE %s\n", subPath) fmt.Printf("MOVE %s\n", subPath)
srcMatter, destDirMatter, srcDirPath, destinationDirPath, destinationName, overwrite := this.prepareMoveCopy(writer, request, user, subPath) srcMatter, destDirMatter, srcDirPath, destinationDirPath, destinationName, overwrite := this.prepareMoveCopy(writer, request, user, subPath)
//move to the new directory //move to the new directory
if destinationDirPath == srcDirPath { if destinationDirPath == srcDirPath {
//if destination path not change. it means rename. //if destination path not change. it means rename.
this.matterService.AtomicRename(request, srcMatter, destinationName, user) this.matterService.AtomicRename(request, srcMatter, destinationName, overwrite, user)
} else { } else {
this.matterService.AtomicMove(request, srcMatter, destDirMatter, overwrite, user) this.matterService.AtomicMove(request, srcMatter, destDirMatter, overwrite, user)
} }
this.logger.Info("finish moving %s => %s", subPath, destDirMatter.Path) this.logger.Info("finish moving %s => %s", subPath, destDirMatter.Path)
if overwrite {
//overwrite old. set the status code 204
writer.WriteHeader(http.StatusNoContent)
} else {
//copy new. set the status code 201
writer.WriteHeader(http.StatusCreated)
}
} }
//copy file/directory //copy file/directory
@ -415,11 +424,6 @@ func (this *DavService) HandleCopy(writer http.ResponseWriter, request *http.Req
fmt.Printf("COPY %s\n", subPath) fmt.Printf("COPY %s\n", subPath)
//debug point
if request.Header.Get("X-Litmus") == "copymove: 7 (copy_coll)" {
fmt.Println("stop here")
}
srcMatter, destDirMatter, _, _, destinationName, overwrite := this.prepareMoveCopy(writer, request, user, subPath) srcMatter, destDirMatter, _, _, destinationName, overwrite := this.prepareMoveCopy(writer, request, user, subPath)
//copy to the new directory //copy to the new directory

View File

@ -342,7 +342,7 @@ func (this *MatterController) Rename(writer http.ResponseWriter, request *http.R
panic(result.UNAUTHORIZED) panic(result.UNAUTHORIZED)
} }
this.matterService.AtomicRename(request, matter, name, user) this.matterService.AtomicRename(request, matter, name, false, user)
return this.Success(matter) return this.Success(matter)
} }

View File

@ -110,34 +110,6 @@ func (this *MatterDao) FindWithRootByPath(path string, user *User) *Matter {
return matter return matter
} }
func (this *MatterDao) FindByUserUuidAndPuuidAndNameAndDirTrue(userUuid string, puuid string, name string) *Matter {
var wp = &builder.WherePair{}
if userUuid != "" {
wp = wp.And(&builder.WherePair{Query: "user_uuid = ?", Args: []interface{}{userUuid}})
}
if puuid != "" {
wp = wp.And(&builder.WherePair{Query: "puuid = ?", Args: []interface{}{puuid}})
}
if name != "" {
wp = wp.And(&builder.WherePair{Query: "name = ?", Args: []interface{}{name}})
}
wp = wp.And(&builder.WherePair{Query: "dir = ?", Args: []interface{}{1}})
var matter = &Matter{}
db := core.CONTEXT.GetDB().Model(&Matter{}).Where(wp.Query, wp.Args...).First(matter)
if db.Error != nil {
return nil
}
return matter
}
func (this *MatterDao) FindByUserUuidAndPuuidAndDirTrue(userUuid string, puuid string) []*Matter { func (this *MatterDao) FindByUserUuidAndPuuidAndDirTrue(userUuid string, puuid string) []*Matter {
var wp = &builder.WherePair{} var wp = &builder.WherePair{}
@ -202,7 +174,7 @@ func (this *MatterDao) CountByUserUuidAndPuuidAndDirAndName(userUuid string, puu
return count return count
} }
func (this *MatterDao) FindByUserUuidAndPuuidAndDirAndName(userUuid string, puuid string, dir bool, name string) *Matter { func (this *MatterDao) FindByUserUuidAndPuuidAndDirAndName(userUuid string, puuid string, dir string, name string) *Matter {
var matter = &Matter{} var matter = &Matter{}
@ -220,7 +192,11 @@ func (this *MatterDao) FindByUserUuidAndPuuidAndDirAndName(userUuid string, puui
wp = wp.And(&builder.WherePair{Query: "name = ?", Args: []interface{}{name}}) wp = wp.And(&builder.WherePair{Query: "name = ?", Args: []interface{}{name}})
} }
wp = wp.And(&builder.WherePair{Query: "dir = ?", Args: []interface{}{dir}}) if dir == TRUE {
wp = wp.And(&builder.WherePair{Query: "dir = ?", Args: []interface{}{true}})
} else if dir == FALSE {
wp = wp.And(&builder.WherePair{Query: "dir = ?", Args: []interface{}{false}})
}
db := core.CONTEXT.GetDB().Where(wp.Query, wp.Args...).First(matter) db := core.CONTEXT.GetDB().Where(wp.Query, wp.Args...).First(matter)

View File

@ -474,7 +474,7 @@ func (this *MatterService) createDirectory(request *http.Request, dirMatter *Mat
} }
//if exist. return. //if exist. return.
matter := this.matterDao.FindByUserUuidAndPuuidAndDirAndName(user.Uuid, dirMatter.Uuid, true, name) matter := this.matterDao.FindByUserUuidAndPuuidAndDirAndName(user.Uuid, dirMatter.Uuid, TRUE, name)
if matter != nil { if matter != nil {
return matter return matter
} }
@ -743,7 +743,9 @@ func (this *MatterService) AtomicCopy(request *http.Request, srcMatter *Matter,
} }
//rename matter to name //rename matter to name
func (this *MatterService) AtomicRename(request *http.Request, matter *Matter, name string, user *User) { func (this *MatterService) AtomicRename(request *http.Request, matter *Matter, name string, overwrite bool, user *User) {
this.logger.Info("Try to rename srcPath = %s to name = %s", matter.Path, name)
if user == nil { if user == nil {
panic(result.BadRequest("user cannot be nil")) panic(result.BadRequest("user cannot be nil"))
@ -758,12 +760,16 @@ func (this *MatterService) AtomicRename(request *http.Request, matter *Matter, n
panic(result.BadRequestI18n(request, i18n.MatterNameNoChange)) panic(result.BadRequestI18n(request, i18n.MatterNameNoChange))
} }
//判断同级文件夹中是否有同名的文件 //check whether the name used by another matter.
count := this.matterDao.CountByUserUuidAndPuuidAndDirAndName(user.Uuid, matter.Puuid, matter.Dir, name) oldMatter := this.matterDao.FindByUserUuidAndPuuidAndDirAndName(user.Uuid, matter.Puuid, "", name)
if oldMatter != nil {
if overwrite {
//delete this one.
this.Delete(request, oldMatter, user)
} else {
panic(result.CustomWebResult(result.PRECONDITION_FAILED, fmt.Sprintf("%s already exists", name)))
}
if count > 0 {
panic(result.BadRequestI18n(request, i18n.MatterExist, name))
} }
if matter.Dir { if matter.Dir {
@ -859,7 +865,7 @@ func (this *MatterService) mirror(request *http.Request, srcPath string, destDir
if fileStat.IsDir() { if fileStat.IsDir() {
//判断当前文件夹下,文件是否已经存在了。 //判断当前文件夹下,文件是否已经存在了。
srcDirMatter := this.matterDao.FindByUserUuidAndPuuidAndDirAndName(user.Uuid, destDirMatter.Uuid, true, fileStat.Name()) srcDirMatter := this.matterDao.FindByUserUuidAndPuuidAndDirAndName(user.Uuid, destDirMatter.Uuid, TRUE, fileStat.Name())
if srcDirMatter == nil { if srcDirMatter == nil {
srcDirMatter = this.createDirectory(request, destDirMatter, fileStat.Name(), user) srcDirMatter = this.createDirectory(request, destDirMatter, fileStat.Name(), user)
@ -878,7 +884,7 @@ func (this *MatterService) mirror(request *http.Request, srcPath string, destDir
} else { } else {
//判断当前文件夹下,文件是否已经存在了。 //判断当前文件夹下,文件是否已经存在了。
matter := this.matterDao.FindByUserUuidAndPuuidAndDirAndName(user.Uuid, destDirMatter.Uuid, false, fileStat.Name()) matter := this.matterDao.FindByUserUuidAndPuuidAndDirAndName(user.Uuid, destDirMatter.Uuid, FALSE, fileStat.Name())
if matter != nil { if matter != nil {
//如果是覆盖,那么删除之前的文件 //如果是覆盖,那么删除之前的文件
if overwrite { if overwrite {