Browse Source

Refine the codes for tank.

master
zicla 7 years ago
parent
commit
ead88d05f4
  1. 6
      rest/dav_service.go
  2. 4
      rest/matter_controller.go
  3. 252
      rest/matter_service.go

6
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)
}

4
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)
}

252
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 {
@ -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) {

Loading…
Cancel
Save