Finish the basic feature of Propfind.

This commit is contained in:
zicla 2019-04-21 02:50:17 +08:00
parent 466e406f74
commit 8461218d63
4 changed files with 72 additions and 13 deletions

View File

@ -11,6 +11,10 @@ const (
//用户身份的cookie字段名
COOKIE_AUTH_KEY = "_ak"
//用户身份的Authorization字段名
AUTHORIZATION_KEY = "Authorization"
//数据库表前缀 tank200表示当前应用版本是tank:2.0.x版数据库结构发生变化必然是中型升级
TABLE_PREFIX = "tank20_"

View File

@ -2,9 +2,9 @@ package rest
import (
"fmt"
"io/ioutil"
"net/http"
"regexp"
"tank/rest/dav"
)
/**
@ -67,6 +67,30 @@ func (this *DavController) Init() {
}
}
//通过BasicAuth的方式授权。
func (this *DavController) CheckCurrentUser(writer http.ResponseWriter, request *http.Request) *User {
username, password, ok := request.BasicAuth()
if !ok {
//要求前端使用Basic的形式授权
writer.Header().Set("WWW-Authenticate", `Basic realm="Restricted"`)
panic(ConstWebResult(CODE_WRAPPER_LOGIN))
}
user := this.userDao.FindByUsername(username)
if user == nil {
this.PanicBadRequest("邮箱或密码错误")
} else {
if !MatchBcrypt(password, user.Password) {
this.PanicBadRequest("邮箱或密码错误")
}
}
return user
}
//注册自己的路由。
func (this *DavController) RegisterRoutes() map[string]func(writer http.ResponseWriter, request *http.Request) {
@ -97,21 +121,45 @@ func (this *DavController) HandleRoutes(writer http.ResponseWriter, request *htt
//完成系统安装
func (this *DavController) Index(writer http.ResponseWriter, request *http.Request, subPath string) {
this.logger.Info("请求访问来了:%s %s", request.RequestURI, subPath)
this.logger.Info("--------- URI: %s SUB_PATH: %s ---------", request.RequestURI, subPath)
//获取请求者
user := this.CheckCurrentUser(writer, request)
method := request.Method
if method == "PROPFIND" {
this.davService.HandlePropfind(writer, request, subPath)
this.davService.HandlePropfind(writer, request, user, subPath)
} else {
handler := &dav.Handler{
FileSystem: dav.Dir("D:/Group/Golang/src/webdav/tmp"),
LockSystem: dav.NewMemLS(),
/*打印所有HEADER以及请求参数*/
fmt.Printf("\n------ 请求: %s ------\n", request.URL)
fmt.Printf("\n------Method------\n")
fmt.Println(request.Method)
fmt.Printf("\n------Header------\n")
for key, value := range request.Header {
fmt.Printf("%s = %s\n", key, value)
}
handler.ServeHTTP(writer, request)
fmt.Printf("\n------请求参数:------\n")
for key, value := range request.Form {
fmt.Printf("%s = %s\n", key, value)
}
fmt.Printf("\n------Body------\n")
body, err := ioutil.ReadAll(request.Body)
if err != nil {
fmt.Println("读取body时出错" + err.Error())
}
fmt.Println(string(body))
fmt.Println("------------------")
this.PanicBadRequest("该方法还不支持。")
}
}

View File

@ -140,16 +140,13 @@ func (this *DavService) Propstats(matter *Matter, propfind dav.Propfind) []dav.P
}
//处理 方法
func (this *DavService) HandlePropfind(writer http.ResponseWriter, request *http.Request, subPath string) {
func (this *DavService) HandlePropfind(writer http.ResponseWriter, request *http.Request, user *User, subPath string) {
fmt.Printf("列出文件/文件夹 %s\n", subPath)
//获取请求的层数。暂不支持 infinity
depth := this.ParseDepth(request)
//获取请求者
user := this.checkUser(writer, request)
//读取请求参数。按照用户的参数请求返回内容。
propfind, _, err := dav.ReadPropfind(request.Body)
this.PanicError(err)
@ -163,7 +160,6 @@ func (this *DavService) HandlePropfind(writer http.ResponseWriter, request *http
matter = this.matterDao.checkByUserUuidAndPath(user.Uuid, subPath)
}
var matters []*Matter
if depth == 0 {
matters = []*Matter{matter}

View File

@ -49,12 +49,23 @@ func (this *UserDao) CheckByUuid(uuid string) *User {
}
// Read
var user *User = &User{}
var user = &User{}
db := CONTEXT.DB.Where(&User{Base: Base{Uuid: uuid}}).First(user)
this.PanicError(db.Error)
return user
}
//按照邮箱查询用户。
func (this *UserDao) FindByUsername(username string) *User {
var user = &User{}
db := CONTEXT.DB.Where(&User{Username: username}).First(user)
if db.Error != nil {
return nil
}
return user
}
//按照邮箱查询用户。
func (this *UserDao) FindByEmail(email string) *User {