Add the MatterLock strategy.
This commit is contained in:
parent
daa5847859
commit
d0fb55318f
@ -282,15 +282,15 @@ func (this *DavService) HandleMkcol(writer http.ResponseWriter, request *http.Re
|
||||
dirPath := GetDirOfPath(subPath)
|
||||
|
||||
//寻找符合条件的matter.
|
||||
var matter *Matter
|
||||
var dirMatter *Matter
|
||||
//如果是空或者/就是请求根目录
|
||||
if dirPath == "" || dirPath == "/" {
|
||||
matter = NewRootMatter(user)
|
||||
dirMatter = NewRootMatter(user)
|
||||
} else {
|
||||
matter = this.matterDao.checkByUserUuidAndPath(user.Uuid, dirPath)
|
||||
dirMatter = this.matterDao.checkByUserUuidAndPath(user.Uuid, dirPath)
|
||||
}
|
||||
|
||||
this.matterService.CreateDirectory(matter.Uuid, thisDirName, user)
|
||||
this.matterService.CreateDirectory(dirMatter, thisDirName, user)
|
||||
|
||||
}
|
||||
|
||||
|
@ -89,25 +89,6 @@ func (this *MatterController) Detail(writer http.ResponseWriter, request *http.R
|
||||
|
||||
}
|
||||
|
||||
//创建一个文件夹。
|
||||
func (this *MatterController) CreateDirectory(writer http.ResponseWriter, request *http.Request) *result.WebResult {
|
||||
|
||||
puuid := request.FormValue("puuid")
|
||||
|
||||
name := request.FormValue("name")
|
||||
|
||||
//管理员可以指定给某个用户创建文件夹。
|
||||
userUuid := request.FormValue("userUuid")
|
||||
user := this.checkUser(writer, request)
|
||||
if user.Role != USER_ROLE_ADMINISTRATOR {
|
||||
userUuid = user.Uuid
|
||||
}
|
||||
user = this.userDao.CheckByUuid(userUuid)
|
||||
|
||||
matter := this.matterService.CreateDirectory(puuid, name, user);
|
||||
return this.Success(matter)
|
||||
}
|
||||
|
||||
//按照分页的方式获取某个文件夹下文件和子文件夹的列表,通常情况下只有一页。
|
||||
func (this *MatterController) Page(writer http.ResponseWriter, request *http.Request) *result.WebResult {
|
||||
|
||||
@ -189,6 +170,36 @@ func (this *MatterController) Page(writer http.ResponseWriter, request *http.Req
|
||||
return this.Success(pager)
|
||||
}
|
||||
|
||||
|
||||
//创建一个文件夹。
|
||||
func (this *MatterController) CreateDirectory(writer http.ResponseWriter, request *http.Request) *result.WebResult {
|
||||
|
||||
puuid := request.FormValue("puuid")
|
||||
name := request.FormValue("name")
|
||||
userUuid := request.FormValue("userUuid")
|
||||
|
||||
|
||||
//管理员可以指定给某个用户创建文件夹。
|
||||
user := this.checkUser(writer, request)
|
||||
if user.Role != USER_ROLE_ADMINISTRATOR {
|
||||
userUuid = user.Uuid
|
||||
}
|
||||
user = this.userDao.CheckByUuid(userUuid)
|
||||
|
||||
//找到父级matter
|
||||
var dirMatter *Matter
|
||||
if puuid == MATTER_ROOT {
|
||||
dirMatter = NewRootMatter(user)
|
||||
} else {
|
||||
dirMatter = this.matterDao.CheckByUuid(puuid)
|
||||
}
|
||||
|
||||
matter := this.matterService.CreateDirectory(dirMatter, name, user);
|
||||
return this.Success(matter)
|
||||
}
|
||||
|
||||
|
||||
|
||||
//上传文件
|
||||
func (this *MatterController) Upload(writer http.ResponseWriter, request *http.Request) *result.WebResult {
|
||||
|
||||
@ -334,7 +345,6 @@ func (this *MatterController) DeleteBatch(writer http.ResponseWriter, request *h
|
||||
return this.Success("删除成功!")
|
||||
}
|
||||
|
||||
|
||||
//重命名一个文件或一个文件夹
|
||||
func (this *MatterController) Rename(writer http.ResponseWriter, request *http.Request) *result.WebResult {
|
||||
|
||||
@ -350,7 +360,6 @@ func (this *MatterController) Rename(writer http.ResponseWriter, request *http.R
|
||||
this.PanicUnauthorized("没有权限")
|
||||
}
|
||||
|
||||
|
||||
this.matterService.Rename(matter, name, user)
|
||||
|
||||
return this.Success(matter)
|
||||
|
@ -3,8 +3,15 @@ package rest
|
||||
import "tank/rest/util"
|
||||
|
||||
const (
|
||||
//根目录的uuid
|
||||
MATTER_ROOT = "root"
|
||||
//cache文件夹名称
|
||||
MATTER_CACHE = "cache"
|
||||
//matter名称最大长度
|
||||
MATTER_NAME_MAX_LENGTH = 200
|
||||
//matter文件夹最大深度
|
||||
MATTER_NAME_MAX_DEPTH = 32
|
||||
|
||||
)
|
||||
|
||||
/**
|
||||
|
@ -8,6 +8,7 @@ import (
|
||||
"regexp"
|
||||
"strings"
|
||||
"tank/rest/download"
|
||||
"tank/rest/result"
|
||||
)
|
||||
|
||||
//@Service
|
||||
@ -132,69 +133,80 @@ func (this *MatterService) Detail(uuid string) *Matter {
|
||||
return matter
|
||||
}
|
||||
|
||||
//创建文件夹 返回刚刚创建的这个文件夹
|
||||
func (this *MatterService) CreateDirectory(puuid string, name string, user *User) *Matter {
|
||||
|
||||
//在dirMatter中创建文件夹 返回刚刚创建的这个文件夹
|
||||
func (this *MatterService) CreateDirectory(dirMatter *Matter, name string, user *User) *Matter {
|
||||
|
||||
this.userService.MatterLock(user.Uuid)
|
||||
defer this.userService.MatterUnlock(user.Uuid)
|
||||
|
||||
//父级matter必须存在
|
||||
if dirMatter == nil {
|
||||
panic(result.BadRequest("dirMatter必须指定"))
|
||||
}
|
||||
|
||||
//必须是文件夹
|
||||
if !dirMatter.Dir {
|
||||
panic(result.BadRequest("dirMatter必须是文件夹"))
|
||||
}
|
||||
|
||||
if dirMatter.UserUuid != user.Uuid {
|
||||
|
||||
panic(result.BadRequest("dirMatter的userUuid和user不一致"))
|
||||
}
|
||||
|
||||
name = strings.TrimSpace(name)
|
||||
//验证参数。
|
||||
if name == "" {
|
||||
this.PanicBadRequest("name参数必填,并且不能全是空格")
|
||||
panic(result.BadRequest("name参数必填,并且不能全是空格"))
|
||||
}
|
||||
if len(name) > 200 {
|
||||
panic("name长度不能超过200")
|
||||
|
||||
if len(name) > MATTER_NAME_MAX_LENGTH {
|
||||
|
||||
panic(result.BadRequest("name长度不能超过%d", MATTER_NAME_MAX_LENGTH))
|
||||
|
||||
}
|
||||
|
||||
if m, _ := regexp.MatchString(`[<>|*?/\\]`, name); m {
|
||||
this.PanicBadRequest(`名称中不能包含以下特殊符号:< > | * ? / \`)
|
||||
|
||||
panic(result.BadRequest(`名称中不能包含以下特殊符号:< > | * ? / \`))
|
||||
}
|
||||
|
||||
if puuid == "" {
|
||||
panic("puuid必填")
|
||||
}
|
||||
|
||||
//判断同级文件夹中是否有同名的文件。
|
||||
count := this.matterDao.CountByUserUuidAndPuuidAndDirAndName(user.Uuid, puuid, true, name)
|
||||
//判断同级文件夹中是否有同名的文件夹
|
||||
count := this.matterDao.CountByUserUuidAndPuuidAndDirAndName(user.Uuid, dirMatter.Uuid, true, name)
|
||||
|
||||
if count > 0 {
|
||||
this.PanicBadRequest("【" + name + "】已经存在了,请使用其他名称。")
|
||||
|
||||
panic(result.BadRequest("%s 已经存在了,请使用其他名称。", name))
|
||||
}
|
||||
|
||||
path := fmt.Sprintf("/%s", name)
|
||||
if puuid != MATTER_ROOT {
|
||||
//验证目标文件夹存在。
|
||||
this.matterDao.CheckByUuidAndUserUuid(puuid, user.Uuid)
|
||||
parts := strings.Split(dirMatter.Path,"/")
|
||||
this.logger.Info("%s的层数:%d",dirMatter.Name,len(parts))
|
||||
|
||||
//获取上级的详情
|
||||
pMatter := this.Detail(puuid)
|
||||
if len(parts) >= 32{
|
||||
panic(result.BadRequest("文件夹最多%d层", MATTER_NAME_MAX_DEPTH))
|
||||
}
|
||||
|
||||
//根据父目录拼接处子目录
|
||||
path = fmt.Sprintf("%s/%s", pMatter.Path, name)
|
||||
//绝对路径
|
||||
absolutePath := GetUserFileRootDir(user.Username) + dirMatter.Path + "/" + name
|
||||
|
||||
//文件夹最多只能有32层。
|
||||
count := 1
|
||||
tmpMatter := pMatter
|
||||
for tmpMatter != nil {
|
||||
count++
|
||||
tmpMatter = tmpMatter.Parent
|
||||
}
|
||||
if count >= 32 {
|
||||
panic("文件夹最多32层")
|
||||
}
|
||||
}
|
||||
//相对路径
|
||||
relativePath := dirMatter.Path + "/" + name
|
||||
|
||||
//磁盘中创建文件夹。
|
||||
dirPath := MakeDirAll(GetUserFileRootDir(user.Username) + path)
|
||||
dirPath := MakeDirAll(absolutePath)
|
||||
this.logger.Info("Create Directory: %s", dirPath)
|
||||
|
||||
//数据库中创建文件夹。
|
||||
matter := &Matter{
|
||||
Puuid: puuid,
|
||||
Puuid: dirMatter.Uuid,
|
||||
UserUuid: user.Uuid,
|
||||
Username: user.Username,
|
||||
Dir: true,
|
||||
Name: name,
|
||||
Path: path,
|
||||
Path: relativePath,
|
||||
}
|
||||
|
||||
matter = this.matterDao.Create(matter)
|
||||
|
||||
return matter
|
||||
|
@ -1,6 +1,9 @@
|
||||
package result
|
||||
|
||||
import "net/http"
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type WebResult struct {
|
||||
Code string `json:"code"`
|
||||
@ -89,6 +92,11 @@ func CustomWebResult(codeWrapper *CodeWrapper, description string) *WebResult {
|
||||
return wr
|
||||
}
|
||||
|
||||
//请求参数有问题
|
||||
func BadRequest(format string, v ...interface{}) *WebResult {
|
||||
return CustomWebResult(CODE_WRAPPER_BAD_REQUEST, fmt.Sprintf(format, v...))
|
||||
}
|
||||
|
||||
//所有的数据库错误情况
|
||||
var (
|
||||
DB_ERROR_DUPLICATE_KEY = "Error 1062: Duplicate entry"
|
||||
|
Loading…
Reference in New Issue
Block a user