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

@ -160,27 +160,6 @@ func (this *DavController) Index(writer http.ResponseWriter, request *http.Reque
//获取请求者
user := this.CheckCurrentUser(writer, request)
method := request.Method
if method == "PROPFIND" {
//列出文件夹或者目录详情
this.davService.HandlePropfind(writer, request, user, subPath)
} else if method == "GET" {
//请求文件详情(下载)
this.davService.HandleGet(writer, request, user, subPath)
} else if method == "PUT" {
//上传文件
this.davService.HandlePut(writer, request, user, subPath)
} else if method == "DELETE" {
//删除文件
this.davService.HandleDelete(writer, request, user, subPath)
} else {
this.PanicBadRequest("该方法还不支持。%s", method)
}
this.davService.HandleDav(writer, request, user, subPath)
}

View File

@ -202,7 +202,7 @@ func (this *DavService) HandlePropfind(writer http.ResponseWriter, request *http
}
//请求文件详情(下载)
func (this *DavService) HandleGet(writer http.ResponseWriter, request *http.Request, user *User, subPath string) {
func (this *DavService) HandleGetHeadPost(writer http.ResponseWriter, request *http.Request, user *User, subPath string) {
fmt.Printf("GET %s\n", subPath)
@ -263,3 +263,95 @@ func (this *DavService) HandleDelete(writer http.ResponseWriter, request *http.R
this.matterDao.Delete(matter)
}
//创建文件夹
func (this *DavService) HandleMkcol(writer http.ResponseWriter, request *http.Request, user *User, subPath string) {
fmt.Printf("MKCOL %s\n", subPath)
thisDirName := GetFilenameOfPath(subPath)
dirPath := GetDirOfPath(subPath)
//寻找符合条件的matter.
var matter *Matter
//如果是空或者/就是请求根目录
if dirPath == "" || dirPath == "/" {
matter = NewRootMatter(user)
} else {
matter = this.matterDao.checkByUserUuidAndPath(user.Uuid, dirPath)
}
this.matterService.Upload(request.Body, user, matter.Uuid, thisDirName, true, false)
}
//跨域请求的OPTIONS询问
func (this *DavService) HandleOptions(w http.ResponseWriter, r *http.Request, user *User, subPath string) {
fmt.Printf("OPTIONS %s\n", subPath)
//寻找符合条件的matter.
var matter *Matter
//如果是空或者/就是请求根目录
if subPath == "" || subPath == "/" {
matter = NewRootMatter(user)
} else {
matter = this.matterDao.checkByUserUuidAndPath(user.Uuid, subPath)
}
allow := "OPTIONS, LOCK, PUT, MKCOL"
if matter.Dir {
allow = "OPTIONS, LOCK, DELETE, PROPPATCH, COPY, MOVE, UNLOCK, PROPFIND"
} else {
allow = "OPTIONS, LOCK, GET, HEAD, POST, DELETE, PROPPATCH, COPY, MOVE, UNLOCK, PROPFIND, PUT"
}
w.Header().Set("Allow", allow)
// http://www.webdav.org/specs/rfc4918.html#dav.compliance.classes
w.Header().Set("DAV", "1, 2")
// http://msdn.microsoft.com/en-au/library/cc250217.aspx
w.Header().Set("MS-Author-Via", "DAV")
}
//处理所有的请求
func (this *DavService) HandleDav(writer http.ResponseWriter, request *http.Request, user *User, subPath string) {
method := request.Method
if method == "OPTIONS" {
//列出文件夹或者目录详情
this.HandleOptions(writer, request, user, subPath)
} else if method == "GET" || method == "HEAD" || method == "POST" {
//请求文件详情(下载)
this.HandleGetHeadPost(writer, request, user, subPath)
} else if method == "DELETE" {
//删除文件
this.HandleDelete(writer, request, user, subPath)
} else if method == "PUT" {
//上传文件
this.HandlePut(writer, request, user, subPath)
} else if method == "MKCOL" {
//TODO: 创建文件夹
this.HandleMkcol(writer, request, user, subPath)
} else if method == "PROPFIND" {
//列出文件夹或者目录详情
this.HandlePropfind(writer, request, user, subPath)
} else {
this.PanicBadRequest("该方法还不支持。%s", method)
}
}

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)
}

View File

@ -131,6 +131,75 @@ func (this *MatterService) Detail(uuid string) *Matter {
return matter
}
//创建文件夹 返回刚刚创建的这个文件夹
func (this *MatterService) CreateDirectory(puuid string, name string,user *User) *Matter {
name = strings.TrimSpace(name)
//验证参数。
if name == "" {
this.PanicBadRequest("name参数必填并且不能全是空格")
}
if len(name) > 200 {
panic("name长度不能超过200")
}
if m, _ := regexp.MatchString(`[<>|*?/\\]`, name); m {
this.PanicBadRequest(`名称中不能包含以下特殊符号:< > | * ? / \`)
}
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.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)
return matter
}
//开始上传文件
//上传文件. alien表明文件是否是应用使用的文件。
func (this *MatterService) Upload(file io.Reader, user *User, puuid string, filename string, privacy bool, alien bool) *Matter {