Try to add create directory.

This commit is contained in:
zicla
2019-04-22 14:42:01 +08:00
parent f0be23879d
commit 437306c064
4 changed files with 164 additions and 85 deletions

View File

@ -1,7 +1,6 @@
package rest
import (
"fmt"
"net/http"
"regexp"
"strconv"
@ -96,18 +95,6 @@ func (this *MatterController) CreateDirectory(writer http.ResponseWriter, reques
puuid := request.FormValue("puuid")
name := request.FormValue("name")
name = strings.TrimSpace(name)
//验证参数。
if name == "" {
this.PanicBadRequest("name参数必填并且不能全是空格")
}
if len(name) > 200 {
panic("name长度不能超过200")
}
if m, _ := regexp.MatchString(`[<>|*?/\\]`, name); m {
this.PanicBadRequest(`名称中不能包含以下特殊符号:< > | * ? / \`)
}
//管理员可以指定给某个用户创建文件夹。
userUuid := request.FormValue("userUuid")
@ -117,55 +104,7 @@ func (this *MatterController) CreateDirectory(writer http.ResponseWriter, reques
}
user = this.userDao.CheckByUuid(userUuid)
if puuid == "" {
panic("puuid必填")
}
//判断同级文件夹中是否有同名的文件。
count := this.matterDao.CountByUserUuidAndPuuidAndDirAndName(user.Uuid, puuid, true, name)
if count > 0 {
this.PanicBadRequest("【" + name + "】已经存在了,请使用其他名称。")
}
path := fmt.Sprintf("/%s", 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
for tmpMatter != nil {
count++
tmpMatter = tmpMatter.Parent
}
if count >= 32 {
panic("文件夹最多32层")
}
}
//磁盘中创建文件夹。
dirPath := MakeDirAll(GetUserFileRootDir(user.Username) + path)
this.logger.Info("Create Directory: %s", dirPath)
//数据库中创建文件夹。
matter := &Matter{
Puuid: puuid,
UserUuid: user.Uuid,
Username: user.Username,
Dir: true,
Name: name,
Path: path,
}
matter = this.matterDao.Create(matter)
matter := this.matterService.CreateDirectory(puuid, name, user);
return this.Success(matter)
}