Refine some codes of make directory.

This commit is contained in:
zicla 2019-03-01 02:25:32 +08:00
parent daa4a3e7b8
commit 431ef6accc
5 changed files with 57 additions and 14 deletions

View File

@ -105,6 +105,7 @@ func (this *MatterController) CreateDirectory(writer http.ResponseWriter, reques
this.PanicBadRequest(`名称中不能包含以下特殊符号:< > | * ? / \`) this.PanicBadRequest(`名称中不能包含以下特殊符号:< > | * ? / \`)
} }
//管理员可以指定给某个用户创建文件夹。
userUuid := request.FormValue("userUuid") userUuid := request.FormValue("userUuid")
user := this.checkUser(writer, request) user := this.checkUser(writer, request)
if user.Role != USER_ROLE_ADMINISTRATOR { if user.Role != USER_ROLE_ADMINISTRATOR {
@ -115,13 +116,18 @@ func (this *MatterController) CreateDirectory(writer http.ResponseWriter, reques
if puuid == "" { if puuid == "" {
panic("puuid必填") panic("puuid必填")
} }
if puuid != "root" {
path := fmt.Sprintf("/%s/%s", user.Username, name)
if puuid != MATTER_ROOT {
//验证目标文件夹存在。 //验证目标文件夹存在。
this.matterDao.CheckByUuidAndUserUuid(puuid, user.Uuid) this.matterDao.CheckByUuidAndUserUuid(puuid, user.Uuid)
//获取上级的详情 //获取上级的详情
pMatter := this.matterService.Detail(puuid) pMatter := this.matterService.Detail(puuid)
//根据父目录拼接处子目录
path = fmt.Sprintf("%s/%s", pMatter.Path, name)
//文件夹最多只能有32层。 //文件夹最多只能有32层。
count := 1 count := 1
tmpMatter := pMatter tmpMatter := pMatter
@ -132,7 +138,6 @@ func (this *MatterController) CreateDirectory(writer http.ResponseWriter, reques
if count >= 32 { if count >= 32 {
panic("文件夹最多32层") panic("文件夹最多32层")
} }
} }
//判断同级文件夹中是否有同名的文件。 //判断同级文件夹中是否有同名的文件。
@ -142,15 +147,22 @@ func (this *MatterController) CreateDirectory(writer http.ResponseWriter, reques
this.PanicBadRequest("【" + name + "】已经存在了,请使用其他名称。") this.PanicBadRequest("【" + name + "】已经存在了,请使用其他名称。")
} }
//磁盘中创建文件夹。
dirPath := MakeDirAll(CONFIG.MatterPath + path)
this.logger.Info("Create Directory: %s", dirPath)
//数据库中创建文件夹。
matter := &Matter{ matter := &Matter{
Puuid: puuid, Puuid: puuid,
UserUuid: user.Uuid, UserUuid: user.Uuid,
Dir: true, Dir: true,
Name: name, Name: name,
Path: path,
} }
matter = this.matterDao.Create(matter) matter = this.matterDao.Create(matter)
return this.Success(matter) return this.Success(matter)
} }
@ -260,7 +272,7 @@ func (this *MatterController) Upload(writer http.ResponseWriter, request *http.R
if puuid == "" { if puuid == "" {
this.PanicBadRequest("puuid必填") this.PanicBadRequest("puuid必填")
} else { } else {
if puuid != "root" { if puuid != MATTER_ROOT {
//找出上一级的文件夹。 //找出上一级的文件夹。
this.matterDao.CheckByUuidAndUserUuid(puuid, userUuid) this.matterDao.CheckByUuidAndUserUuid(puuid, userUuid)
@ -316,7 +328,7 @@ func (this *MatterController) Crawl(writer http.ResponseWriter, request *http.Re
if puuid == "" { if puuid == "" {
this.PanicBadRequest("puuid必填") this.PanicBadRequest("puuid必填")
} else { } else {
if puuid != "root" { if puuid != MATTER_ROOT {
//找出上一级的文件夹。 //找出上一级的文件夹。
this.matterDao.CheckByUuidAndUserUuid(puuid, userUuid) this.matterDao.CheckByUuidAndUserUuid(puuid, userUuid)
} }
@ -502,7 +514,7 @@ func (this *MatterController) Move(writer http.ResponseWriter, request *http.Req
if destUuid == "" { if destUuid == "" {
this.PanicBadRequest("destUuid参数必填") this.PanicBadRequest("destUuid参数必填")
} else { } else {
if destUuid != "root" { if destUuid != MATTER_ROOT {
destMatter = this.matterService.Detail(destUuid) destMatter = this.matterService.Detail(destUuid)
if user.Role != USER_ROLE_ADMINISTRATOR && destMatter.UserUuid != user.Uuid { if user.Role != USER_ROLE_ADMINISTRATOR && destMatter.UserUuid != user.Uuid {
@ -533,7 +545,7 @@ func (this *MatterController) Move(writer http.ResponseWriter, request *http.Req
} }
//判断和目标文件夹是否是同一个主人。 //判断和目标文件夹是否是同一个主人。
if destUuid != "root" { if destUuid != MATTER_ROOT {
if srcMatter.UserUuid != destMatter.UserUuid { if srcMatter.UserUuid != destMatter.UserUuid {
panic("文件和目标文件夹的拥有者不是同一人") panic("文件和目标文件夹的拥有者不是同一人")
} }

View File

@ -1,7 +1,6 @@
package rest package rest
import ( import (
"fmt"
"github.com/jinzhu/gorm" "github.com/jinzhu/gorm"
"github.com/nu7hatch/gouuid" "github.com/nu7hatch/gouuid"
@ -240,6 +239,12 @@ func (this *MatterDao) Delete(matter *Matter) {
this.Delete(f) this.Delete(f)
} }
//从磁盘中删除该文件夹。
removeError := os.Remove(CONFIG.MatterPath + matter.Path)
if removeError != nil {
this.logger.Error("从磁盘上删除文件夹%s出错%s", CONFIG.MatterPath+matter.Path, removeError.Error())
}
//删除文件夹本身 //删除文件夹本身
db := CONTEXT.DB.Delete(&matter) db := CONTEXT.DB.Delete(&matter)
this.PanicError(db.Error) this.PanicError(db.Error)
@ -253,6 +258,9 @@ func (this *MatterDao) Delete(matter *Matter) {
this.imageCacheDao.DeleteByMatterUuid(matter.Uuid) this.imageCacheDao.DeleteByMatterUuid(matter.Uuid)
filePath := CONFIG.MatterPath + matter.Path filePath := CONFIG.MatterPath + matter.Path
//TODO: do it here.
//递归找寻文件的上级目录uuid. 因为是/开头的缘故 //递归找寻文件的上级目录uuid. 因为是/开头的缘故
parts := strings.Split(matter.Path, "/") parts := strings.Split(matter.Path, "/")
dirPath := CONFIG.MatterPath + "/" + parts[1] + "/" + parts[2] + "/" + parts[3] dirPath := CONFIG.MatterPath + "/" + parts[1] + "/" + parts[2] + "/" + parts[3]
@ -260,13 +268,13 @@ func (this *MatterDao) Delete(matter *Matter) {
//删除文件 //删除文件
err := os.Remove(filePath) err := os.Remove(filePath)
if err != nil { if err != nil {
this.logger.Error(fmt.Sprintf("删除磁盘上的文件出错 %s", err.Error())) this.logger.Error("删除磁盘上的文件出错 %s", err.Error())
} }
//删除这一层文件夹 //删除这一层文件夹
err = os.Remove(dirPath) err = os.Remove(dirPath)
if err != nil { if err != nil {
this.logger.Error(fmt.Sprintf("删除磁盘上的文件夹出错 %s", err.Error())) this.logger.Error("删除磁盘上的文件夹出错 %s", err.Error())
} }
} }

View File

@ -1,5 +1,9 @@
package rest package rest
const (
MATTER_ROOT = "root"
)
/** /**
* 文件alien表示是否是应用内使用的文件比如说蓝眼云盘的头像alien = true 这种文件在上传时不需要指定存放目录会统一放在同一个文件夹下 * 文件alien表示是否是应用内使用的文件比如说蓝眼云盘的头像alien = true 这种文件在上传时不需要指定存放目录会统一放在同一个文件夹下
*/ */

View File

@ -53,7 +53,7 @@ func (this *MatterService) GetDirUuid(userUuid string, dir string) string {
} }
if dir == "/" { if dir == "/" {
return "root" return MATTER_ROOT
} }
if dir[len(dir)-1] == '/' { if dir[len(dir)-1] == '/' {
@ -67,7 +67,7 @@ func (this *MatterService) GetDirUuid(userUuid string, dir string) string {
panic("文件夹最多32层。") panic("文件夹最多32层。")
} }
puuid := "root" puuid := MATTER_ROOT
for k, name := range folders { for k, name := range folders {
if len(name) > 200 { if len(name) > 200 {
@ -105,7 +105,7 @@ func (this *MatterService) Detail(uuid string) *Matter {
//组装file的内容展示其父组件。 //组装file的内容展示其父组件。
puuid := matter.Puuid puuid := matter.Puuid
tmpMatter := matter tmpMatter := matter
for puuid != "root" { for puuid != MATTER_ROOT {
pFile := this.matterDao.CheckByUuid(puuid) pFile := this.matterDao.CheckByUuid(puuid)
tmpMatter.Parent = pFile tmpMatter.Parent = pFile
tmpMatter = pFile tmpMatter = pFile

View File

@ -58,6 +58,7 @@ func MakeDirAll(dirPath string) string {
panic("判断文件是否存在时出错!") panic("判断文件是否存在时出错!")
} }
if !exists { if !exists {
//TODO:文件权限需要进一步考虑
err = os.MkdirAll(dirPath, 0777) err = os.MkdirAll(dirPath, 0777)
if err != nil { if err != nil {
panic("创建文件夹时出错!") panic("创建文件夹时出错!")
@ -67,6 +68,24 @@ func MakeDirAll(dirPath string) string {
return dirPath return dirPath
} }
//移除某个文件夹。 例如:/var/www/matter => /var/www
func RemoveDirectory(dirPath string) string {
exists, err := PathExists(dirPath)
if err != nil {
panic("判断文件是否存在时出错!")
}
if exists {
err = os.Remove(dirPath)
if err != nil {
panic("删除文件夹时出错!")
}
}
return dirPath
}
//获取配置文件存放的位置 //获取配置文件存放的位置
//例如C:\Users\lishuang\AppData\Local\Temp/conf //例如C:\Users\lishuang\AppData\Local\Temp/conf
func GetConfPath() string { func GetConfPath() string {
@ -115,7 +134,7 @@ func GetUserFilePath(username string, cache bool) (string, string) {
datePath := now.Format("2006-01-02") datePath := now.Format("2006-01-02")
//毫秒时间戳 //毫秒时间戳
timestamp := now.UnixNano() / 1e6 timestamp := now.UnixNano() / 1e6
//如果是缓存文件夹那么统一放在cache这个文件夹下面 //如果是缓存文件夹那么统一放在cache这个文件夹下面
if cache { if cache {
datePath = fmt.Sprintf("cache/%s", datePath) datePath = fmt.Sprintf("cache/%s", datePath)