diff --git a/rest/matter_controller.go b/rest/matter_controller.go index 3777b8d..7c51343 100644 --- a/rest/matter_controller.go +++ b/rest/matter_controller.go @@ -105,6 +105,7 @@ func (this *MatterController) CreateDirectory(writer http.ResponseWriter, reques this.PanicBadRequest(`名称中不能包含以下特殊符号:< > | * ? / \`) } + //管理员可以指定给某个用户创建文件夹。 userUuid := request.FormValue("userUuid") user := this.checkUser(writer, request) if user.Role != USER_ROLE_ADMINISTRATOR { @@ -115,13 +116,18 @@ func (this *MatterController) CreateDirectory(writer http.ResponseWriter, reques if puuid == "" { panic("puuid必填") } - if puuid != "root" { + + path := fmt.Sprintf("/%s/%s", user.Username, name) + if puuid != MATTER_ROOT { //验证目标文件夹存在。 this.matterDao.CheckByUuidAndUserUuid(puuid, user.Uuid) //获取上级的详情 pMatter := this.matterService.Detail(puuid) + //根据父目录拼接处子目录 + path = fmt.Sprintf("%s/%s", pMatter.Path, name) + //文件夹最多只能有32层。 count := 1 tmpMatter := pMatter @@ -132,7 +138,6 @@ func (this *MatterController) CreateDirectory(writer http.ResponseWriter, reques if count >= 32 { panic("文件夹最多32层") } - } //判断同级文件夹中是否有同名的文件。 @@ -142,15 +147,22 @@ func (this *MatterController) CreateDirectory(writer http.ResponseWriter, reques this.PanicBadRequest("【" + name + "】已经存在了,请使用其他名称。") } + //磁盘中创建文件夹。 + dirPath := MakeDirAll(CONFIG.MatterPath + path) + this.logger.Info("Create Directory: %s", dirPath) + + + //数据库中创建文件夹。 matter := &Matter{ Puuid: puuid, UserUuid: user.Uuid, Dir: true, Name: name, + Path: path, } - matter = this.matterDao.Create(matter) + return this.Success(matter) } @@ -260,7 +272,7 @@ func (this *MatterController) Upload(writer http.ResponseWriter, request *http.R if puuid == "" { this.PanicBadRequest("puuid必填") } else { - if puuid != "root" { + if puuid != MATTER_ROOT { //找出上一级的文件夹。 this.matterDao.CheckByUuidAndUserUuid(puuid, userUuid) @@ -316,7 +328,7 @@ func (this *MatterController) Crawl(writer http.ResponseWriter, request *http.Re if puuid == "" { this.PanicBadRequest("puuid必填") } else { - if puuid != "root" { + if puuid != MATTER_ROOT { //找出上一级的文件夹。 this.matterDao.CheckByUuidAndUserUuid(puuid, userUuid) } @@ -502,7 +514,7 @@ func (this *MatterController) Move(writer http.ResponseWriter, request *http.Req if destUuid == "" { this.PanicBadRequest("destUuid参数必填") } else { - if destUuid != "root" { + if destUuid != MATTER_ROOT { destMatter = this.matterService.Detail(destUuid) 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 { panic("文件和目标文件夹的拥有者不是同一人") } diff --git a/rest/matter_dao.go b/rest/matter_dao.go index 8f626c7..07f7fad 100644 --- a/rest/matter_dao.go +++ b/rest/matter_dao.go @@ -1,7 +1,6 @@ package rest import ( - "fmt" "github.com/jinzhu/gorm" "github.com/nu7hatch/gouuid" @@ -240,6 +239,12 @@ func (this *MatterDao) Delete(matter *Matter) { 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) this.PanicError(db.Error) @@ -253,6 +258,9 @@ func (this *MatterDao) Delete(matter *Matter) { this.imageCacheDao.DeleteByMatterUuid(matter.Uuid) filePath := CONFIG.MatterPath + matter.Path + + + //TODO: do it here. //递归找寻文件的上级目录uuid. 因为是/开头的缘故 parts := strings.Split(matter.Path, "/") dirPath := CONFIG.MatterPath + "/" + parts[1] + "/" + parts[2] + "/" + parts[3] @@ -260,13 +268,13 @@ func (this *MatterDao) Delete(matter *Matter) { //删除文件 err := os.Remove(filePath) if err != nil { - this.logger.Error(fmt.Sprintf("删除磁盘上的文件出错 %s", err.Error())) + this.logger.Error("删除磁盘上的文件出错 %s", err.Error()) } //删除这一层文件夹 err = os.Remove(dirPath) if err != nil { - this.logger.Error(fmt.Sprintf("删除磁盘上的文件夹出错 %s", err.Error())) + this.logger.Error("删除磁盘上的文件夹出错 %s", err.Error()) } } diff --git a/rest/matter_model.go b/rest/matter_model.go index 40738b0..435bf54 100644 --- a/rest/matter_model.go +++ b/rest/matter_model.go @@ -1,5 +1,9 @@ package rest +const ( + MATTER_ROOT = "root" +) + /** * 文件。alien表示是否是应用内使用的文件,比如说蓝眼云盘的头像,alien = true 这种文件在上传时不需要指定存放目录,会统一放在同一个文件夹下。 */ diff --git a/rest/matter_service.go b/rest/matter_service.go index a6a9802..83a30c2 100644 --- a/rest/matter_service.go +++ b/rest/matter_service.go @@ -53,7 +53,7 @@ func (this *MatterService) GetDirUuid(userUuid string, dir string) string { } if dir == "/" { - return "root" + return MATTER_ROOT } if dir[len(dir)-1] == '/' { @@ -67,7 +67,7 @@ func (this *MatterService) GetDirUuid(userUuid string, dir string) string { panic("文件夹最多32层。") } - puuid := "root" + puuid := MATTER_ROOT for k, name := range folders { if len(name) > 200 { @@ -105,7 +105,7 @@ func (this *MatterService) Detail(uuid string) *Matter { //组装file的内容,展示其父组件。 puuid := matter.Puuid tmpMatter := matter - for puuid != "root" { + for puuid != MATTER_ROOT { pFile := this.matterDao.CheckByUuid(puuid) tmpMatter.Parent = pFile tmpMatter = pFile diff --git a/rest/util_path.go b/rest/util_path.go index 5fe07c2..b91fd84 100644 --- a/rest/util_path.go +++ b/rest/util_path.go @@ -58,6 +58,7 @@ func MakeDirAll(dirPath string) string { panic("判断文件是否存在时出错!") } if !exists { + //TODO:文件权限需要进一步考虑 err = os.MkdirAll(dirPath, 0777) if err != nil { panic("创建文件夹时出错!") @@ -67,6 +68,24 @@ func MakeDirAll(dirPath string) string { 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 func GetConfPath() string { @@ -115,7 +134,7 @@ func GetUserFilePath(username string, cache bool) (string, string) { datePath := now.Format("2006-01-02") //毫秒时间戳 timestamp := now.UnixNano() / 1e6 - + //如果是缓存文件夹,那么统一放在cache这个文件夹下面 if cache { datePath = fmt.Sprintf("cache/%s", datePath)