Commits (2)
...@@ -2,6 +2,8 @@ package controller ...@@ -2,6 +2,8 @@ package controller
import ( import (
"backend/model" "backend/model"
"bytes"
"encoding/json"
"io" "io"
"math/rand" "math/rand"
"mime/multipart" "mime/multipart"
...@@ -163,7 +165,7 @@ func DBupdate(c echo.Context, info *Upload) (string, string) { ...@@ -163,7 +165,7 @@ func DBupdate(c echo.Context, info *Upload) (string, string) {
//num,_=strconv.ParseUint(GetSetting("maxDefaultAccess"), 10, 64) //num,_=strconv.ParseUint(GetSetting("maxDefaultAccess"), 10, 64)
info.MaxView = uint(Settings.MaxDefaultView) // 设置最大默认可访问次数 info.MaxView = uint(Settings.MaxDefaultView) // 设置最大默认可访问次数
} }
//model.Savetext(info.Content, info.MaxView, info.Passwd, info.Expiration, url, info.Type, info.Name,info.HighLight) model.Savetext(info.Content, info.MaxView, info.Passwd, info.Expiration, url, info.Type, info.Name, info.HighLight)
return sid, url return sid, url
} }
...@@ -236,3 +238,13 @@ func TypeComplement(typ string) string { ...@@ -236,3 +238,13 @@ func TypeComplement(typ string) string {
} }
return typ return typ
} }
func myMarshal(data interface{}) (string, error) {
bf := bytes.NewBuffer([]byte{})
jsonEncoder := json.NewEncoder(bf)
jsonEncoder.SetEscapeHTML(false)
if err := jsonEncoder.Encode(data); err != nil {
return "", err
}
return bf.String(), nil
}
...@@ -3,7 +3,6 @@ package controller ...@@ -3,7 +3,6 @@ package controller
import ( import (
"backend/app/response" "backend/app/response"
"backend/model" "backend/model"
"encoding/json"
"net/http" "net/http"
"time" "time"
...@@ -74,13 +73,13 @@ func FileUp(c echo.Context) error { ...@@ -74,13 +73,13 @@ func FileUp(c echo.Context) error {
// 更新数据库 // 更新数据库
_, data.Url = DBupdate(c, info) // 不分配uid _, data.Url = DBupdate(c, info) // 不分配uid
data.Name = info.Name data.Name = info.Name
dataj, err := json.Marshal(data) dataj, err := myMarshal(data)
if err != nil { if err != nil {
logrus.Println(err) logrus.Println(err)
return err return err
} }
//return response.SendResponse2(c, http.StatusOK, "文件上传成功", info.TextName, GetTextContentType(info.TextType), url) //return response.SendResponse2(c, http.StatusOK, "文件上传成功", info.TextName, GetTextContentType(info.TextType), url)
return response.SendResponse(c, http.StatusOK, "文件上传成功", string(dataj)) return response.SendResponse(c, http.StatusOK, "文件上传成功", dataj)
} }
/* /*
...@@ -104,6 +103,8 @@ func Down(c echo.Context) error { ...@@ -104,6 +103,8 @@ func Down(c echo.Context) error {
SetCookie(c, cookie, IdGen(8), 1800, time.Time{}) SetCookie(c, cookie, IdGen(8), 1800, time.Time{})
} }
c.SetParamNames("url")
c.SetParamValues(info.Url)
// 鉴权 // 鉴权
stat := Autheticate(cookie, info.Url, info.Passwd, time.Now().Add(1800)) // 包含创建链接Createlink stat := Autheticate(cookie, info.Url, info.Passwd, time.Now().Add(1800)) // 包含创建链接Createlink
// response // response
...@@ -113,12 +114,11 @@ func Down(c echo.Context) error { ...@@ -113,12 +114,11 @@ func Down(c echo.Context) error {
return response.SendResponse(c, http.StatusForbidden, cookieMsg+"error:密码错误", "") //403 return response.SendResponse(c, http.StatusForbidden, cookieMsg+"error:密码错误", "") //403
case 1: // 鉴权通过 case 1: // 鉴权通过
Data := new(Msg) Data := new(Msg)
Data.Content = model.Find1(info.Url, "content") // 文件内容 Data.Content = model.Find1(info.Url, "content") // 文件内容
Data.Type = model.Find1(info.Url, "TextType") // 文件类型 Data.Type = GetFileContentType(model.Find1(info.Url, "TextType")) // 文件类型
Data.Type = GetFileContentType(Data.Type)
Data.Name = model.Find1(info.Url, "TextName") Data.Name = model.Find1(info.Url, "TextName")
dataj, _ := json.Marshal(Data) // 文件名 dataj, _ := myMarshal(Data) // 文件名
return response.SendResponse(c, http.StatusOK, cookieMsg+"success", string(dataj)) // 返回数据 return response.SendResponse(c, http.StatusOK, cookieMsg+"success", dataj) // 返回数据
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:
......
...@@ -30,7 +30,7 @@ func addRoutes() { ...@@ -30,7 +30,7 @@ func addRoutes() {
* 若文件没设密码则不带密码也可访问 * 若文件没设密码则不带密码也可访问
* 状态码: 403:密码错误; 410:内容过期; 200:访问成功; 401:请进行身份验证(输密码) * 状态码: 403:密码错误; 410:内容过期; 200:访问成功; 401:请进行身份验证(输密码)
*/ */
api.POST("/file/downlaod", controller.Down) api.POST("/file/download", controller.Down)
api.POST("text/upload", controller.TextUp) api.POST("text/upload", controller.TextUp)
api.POST("text/download", controller.Down) api.POST("text/download", controller.Down)
} }
......