Commit 9d8521e6 authored by chenhan wang's avatar chenhan wang
Browse files

增加与数据库的交互,未调试

parent 518215c7
...@@ -5,7 +5,7 @@ import ( ...@@ -5,7 +5,7 @@ import (
"net/http" "net/http"
"pastebin_backend/app/response" "pastebin_backend/app/response"
//"pastebin_backend/model" "pastebin_backend/model"
//"error" //"error"
"time" "time"
...@@ -28,18 +28,17 @@ type File struct { ...@@ -28,18 +28,17 @@ type File struct {
Expiration int `json:"expiration"` Expiration int `json:"expiration"`
User string `json:"user"` User string `json:"user"`
Passwd string `json:"passwd"` Passwd string `json:"passwd"`
SessionId string `json:"sessionId"`
FileLink string `json:"fileLink"`
//Expiry time.Time `json:"expiry"` // 有效期 //Expiry time.Time `json:"expiry"` // 有效期
//Content string `json:"content"` //Content string `json:"content"`
} }
type FileRe struct {
FileName string `json:"fileName"`
FileType string `json:"fileType"`
}
type pass struct { type pass struct {
User string `json:"user"` SessionId string `json:"sessionId"`
Passwd string `json:"passwd"` Passwd string `json:"passwd"`
FileLink string `json:"fileLink"`
} }
type Text struct { type Text struct {
...@@ -58,32 +57,26 @@ func AskPasswd(c echo.Context) error { ...@@ -58,32 +57,26 @@ func AskPasswd(c echo.Context) error {
if err := c.Bind(info); err != nil { if err := c.Bind(info); err != nil {
return err return err
} }
if info.Passwd == "" || info.User == "" { if info.Passwd == "" || info.SessionId == "" {
//return c.String(http.StatusBadRequest, "请输入用户名和密码") //return c.String(http.StatusBadRequest, "请输入用户名和密码")
c.String(http.StatusBadRequest, "请输入用户名和密码") c.String(http.StatusBadRequest, "请输入SessionId和密码")
return echo.ErrBadRequest return echo.ErrBadRequest
} }
// 在数据库中查找,若user存在且密码一样,重置cookie有效期 if model.Createlink(info.SessionId,info.Passwd,info.FileLink)==true{
// user存在但密码不一样,密码错误或用户名已存在 // 成功建立连接
// user不存在,数据库中加记录并生成一个新的cookie SetCookie(c, info)
return c.String(http.StatusOK, "登录成功")
SetCookie(c, info) }
return c.String(http.StatusOK, "cookie设置") // return c.String(http.StatusOK, "登录失败") //
//return nil
} }
// 设置cookie名为user,根据数据库分配uuid // 设置cookie name sid, value link
func SetCookie(c echo.Context, info *pass) error { func SetCookie(c echo.Context, info *pass) error {
/*uuid, err := uuid.NewUUID()
if err != nil {
logrus.Fatal(err)
}
cookie := new(http.Cookie) cookie := new(http.Cookie)
cookie.Name = "user" // 标识为user cookie.Name = info.SessionId // 标识为user
//cookie.Value = string(uuid) // 通过uuid和数据库,确定user是谁 //cookie.Value = string(uuid) // 通过uuid和数据库,确定user是谁
cookie.Value = "tmp" cookie.Value = info.FileLink
cookie.Path = "/" cookie.Path = "/"
// cookie有效期为3600秒 // cookie有效期为3600秒
cookie.MaxAge = 3600 cookie.MaxAge = 3600
...@@ -91,7 +84,7 @@ func SetCookie(c echo.Context, info *pass) error { ...@@ -91,7 +84,7 @@ func SetCookie(c echo.Context, info *pass) error {
// 在数据库中加入 cookie记录 // 在数据库中加入 cookie记录
// 设置cookie // 设置cookie
c.SetCookie(cookie)*/ c.SetCookie(cookie)
return c.String(http.StatusOK, "cookie设置") return c.String(http.StatusOK, "cookie设置")
} }
...@@ -102,7 +95,6 @@ func RecvFile(c echo.Context) error { ...@@ -102,7 +95,6 @@ func RecvFile(c echo.Context) error {
if err != nil { if err != nil {
return err return err
} }
// 打开用户上传的文件 // 打开用户上传的文件
src, err := file.Open() src, err := file.Open()
if err != nil { if err != nil {
...@@ -138,7 +130,6 @@ func RecvFile(c echo.Context) error { ...@@ -138,7 +130,6 @@ func RecvFile(c echo.Context) error {
if fi.Size() > 8*1024*10224 { if fi.Size() > 8*1024*10224 {
c.HTML(http.StatusOK, fmt.Sprintf("<p>文件上传失败: 文件大小超过8MB</p>")) c.HTML(http.StatusOK, fmt.Sprintf("<p>文件上传失败: 文件大小超过8MB</p>"))
} }
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
return err return err
...@@ -151,30 +142,40 @@ func RecvFile(c echo.Context) error { ...@@ -151,30 +142,40 @@ func RecvFile(c echo.Context) error {
} }
// 更新数据库 // 更新数据库
/* dinfo:=model.Users{
dinfo:=model.Users{ Username: info.User,
Username: info.User, Passwd: info.Passwd,
Passwd: info.Passwd, Name:info.FileName,// 无后缀
Name:info.FileName,// 无后缀 Size: uint(fi.Size()),
Size: uint(fi.Size()), Route:filePath,
Route:filePath, //Time1: info.Expiry,
//Time1: info.Expiry, }
}*/ model.Createuser(dinfo) // 数据库一条新纪录
return c.HTML(http.StatusOK, fmt.Sprintf("<p>文件上传成功: %s</p>", file.Filename)) return c.HTML(http.StatusOK, fmt.Sprintf("<p>文件上传成功: %s</p>", file.Filename))
} }
// 根据用户请求的文件名(不带后缀)和文件类型,返回对应文件的链接 // 根据用户请求的文件名(不带后缀)和文件类型,返回对应文件的链接
func SendFile(c echo.Context) error { func SendFile(c echo.Context) error {
// 判断需不需要密码 info := new(File)
info := new(FileRe)
if err := c.Bind(info); err != nil { if err := c.Bind(info); err != nil {
return err return err
} }
// 判断需不需要密码
if info.FileType == "" { if info.FileType == "" {
info.FileType = "txt" info.FileType = "txt"
} }
if model.find(info.FileLink,info.SessionId,)==false{
cookie,err:=c.Cookie(info.SessionId)
if err!=nil{
return err
}
if cookie.Value!=info.FileLink{
return err // you wenti
}
}
filePath := "./files/" + info.FileType + "/" + info.FileName + "." + info.FileType filePath := "./files/" + info.FileType + "/" + info.FileName + "." + info.FileType
src, err := os.Open(filePath) src, err := os.Open(filePath)
if err != nil { if err != nil {
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment