From ead88d05f4c7ef2d2f70451ff51f09a64f746c68 Mon Sep 17 00:00:00 2001 From: zicla Date: Wed, 24 Apr 2019 21:00:32 +0800 Subject: [PATCH] Refine the codes for tank. --- rest/dav_service.go | 6 +- rest/matter_controller.go | 4 +- rest/matter_service.go | 258 ++++++++++++++++++++------------------ 3 files changed, 143 insertions(+), 125 deletions(-) diff --git a/rest/dav_service.go b/rest/dav_service.go index bfa505c..cf4317a 100644 --- a/rest/dav_service.go +++ b/rest/dav_service.go @@ -249,7 +249,7 @@ func (this *DavService) HandlePut(writer http.ResponseWriter, request *http.Requ //如果存在,那么先删除再说。 srcMatter := this.matterDao.findByUserUuidAndPath(user.Uuid, subPath) if srcMatter != nil { - this.matterDao.Delete(srcMatter) + this.matterService.Delete(srcMatter) } this.matterService.Upload(request.Body, user, matter.Uuid, filename, true, false) @@ -270,7 +270,7 @@ func (this *DavService) HandleDelete(writer http.ResponseWriter, request *http.R matter = this.matterDao.checkByUserUuidAndPath(user.Uuid, subPath) } - this.matterDao.Delete(matter) + this.matterService.Delete(matter) } //创建文件夹 @@ -409,7 +409,7 @@ func (this *DavService) prepareMoveCopy( //如果目标matter还存在了。 if overwrite { //要求覆盖。那么删除。 - this.matterDao.Delete(destMatter) + this.matterService.Delete(destMatter) } else { this.PanicBadRequest("%s已经存在,操作失败!", destinationName) } diff --git a/rest/matter_controller.go b/rest/matter_controller.go index 7e9236b..9f1313e 100644 --- a/rest/matter_controller.go +++ b/rest/matter_controller.go @@ -307,7 +307,7 @@ func (this *MatterController) Delete(writer http.ResponseWriter, request *http.R this.PanicUnauthorized("没有权限") } - this.matterDao.Delete(matter) + this.matterService.Delete(matter) return this.Success("删除成功!") } @@ -338,7 +338,7 @@ func (this *MatterController) DeleteBatch(writer http.ResponseWriter, request *h this.PanicUnauthorized("没有权限") } - this.matterDao.Delete(matter) + this.matterService.Delete(matter) } diff --git a/rest/matter_service.go b/rest/matter_service.go index 4804986..4d8e579 100644 --- a/rest/matter_service.go +++ b/rest/matter_service.go @@ -53,6 +53,129 @@ func (this *MatterService) Init() { } +//文件下载。支持分片下载 +func (this *MatterService) DownloadFile( + writer http.ResponseWriter, + request *http.Request, + filePath string, + filename string, + withContentDisposition bool) { + + download.DownloadFile(writer, request, filePath, filename, withContentDisposition) +} + +//删除文件 +func (this *MatterService) Delete(matter *Matter) { + + if matter == nil { + panic(result.BadRequest("matter不能为nil")) + } + + + //操作锁 + this.userService.MatterLock(matter.UserUuid) + defer this.userService.MatterUnlock(matter.UserUuid) + + + + this.matterDao.Delete(matter) + +} + + +//开始上传文件 +//上传文件. alien表明文件是否是应用使用的文件。 +func (this *MatterService) Upload(file io.Reader, user *User, puuid string, filename string, privacy bool, alien bool) *Matter { + + //文件名不能太长。 + if len(filename) > 200 { + panic("文件名不能超过200") + } + + //文件夹路径 + var dirAbsolutePath string + var dirRelativePath string + if puuid == "" { + this.PanicBadRequest("puuid必填") + } else { + + if puuid == MATTER_ROOT { + dirAbsolutePath = GetUserFileRootDir(user.Username) + dirRelativePath = "" + } else { + //验证puuid是否存在 + dirMatter := this.matterDao.CheckByUuidAndUserUuid(puuid, user.Uuid) + + dirAbsolutePath = GetUserFileRootDir(user.Username) + dirMatter.Path + dirRelativePath = dirMatter.Path + + } + } + + //查找文件夹下面是否有同名文件。 + matters := this.matterDao.ListByUserUuidAndPuuidAndDirAndName(user.Uuid, puuid, false, filename) + //如果有同名的文件,那么我们直接覆盖同名文件。 + for _, dbFile := range matters { + this.PanicBadRequest("该目录下%s已经存在了", dbFile.Name) + } + + //获取文件应该存放在的物理路径的绝对路径和相对路径。 + fileAbsolutePath := dirAbsolutePath + "/" + filename + fileRelativePath := dirRelativePath + "/" + filename + + //创建父文件夹 + MakeDirAll(dirAbsolutePath) + + //如果文件已经存在了,那么直接覆盖。 + exist, err := PathExists(fileAbsolutePath) + this.PanicError(err) + if exist { + this.logger.Error("%s已经存在,将其删除", fileAbsolutePath) + removeError := os.Remove(fileAbsolutePath) + this.PanicError(removeError) + } + + distFile, err := os.OpenFile(fileAbsolutePath, os.O_WRONLY|os.O_CREATE, 0777) + this.PanicError(err) + + defer func() { + err := distFile.Close() + this.PanicError(err) + }() + + written, err := io.Copy(distFile, file) + this.PanicError(err) + + this.logger.Info("上传文件%s大小为%v", filename, HumanFileSize(written)) + + //判断用户自身上传大小的限制。 + if user.SizeLimit >= 0 { + if written > user.SizeLimit { + this.PanicBadRequest("文件大小超出限制 " + HumanFileSize(user.SizeLimit) + ">" + HumanFileSize(written)) + } + } + + //将文件信息存入数据库中。 + matter := &Matter{ + Puuid: puuid, + UserUuid: user.Uuid, + Username: user.Username, + Dir: false, + Alien: alien, + Name: filename, + Md5: "", + Size: written, + Privacy: privacy, + Path: fileRelativePath, + } + + matter = this.matterDao.Create(matter) + + return matter +} + + + //根据一个文件夹路径,找到最后一个文件夹的uuid,如果中途出错,返回err. func (this *MatterService) GetDirUuid(user *User, dir string) (puuid string, dirRelativePath string) { @@ -115,25 +238,6 @@ func (this *MatterService) GetDirUuid(user *User, dir string) (puuid string, dir return puuid, parentRelativePath } -//获取某个文件的详情,会把父级依次倒着装进去。如果中途出错,直接抛出异常。 -func (this *MatterService) Detail(uuid string) *Matter { - - matter := this.matterDao.CheckByUuid(uuid) - - //组装file的内容,展示其父组件。 - puuid := matter.Puuid - tmpMatter := matter - for puuid != MATTER_ROOT { - pFile := this.matterDao.CheckByUuid(puuid) - tmpMatter.Parent = pFile - tmpMatter = pFile - puuid = pFile.Puuid - } - - return matter -} - - //在dirMatter中创建文件夹 返回刚刚创建的这个文件夹 func (this *MatterService) CreateDirectory(dirMatter *Matter, name string, user *User) *Matter { @@ -180,10 +284,10 @@ func (this *MatterService) CreateDirectory(dirMatter *Matter, name string, user panic(result.BadRequest("%s 已经存在了,请使用其他名称。", name)) } - parts := strings.Split(dirMatter.Path,"/") - this.logger.Info("%s的层数:%d",dirMatter.Name,len(parts)) + parts := strings.Split(dirMatter.Path, "/") + this.logger.Info("%s的层数:%d", dirMatter.Name, len(parts)) - if len(parts) >= 32{ + if len(parts) >= 32 { panic(result.BadRequest("文件夹最多%d层", MATTER_NAME_MAX_DEPTH)) } @@ -212,94 +316,21 @@ func (this *MatterService) CreateDirectory(dirMatter *Matter, name string, user return matter } -//开始上传文件 -//上传文件. alien表明文件是否是应用使用的文件。 -func (this *MatterService) Upload(file io.Reader, user *User, puuid string, filename string, privacy bool, alien bool) *Matter { - - //文件名不能太长。 - if len(filename) > 200 { - panic("文件名不能超过200") - } - - //文件夹路径 - var dirAbsolutePath string - var dirRelativePath string - if puuid == "" { - this.PanicBadRequest("puuid必填") - } else { - - if puuid == MATTER_ROOT { - dirAbsolutePath = GetUserFileRootDir(user.Username) - dirRelativePath = "" - } else { - //验证puuid是否存在 - dirMatter := this.matterDao.CheckByUuidAndUserUuid(puuid, user.Uuid) - - dirAbsolutePath = GetUserFileRootDir(user.Username) + dirMatter.Path - dirRelativePath = dirMatter.Path - - } - } - - //查找文件夹下面是否有同名文件。 - matters := this.matterDao.ListByUserUuidAndPuuidAndDirAndName(user.Uuid, puuid, false, filename) - //如果有同名的文件,那么我们直接覆盖同名文件。 - for _, dbFile := range matters { - this.PanicBadRequest("该目录下%s已经存在了", dbFile.Name) - } - - //获取文件应该存放在的物理路径的绝对路径和相对路径。 - fileAbsolutePath := dirAbsolutePath + "/" + filename - fileRelativePath := dirRelativePath + "/" + filename - - //创建父文件夹 - MakeDirAll(dirAbsolutePath) - - //如果文件已经存在了,那么直接覆盖。 - exist, err := PathExists(fileAbsolutePath) - this.PanicError(err) - if exist { - this.logger.Error("%s已经存在,将其删除", fileAbsolutePath) - removeError := os.Remove(fileAbsolutePath) - this.PanicError(removeError) - } - - distFile, err := os.OpenFile(fileAbsolutePath, os.O_WRONLY|os.O_CREATE, 0777) - this.PanicError(err) - - defer func() { - err := distFile.Close() - this.PanicError(err) - }() - - written, err := io.Copy(distFile, file) - this.PanicError(err) - - this.logger.Info("上传文件%s大小为%v", filename, HumanFileSize(written)) +//获取某个文件的详情,会把父级依次倒着装进去。如果中途出错,直接抛出异常。 +func (this *MatterService) Detail(uuid string) *Matter { - //判断用户自身上传大小的限制。 - if user.SizeLimit >= 0 { - if written > user.SizeLimit { - this.PanicBadRequest("文件大小超出限制 " + HumanFileSize(user.SizeLimit) + ">" + HumanFileSize(written)) - } - } + matter := this.matterDao.CheckByUuid(uuid) - //将文件信息存入数据库中。 - matter := &Matter{ - Puuid: puuid, - UserUuid: user.Uuid, - Username: user.Username, - Dir: false, - Alien: alien, - Name: filename, - Md5: "", - Size: written, - Privacy: privacy, - Path: fileRelativePath, + //组装file的内容,展示其父组件。 + puuid := matter.Puuid + tmpMatter := matter + for puuid != MATTER_ROOT { + pFile := this.matterDao.CheckByUuid(puuid) + tmpMatter.Parent = pFile + tmpMatter = pFile + puuid = pFile.Puuid } - matter = this.matterDao.Create(matter) - return matter } @@ -363,7 +394,7 @@ func (this *MatterService) Crawl(url string, filename string, user *User, puuid matters := this.matterDao.ListByUserUuidAndPuuidAndDirAndName(user.Uuid, puuid, false, filename) //如果有同名的文件,那么我们直接覆盖同名文件。 for _, dbFile := range matters { - this.matterDao.Delete(dbFile) + this.Delete(dbFile) } //将文件信息存入数据库中。 @@ -385,19 +416,6 @@ func (this *MatterService) Crawl(url string, filename string, user *User, puuid return matter } -//文件下载。具有进度功能。 -//下载功能参考:https://github.com/Masterminds/go-fileserver -func (this *MatterService) DownloadFile( - writer http.ResponseWriter, - request *http.Request, - filePath string, - filename string, - withContentDisposition bool) { - - download.DownloadFile(writer, request, filePath, filename, withContentDisposition) - -} - //调整一个Matter的path值 func (this *MatterService) adjustPath(matter *Matter, parentMatter *Matter) {