Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Menu
Open sidebar
chenhan wang
pastebin
Commits
9d8521e6
Commit
9d8521e6
authored
Dec 17, 2022
by
chenhan wang
Browse files
增加与数据库的交互,未调试
parent
518215c7
Changes
1
Show whitespace changes
Inline
Side-by-side
backend/app/controller/interface.go
View file @
9d8521e6
...
...
@@ -5,7 +5,7 @@ import (
"net/http"
"pastebin_backend/app/response"
//
"pastebin_backend/model"
"pastebin_backend/model"
//"error"
"time"
...
...
@@ -28,18 +28,17 @@ type File struct {
Expiration
int
`json:"expiration"`
User
string
`json:"user"`
Passwd
string
`json:"passwd"`
SessionId
string
`json:"sessionId"`
FileLink
string
`json:"fileLink"`
//Expiry time.Time `json:"expiry"` // 有效期
//Content string `json:"content"`
}
type
FileRe
struct
{
FileName
string
`json:"fileName"`
FileType
string
`json:"fileType"`
}
type
pass
struct
{
User
string
`json:"
u
se
r
"`
SessionId
string
`json:"se
ssionId
"`
Passwd
string
`json:"passwd"`
FileLink
string
`json:"fileLink"`
}
type
Text
struct
{
...
...
@@ -58,32 +57,26 @@ func AskPasswd(c echo.Context) error {
if
err
:=
c
.
Bind
(
info
);
err
!=
nil
{
return
err
}
if
info
.
Passwd
==
""
||
info
.
User
==
""
{
if
info
.
Passwd
==
""
||
info
.
SessionId
==
""
{
//return c.String(http.StatusBadRequest, "请输入用户名和密码")
c
.
String
(
http
.
StatusBadRequest
,
"请输入
用户名
和密码"
)
c
.
String
(
http
.
StatusBadRequest
,
"请输入
SessionId
和密码"
)
return
echo
.
ErrBadRequest
}
// 在数据库中查找,若user存在且密码一样,重置cookie有效期
// user存在但密码不一样,密码错误或用户名已存在
// user不存在,数据库中加记录并生成一个新的cookie
if
model
.
Createlink
(
info
.
SessionId
,
info
.
Passwd
,
info
.
FileLink
)
==
true
{
// 成功建立连接
SetCookie
(
c
,
info
)
return
c
.
String
(
http
.
StatusOK
,
"cookie设置"
)
//
//return nil
return
c
.
String
(
http
.
StatusOK
,
"登录成功"
)
}
return
c
.
String
(
http
.
StatusOK
,
"登录失败"
)
//
}
// 设置cookie
名为user,根据数据库分配uuid
// 设置cookie
name sid, value link
func
SetCookie
(
c
echo
.
Context
,
info
*
pass
)
error
{
/*uuid, err := uuid.NewUUID()
if err != nil {
logrus.Fatal(err)
}
cookie
:=
new
(
http
.
Cookie
)
cookie.Name =
"user"
// 标识为user
cookie
.
Name
=
info
.
SessionId
// 标识为user
//cookie.Value = string(uuid) // 通过uuid和数据库,确定user是谁
cookie.Value =
"tmp"
cookie
.
Value
=
info
.
FileLink
cookie
.
Path
=
"/"
// cookie有效期为3600秒
cookie
.
MaxAge
=
3600
...
...
@@ -91,7 +84,7 @@ func SetCookie(c echo.Context, info *pass) error {
// 在数据库中加入 cookie记录
// 设置cookie
c.SetCookie(cookie)
*/
c
.
SetCookie
(
cookie
)
return
c
.
String
(
http
.
StatusOK
,
"cookie设置"
)
}
...
...
@@ -102,7 +95,6 @@ func RecvFile(c echo.Context) error {
if
err
!=
nil
{
return
err
}
// 打开用户上传的文件
src
,
err
:=
file
.
Open
()
if
err
!=
nil
{
...
...
@@ -138,7 +130,6 @@ func RecvFile(c echo.Context) error {
if
fi
.
Size
()
>
8
*
1024
*
10224
{
c
.
HTML
(
http
.
StatusOK
,
fmt
.
Sprintf
(
"<p>文件上传失败: 文件大小超过8MB</p>"
))
}
if
err
!=
nil
{
fmt
.
Println
(
err
)
return
err
...
...
@@ -151,7 +142,6 @@ func RecvFile(c echo.Context) error {
}
// 更新数据库
/*
dinfo
:=
model
.
Users
{
Username
:
info
.
User
,
Passwd
:
info
.
Passwd
,
...
...
@@ -159,22 +149,33 @@ func RecvFile(c echo.Context) error {
Size
:
uint
(
fi
.
Size
()),
Route
:
filePath
,
//Time1: info.Expiry,
}
*/
}
model
.
Createuser
(
dinfo
)
// 数据库一条新纪录
return
c
.
HTML
(
http
.
StatusOK
,
fmt
.
Sprintf
(
"<p>文件上传成功: %s</p>"
,
file
.
Filename
))
}
// 根据用户请求的文件名(不带后缀)和文件类型,返回对应文件的链接
func
SendFile
(
c
echo
.
Context
)
error
{
// 判断需不需要密码
info
:=
new
(
FileRe
)
info
:=
new
(
File
)
if
err
:=
c
.
Bind
(
info
);
err
!=
nil
{
return
err
}
// 判断需不需要密码
if
info
.
FileType
==
""
{
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
src
,
err
:=
os
.
Open
(
filePath
)
if
err
!=
nil
{
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment