Commit e730125d authored by chenhan wang's avatar chenhan wang
Browse files

fix:rewrite the api list and the corrosponding function

parent d8215c14
......@@ -100,8 +100,8 @@ func SetCookie(c echo.Context, cookie *http.Cookie, sid string, maxAge int, time
/*
* 判断文件大小是否超过阈值(threshold,单位B)
*/
func overflow(info *File, threshold int) bool {
len := len(info.Content)
func overflow(content string, threshold int) bool {
len := len(content)
// 大小比较
if len > threshold {
return true
......@@ -112,7 +112,7 @@ func overflow(info *File, threshold int) bool {
}
// / ========== 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)
cookie, _ := c.Cookie("User")
var sid string
......@@ -121,12 +121,12 @@ func DBupdate(c echo.Context, info *File) (string, string) {
} else {
sid = cookie.Value
}
sid, _ = newAuthenticate(sid, url, info.Url, info.Time, true)
if info.MaxAccess == 0 {
sid, _ = newAuthenticate(sid, url, url, info.Expiration, true)
if info.MaxView == 0 {
//num,_=strconv.ParseUint(GetSetting("maxDefaultAccess"), 10, 64)
info.MaxAccess = 30 // 设置最大默认可访问次数
info.MaxView = 30 // 设置最大默认可访问次数
}
model.Savetext(info.Content, info.MaxAccess, info.Passwd, info.Time, url, info.FileType, info.FileName)
//model.Savetext(info.Content, info.MaxView, info.Passwd, info.Expiration, url, info.Type, info.Name,info.HighLight)
return sid, url
}
......
......@@ -3,7 +3,7 @@ package controller
import (
"backend/app/response"
"backend/model"
"fmt"
"encoding/json"
"net/http"
"time"
......@@ -11,42 +11,98 @@ import (
"github.com/sirupsen/logrus"
)
type File struct {
Content string `json:"content" form:"content" query:"content"`
FileName string `json:"fileName" form:"fileName" query:"fileName"`
FileType string `json:"fileType" form:"fileType" query:"fileType"`
Expiration int `json:"expiration" form:"expiration" query:"expiration"`
User string `json:"user" form:"user" query:"user"`
Passwd string `json:"passwd" form:"passwd" query:"passwd"`
Url string `json:"url" form:"url" query:"url"`
Time time.Time `json:"time" form:"time" query:"time"` // 用户指定的时间期限
MaxAccess uint `json:"maxaccess" form:"maxaccess" query:"maxaccess"` // 文件最大可访问次数
type Upload struct {
Content string `json:"Content" form:"Content" query:"Content"`
Passwd string `json:"Passwd" form:"Passwd" query:"Passwd"`
Name string `json:"Name" form:"Name" query:"Name"`
Type string `json:"Type" form:"Type" query:"Type"`
HighLight bool `json:"HighLight" form:"HighLight" query:"HighLight"`
Expiration time.Time `json:"Expiration" form:"Expiration" query:"Expiration"`
MaxAge int `json:"MaxAge" form:"MaxAge" query:"MaxAge"` // 用户指定的时间期限
MaxView uint `json:"MaxView" form:"MaxView" query:"MaxView"` // 文件最大可访问次数
//Expiry time.Time `json:"expiry"` // 有效期
//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 {
// just test
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传
func RecvFile(c echo.Context) error {
info := new(File)
func TextUp(c echo.Context) error {
info := new(Upload)
if err := c.Bind(info); err != nil {
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.", "")
}
// 更新数据库
_, 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 {
* 返回:一个可供URL访问的链接(string)
* cookie.Value 传sessionId
*/
func SendFile(c echo.Context) error {
info := new(File)
func Down(c echo.Context) error {
info := new(Download)
if err := c.Bind(info); err != nil {
logrus.Println(err)
return err
}
info.Time = timeAssign(info.Time) // 默认时间为当前半小时后
//info.Time = timeAssign(info.Time) // 默认时间为当前半小时后
cookie, _ := c.Cookie("User")
cookieMsg := ""
if cookie == nil {
cookieMsg = "没有cookie,已分配.\n"
// cookieMsg = "没有cookie,已分配.\n"
cookie = new(http.Cookie)
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
switch stat {
case 0:
return response.SendResponse(c, http.StatusForbidden, cookieMsg+"error:密码错误", "") //403
case 1: // 鉴权通过
data := model.Find1(info.Url, "content") // 文件内容
typ := model.Find1(info.Url, "fileType") // 文件类型
name := model.Find1(info.Url, "fileName") // 文件名
return response.SendResponse3(c, http.StatusOK, cookieMsg+"success", name, GetFileContentType(typ), data) // 返回数据
Data := new(Msg)
Data.Content = model.Find1(info.Url, "content") // 文件内容
Data.Type = model.Find1(info.Url, "TextType") // 文件类型
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:
return response.SendResponse(c, http.StatusGone, cookieMsg+"error:内容过期", "") //410
case 3:
......@@ -91,17 +150,3 @@ func SendFile(c echo.Context) error {
}
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")
}
}
......@@ -20,7 +20,7 @@ func addRoutes() {
* 使用时最好带cookie,没有就用申请api申请。
* 其余参数可以不设置,有效期默认半小时后, 最大可访问次数默认30(config文件)
*/
api.POST("/file/recv", controller.RecvFile) // 接收文件
api.POST("/file/upload", controller.FileUp) // 接收文件
/*
* 前端用于下载文件的接口
......@@ -30,7 +30,9 @@ func addRoutes() {
* 若文件没设密码则不带密码也可访问
* 状态码: 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() {
......
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