|
|
@ -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.
|
|
|
//根据一个文件夹路径,找到最后一个文件夹的uuid,如果中途出错,返回err.
|
|
|
func (this *MatterService) GetDirUuid(user *User, dir string) (puuid string, dirRelativePath string) { |
|
|
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 |
|
|
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中创建文件夹 返回刚刚创建的这个文件夹
|
|
|
//在dirMatter中创建文件夹 返回刚刚创建的这个文件夹
|
|
|
func (this *MatterService) CreateDirectory(dirMatter *Matter, name string, user *User) *Matter { |
|
|
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)) |
|
|
panic(result.BadRequest("%s 已经存在了,请使用其他名称。", name)) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
parts := strings.Split(dirMatter.Path,"/") |
|
|
parts := strings.Split(dirMatter.Path, "/") |
|
|
this.logger.Info("%s的层数:%d",dirMatter.Name,len(parts)) |
|
|
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)) |
|
|
panic(result.BadRequest("文件夹最多%d层", MATTER_NAME_MAX_DEPTH)) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@ -212,94 +316,21 @@ func (this *MatterService) CreateDirectory(dirMatter *Matter, name string, user |
|
|
return matter |
|
|
return matter |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
//开始上传文件
|
|
|
//获取某个文件的详情,会把父级依次倒着装进去。如果中途出错,直接抛出异常。
|
|
|
//上传文件. alien表明文件是否是应用使用的文件。
|
|
|
func (this *MatterService) Detail(uuid string) *Matter { |
|
|
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)) |
|
|
|
|
|
|
|
|
|
|
|
//判断用户自身上传大小的限制。
|
|
|
matter := this.matterDao.CheckByUuid(uuid) |
|
|
if user.SizeLimit >= 0 { |
|
|
|
|
|
if written > user.SizeLimit { |
|
|
|
|
|
this.PanicBadRequest("文件大小超出限制 " + HumanFileSize(user.SizeLimit) + ">" + HumanFileSize(written)) |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
//将文件信息存入数据库中。
|
|
|
//组装file的内容,展示其父组件。
|
|
|
matter := &Matter{ |
|
|
puuid := matter.Puuid |
|
|
Puuid: puuid, |
|
|
tmpMatter := matter |
|
|
UserUuid: user.Uuid, |
|
|
for puuid != MATTER_ROOT { |
|
|
Username: user.Username, |
|
|
pFile := this.matterDao.CheckByUuid(puuid) |
|
|
Dir: false, |
|
|
tmpMatter.Parent = pFile |
|
|
Alien: alien, |
|
|
tmpMatter = pFile |
|
|
Name: filename, |
|
|
puuid = pFile.Puuid |
|
|
Md5: "", |
|
|
|
|
|
Size: written, |
|
|
|
|
|
Privacy: privacy, |
|
|
|
|
|
Path: fileRelativePath, |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
matter = this.matterDao.Create(matter) |
|
|
|
|
|
|
|
|
|
|
|
return 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) |
|
|
matters := this.matterDao.ListByUserUuidAndPuuidAndDirAndName(user.Uuid, puuid, false, filename) |
|
|
//如果有同名的文件,那么我们直接覆盖同名文件。
|
|
|
//如果有同名的文件,那么我们直接覆盖同名文件。
|
|
|
for _, dbFile := range matters { |
|
|
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 |
|
|
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值
|
|
|
//调整一个Matter的path值
|
|
|
func (this *MatterService) adjustPath(matter *Matter, parentMatter *Matter) { |
|
|
func (this *MatterService) adjustPath(matter *Matter, parentMatter *Matter) { |
|
|
|
|
|
|
|
|
|