Skip to content

Commit

Permalink
Pass all the move tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
zicla committed Mar 11, 2020
1 parent eb89807 commit 4611def
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 48 deletions.
20 changes: 12 additions & 8 deletions code/rest/dav_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,13 +272,13 @@ func (this *DavService) HandleMkcol(writer http.ResponseWriter, request *http.Re
}

//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 {
panic(result.CustomWebResult(result.METHOD_NOT_ALLOWED, fmt.Sprintf("%s already exists", dirPath)))
}

//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 {
panic(result.CustomWebResult(result.METHOD_NOT_ALLOWED, fmt.Sprintf("%s file already exists", dirPath)))
}
Expand Down Expand Up @@ -399,27 +399,31 @@ func (this *DavService) HandleMove(writer http.ResponseWriter, request *http.Req
fmt.Printf("MOVE %s\n", subPath)

srcMatter, destDirMatter, srcDirPath, destinationDirPath, destinationName, overwrite := this.prepareMoveCopy(writer, request, user, subPath)

//move to the new directory
if destinationDirPath == srcDirPath {
//if destination path not change. it means rename.
this.matterService.AtomicRename(request, srcMatter, destinationName, user)
this.matterService.AtomicRename(request, srcMatter, destinationName, overwrite, user)
} else {
this.matterService.AtomicMove(request, srcMatter, destDirMatter, overwrite, user)
}

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
func (this *DavService) HandleCopy(writer http.ResponseWriter, request *http.Request, user *User, subPath string) {

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)

//copy to the new directory
Expand Down
2 changes: 1 addition & 1 deletion code/rest/matter_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ func (this *MatterController) Rename(writer http.ResponseWriter, request *http.R
panic(result.UNAUTHORIZED)
}

this.matterService.AtomicRename(request, matter, name, user)
this.matterService.AtomicRename(request, matter, name, false, user)

return this.Success(matter)
}
Expand Down
36 changes: 6 additions & 30 deletions code/rest/matter_dao.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,34 +110,6 @@ func (this *MatterDao) FindWithRootByPath(path string, user *User) *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 {

var wp = &builder.WherePair{}
Expand Down Expand Up @@ -202,7 +174,7 @@ func (this *MatterDao) CountByUserUuidAndPuuidAndDirAndName(userUuid string, puu
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{}

Expand All @@ -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: "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)

Expand Down
24 changes: 15 additions & 9 deletions code/rest/matter_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,7 @@ func (this *MatterService) createDirectory(request *http.Request, dirMatter *Mat
}

//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 {
return matter
}
Expand Down Expand Up @@ -743,7 +743,9 @@ func (this *MatterService) AtomicCopy(request *http.Request, srcMatter *Matter,
}

//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 {
panic(result.BadRequest("user cannot be nil"))
Expand All @@ -758,12 +760,16 @@ func (this *MatterService) AtomicRename(request *http.Request, matter *Matter, n
panic(result.BadRequestI18n(request, i18n.MatterNameNoChange))
}

//判断同级文件夹中是否有同名的文件
count := this.matterDao.CountByUserUuidAndPuuidAndDirAndName(user.Uuid, matter.Puuid, matter.Dir, name)

if count > 0 {
//check whether the name used by another matter.
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)))
}

panic(result.BadRequestI18n(request, i18n.MatterExist, name))
}

if matter.Dir {
Expand Down Expand Up @@ -859,7 +865,7 @@ func (this *MatterService) mirror(request *http.Request, srcPath string, destDir
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 {
srcDirMatter = this.createDirectory(request, destDirMatter, fileStat.Name(), user)
Expand All @@ -878,7 +884,7 @@ func (this *MatterService) mirror(request *http.Request, srcPath string, destDir
} 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 overwrite {
Expand Down

0 comments on commit 4611def

Please sign in to comment.