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
e730125d
Commit
e730125d
authored
Feb 13, 2023
by
chenhan wang
Browse files
fix:rewrite the api list and the corrosponding function
parent
d8215c14
Changes
3
Show whitespace changes
Inline
Side-by-side
backend/app/controller/foo.go
View file @
e730125d
...
@@ -100,8 +100,8 @@ func SetCookie(c echo.Context, cookie *http.Cookie, sid string, maxAge int, time
...
@@ -100,8 +100,8 @@ func SetCookie(c echo.Context, cookie *http.Cookie, sid string, maxAge int, time
/*
/*
* 判断文件大小是否超过阈值(threshold,单位B)
* 判断文件大小是否超过阈值(threshold,单位B)
*/
*/
func
overflow
(
info
*
File
,
threshold
int
)
bool
{
func
overflow
(
content
string
,
threshold
int
)
bool
{
len
:=
len
(
info
.
C
ontent
)
len
:=
len
(
c
ontent
)
// 大小比较
// 大小比较
if
len
>
threshold
{
if
len
>
threshold
{
return
true
return
true
...
@@ -112,7 +112,7 @@ func overflow(info *File, threshold int) bool {
...
@@ -112,7 +112,7 @@ func overflow(info *File, threshold int) bool {
}
}
// / ========== DB related==================
// / ========== DB related==================
func
DBupdate
(
c
echo
.
Context
,
info
*
File
)
(
string
,
string
)
{
func
DBupdate
(
c
echo
.
Context
,
info
*
Upload
)
(
string
,
string
)
{
url
:=
"http://pastebin/"
+
IdGen
(
8
)
url
:=
"http://pastebin/"
+
IdGen
(
8
)
cookie
,
_
:=
c
.
Cookie
(
"User"
)
cookie
,
_
:=
c
.
Cookie
(
"User"
)
var
sid
string
var
sid
string
...
@@ -121,12 +121,12 @@ func DBupdate(c echo.Context, info *File) (string, string) {
...
@@ -121,12 +121,12 @@ func DBupdate(c echo.Context, info *File) (string, string) {
}
else
{
}
else
{
sid
=
cookie
.
Value
sid
=
cookie
.
Value
}
}
sid
,
_
=
newAuthenticate
(
sid
,
url
,
info
.
U
rl
,
info
.
Time
,
true
)
sid
,
_
=
newAuthenticate
(
sid
,
url
,
u
rl
,
info
.
Expiration
,
true
)
if
info
.
Max
Access
==
0
{
if
info
.
Max
View
==
0
{
//num,_=strconv.ParseUint(GetSetting("maxDefaultAccess"), 10, 64)
//num,_=strconv.ParseUint(GetSetting("maxDefaultAccess"), 10, 64)
info
.
Max
Access
=
30
// 设置最大默认可访问次数
info
.
Max
View
=
30
// 设置最大默认可访问次数
}
}
model
.
Savetext
(
info
.
Content
,
info
.
Max
Access
,
info
.
Passwd
,
info
.
Time
,
url
,
info
.
File
Type
,
info
.
File
Name
)
//
model.Savetext(info.Content, info.Max
View
, info.Passwd, info.
Expiration
, url, info.Type, info.Name
,info.HighLight
)
return
sid
,
url
return
sid
,
url
}
}
...
...
backend/app/controller/interface.go
View file @
e730125d
...
@@ -3,7 +3,7 @@ package controller
...
@@ -3,7 +3,7 @@ package controller
import
(
import
(
"backend/app/response"
"backend/app/response"
"backend/model"
"backend/model"
"
fmt
"
"
encoding/json
"
"net/http"
"net/http"
"time"
"time"
...
@@ -11,42 +11,98 @@ import (
...
@@ -11,42 +11,98 @@ import (
"github.com/sirupsen/logrus"
"github.com/sirupsen/logrus"
)
)
type
File
struct
{
type
Upload
struct
{
Content
string
`json:"content" form:"content" query:"content"`
Content
string
`json:"Content" form:"Content" query:"Content"`
FileName
string
`json:"fileName" form:"fileName" query:"fileName"`
Passwd
string
`json:"Passwd" form:"Passwd" query:"Passwd"`
FileType
string
`json:"fileType" form:"fileType" query:"fileType"`
Name
string
`json:"Name" form:"Name" query:"Name"`
Expiration
int
`json:"expiration" form:"expiration" query:"expiration"`
Type
string
`json:"Type" form:"Type" query:"Type"`
User
string
`json:"user" form:"user" query:"user"`
HighLight
bool
`json:"HighLight" form:"HighLight" query:"HighLight"`
Passwd
string
`json:"passwd" form:"passwd" query:"passwd"`
Expiration
time
.
Time
`json:"Expiration" form:"Expiration" query:"Expiration"`
Url
string
`json:"url" form:"url" query:"url"`
MaxAge
int
`json:"MaxAge" form:"MaxAge" query:"MaxAge"`
// 用户指定的时间期限
Time
time
.
Time
`json:"time" form:"time" query:"time"`
// 用户指定的时间期限
MaxView
uint
`json:"MaxView" form:"MaxView" query:"MaxView"`
// 文件最大可访问次数
MaxAccess
uint
`json:"maxaccess" form:"maxaccess" query:"maxaccess"`
// 文件最大可访问次数
//Expiry time.Time `json:"expiry"` // 有效期
//Expiry time.Time `json:"expiry"` // 有效期
//Content string `json:"content"`
//Content string `json:"content"`
}
}
type
Download
struct
{
Passwd
string
`json:"Passwd" form:"Passwd" query:"Passwd"`
Url
string
`json:"Url" form:"Url" query:"Url"`
}
type
Msg
struct
{
Name
string
Type
string
Content
string
}
func
Ping
(
c
echo
.
Context
)
error
{
func
Ping
(
c
echo
.
Context
)
error
{
// just test
// just test
return
response
.
SendResponse
(
c
,
http
.
StatusOK
,
""
,
"pong!"
)
return
response
.
SendResponse
(
c
,
http
.
StatusOK
,
""
,
"pong!"
)
}
}
// 接收浏览器发来的文件,把文件储存在.\files\目录下
// 申请一个包含uid的cookie
func
AskUid
(
c
echo
.
Context
)
error
{
nsid
:=
IdGen
(
8
)
info
:=
new
(
Upload
)
cookie
:=
new
(
http
.
Cookie
)
err
:=
SetCookie
(
c
,
cookie
,
nsid
,
info
.
MaxAge
,
info
.
Expiration
)
if
err
!=
nil
{
return
err
}
else
{
return
c
.
HTML
(
http
.
StatusOK
,
"success"
)
}
}
// 成功则返回上传成功,否则报错
// 成功则返回上传成功,否则报错
// sessionId不直接绑定,通过cookie传
// sessionId不直接绑定,通过cookie传
func
RecvFile
(
c
echo
.
Context
)
error
{
func
TextUp
(
c
echo
.
Context
)
error
{
info
:=
new
(
File
)
info
:=
new
(
Upload
)
if
err
:=
c
.
Bind
(
info
);
err
!=
nil
{
if
err
:=
c
.
Bind
(
info
);
err
!=
nil
{
return
err
return
err
}
}
info
.
Time
=
timeAssign
(
info
.
Time
)
// 默认时间
info
.
Expiration
=
timeAssign
(
info
.
Expiration
)
// 默认时间
if
overflow
(
info
,
8
*
1024
*
1024
)
{
if
overflow
(
info
.
Content
,
8
*
1024
*
1024
)
{
return
response
.
SendResponse
(
c
,
http
.
StatusForbidden
,
"error:文件上传失败: 文件大小超过8MB."
,
""
)
return
response
.
SendResponse
(
c
,
http
.
StatusForbidden
,
"error:文件上传失败: 文件大小超过8MB."
,
""
)
}
}
// 更新数据库
// 更新数据库
_
,
url
:=
DBupdate
(
c
,
info
)
// 不分配uid
_
,
url
:=
DBupdate
(
c
,
info
)
// 不分配uid
return
response
.
SendResponse2
(
c
,
http
.
StatusOK
,
fmt
.
Sprintf
(
"文件上传成功: %s"
,
info
.
FileName
),
info
.
FileName
,
GetFileContentType
(
info
.
FileType
),
url
)
//return response.SendResponse2(c, http.StatusOK, "文件上传成功", info.TextName, GetTextContentType(info.TextType), url)
return
response
.
SendResponse
(
c
,
http
.
StatusOK
,
"文件上传成功"
,
url
)
}
func
FileUp
(
c
echo
.
Context
)
error
{
info
:=
new
(
Upload
)
if
err
:=
c
.
Bind
(
info
);
err
!=
nil
{
return
err
}
info
.
Expiration
=
timeAssign
(
info
.
Expiration
)
// 默认时间
file
,
err
:=
c
.
FormFile
(
"file"
)
if
err
!=
nil
{
logrus
.
Println
(
err
)
return
err
}
info
.
Name
=
file
.
Filename
// 打开用户上传的文件
src
,
err
:=
file
.
Open
()
if
err
!=
nil
{
logrus
.
Println
(
err
)
return
err
}
defer
src
.
Close
()
if
overflow
(
info
.
Content
,
8
*
1024
*
1024
)
{
return
response
.
SendResponse
(
c
,
http
.
StatusForbidden
,
"error:文件上传失败: 文件大小超过8MB."
,
""
)
}
// 更新数据库
_
,
url
:=
DBupdate
(
c
,
info
)
// 不分配uid
//return response.SendResponse2(c, http.StatusOK, "文件上传成功", info.TextName, GetTextContentType(info.TextType), url)
return
response
.
SendResponse
(
c
,
http
.
StatusOK
,
"文件上传成功"
,
url
)
}
}
/*
/*
...
@@ -54,34 +110,37 @@ func RecvFile(c echo.Context) error {
...
@@ -54,34 +110,37 @@ func RecvFile(c echo.Context) error {
* 返回:一个可供URL访问的链接(string)
* 返回:一个可供URL访问的链接(string)
* cookie.Value 传sessionId
* cookie.Value 传sessionId
*/
*/
func
SendFile
(
c
echo
.
Context
)
error
{
func
Down
(
c
echo
.
Context
)
error
{
info
:=
new
(
File
)
info
:=
new
(
Download
)
if
err
:=
c
.
Bind
(
info
);
err
!=
nil
{
if
err
:=
c
.
Bind
(
info
);
err
!=
nil
{
logrus
.
Println
(
err
)
logrus
.
Println
(
err
)
return
err
return
err
}
}
info
.
Time
=
timeAssign
(
info
.
Time
)
// 默认时间为当前半小时后
//
info.Time = timeAssign(info.Time) // 默认时间为当前半小时后
cookie
,
_
:=
c
.
Cookie
(
"User"
)
cookie
,
_
:=
c
.
Cookie
(
"User"
)
cookieMsg
:=
""
cookieMsg
:=
""
if
cookie
==
nil
{
if
cookie
==
nil
{
cookieMsg
=
"没有cookie,已分配.
\n
"
//
cookieMsg = "没有cookie,已分配.\n"
cookie
=
new
(
http
.
Cookie
)
cookie
=
new
(
http
.
Cookie
)
SetCookie
(
c
,
cookie
,
IdGen
(
8
),
1800
,
time
.
Time
{})
SetCookie
(
c
,
cookie
,
IdGen
(
8
),
1800
,
time
.
Time
{})
}
}
// 鉴权
// 鉴权
stat
:=
Autheticate
(
cookie
,
info
.
Url
,
info
.
Passwd
,
info
.
Time
)
// 包含创建链接Createlink
stat
:=
Autheticate
(
cookie
,
info
.
Url
,
info
.
Passwd
,
time
.
Now
()
.
Add
(
1800
)
)
// 包含创建链接Createlink
// response
// response
switch
stat
{
switch
stat
{
case
0
:
case
0
:
return
response
.
SendResponse
(
c
,
http
.
StatusForbidden
,
cookieMsg
+
"error:密码错误"
,
""
)
//403
return
response
.
SendResponse
(
c
,
http
.
StatusForbidden
,
cookieMsg
+
"error:密码错误"
,
""
)
//403
case
1
:
// 鉴权通过
case
1
:
// 鉴权通过
data
:=
model
.
Find1
(
info
.
Url
,
"content"
)
// 文件内容
Data
:=
new
(
Msg
)
typ
:=
model
.
Find1
(
info
.
Url
,
"fileType"
)
// 文件类型
Data
.
Content
=
model
.
Find1
(
info
.
Url
,
"content"
)
// 文件内容
name
:=
model
.
Find1
(
info
.
Url
,
"fileName"
)
// 文件名
Data
.
Type
=
model
.
Find1
(
info
.
Url
,
"TextType"
)
// 文件类型
return
response
.
SendResponse3
(
c
,
http
.
StatusOK
,
cookieMsg
+
"success"
,
name
,
GetFileContentType
(
typ
),
data
)
// 返回数据
Data
.
Type
=
GetFileContentType
(
Data
.
Type
)
Data
.
Name
=
model
.
Find1
(
info
.
Url
,
"TextName"
)
data
,
_
:=
json
.
Marshal
(
Data
)
// 文件名
return
response
.
SendResponse
(
c
,
http
.
StatusOK
,
cookieMsg
+
"success"
,
data
)
// 返回数据
case
2
:
case
2
:
return
response
.
SendResponse
(
c
,
http
.
StatusGone
,
cookieMsg
+
"error:内容过期"
,
""
)
//410
return
response
.
SendResponse
(
c
,
http
.
StatusGone
,
cookieMsg
+
"error:内容过期"
,
""
)
//410
case
3
:
case
3
:
...
@@ -91,17 +150,3 @@ func SendFile(c echo.Context) error {
...
@@ -91,17 +150,3 @@ func SendFile(c echo.Context) error {
}
}
return
nil
return
nil
}
}
// 申请一个包含uid的cookie
func
AskUid
(
c
echo
.
Context
)
error
{
nsid
:=
IdGen
(
8
)
info
:=
new
(
File
)
cookie
:=
new
(
http
.
Cookie
)
err
:=
SetCookie
(
c
,
cookie
,
nsid
,
info
.
Expiration
,
info
.
Time
)
if
err
!=
nil
{
return
err
}
else
{
return
c
.
HTML
(
http
.
StatusOK
,
"success"
)
}
}
backend/app/routes.go
View file @
e730125d
...
@@ -20,7 +20,7 @@ func addRoutes() {
...
@@ -20,7 +20,7 @@ func addRoutes() {
* 使用时最好带cookie,没有就用申请api申请。
* 使用时最好带cookie,没有就用申请api申请。
* 其余参数可以不设置,有效期默认半小时后, 最大可访问次数默认30(config文件)
* 其余参数可以不设置,有效期默认半小时后, 最大可访问次数默认30(config文件)
*/
*/
api
.
POST
(
"/file/
recv
"
,
controller
.
Recv
File
)
// 接收文件
api
.
POST
(
"/file/
upload
"
,
controller
.
File
Up
)
// 接收文件
/*
/*
* 前端用于下载文件的接口
* 前端用于下载文件的接口
...
@@ -30,7 +30,9 @@ func addRoutes() {
...
@@ -30,7 +30,9 @@ func addRoutes() {
* 若文件没设密码则不带密码也可访问
* 若文件没设密码则不带密码也可访问
* 状态码: 403:密码错误; 410:内容过期; 200:访问成功; 401:请进行身份验证(输密码)
* 状态码: 403:密码错误; 410:内容过期; 200:访问成功; 401:请进行身份验证(输密码)
*/
*/
api
.
POST
(
"/file/send"
,
controller
.
SendFile
)
api
.
POST
(
"/file/downlaod"
,
controller
.
Down
)
api
.
POST
(
"text/upload"
,
controller
.
TextUp
)
api
.
POST
(
"text/download"
,
controller
.
Down
)
}
}
func
ApiAssign
()
{
func
ApiAssign
()
{
...
...
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