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

修改sendfile

parent b772f6d4
File added
......@@ -72,9 +72,9 @@ func AskPasswd(c echo.Context) error {
// 设置cookie name sid, value link
func SetCookie(c echo.Context, info *pass) error {
cookie := new(http.Cookie)
cookie.Name = info.SessionId // 标识为user
cookie.Name = "User"// 标识为user
//cookie.Value = string(uuid) // 通过uuid和数据库,确定user是谁
cookie.Value = info.FileLink
cookie.Value = info.SessionId
cookie.Path = "/"
// cookie有效期为3600秒
cookie.MaxAge = 3600
......@@ -152,20 +152,24 @@ func RecvFile(c echo.Context) error {
return c.HTML(http.StatusOK, fmt.Sprintf("<p>文件上传成功: %s</p>", file.Filename))
}
// 根据用户请求的文件名(不带后缀)和文件类型,返回对应文件的链接
/*
输入:前端提供的文件链接,
返回:一个可供URL访问的链接(string)
*/
func SendFile(c echo.Context) error {
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 !model.Find(info.FileLink, info.SessionId) {
// 没有sessionId(第一次访问)
cookie, err := c.Cookie("User")
if err != nil {
return err
}
......@@ -175,14 +179,21 @@ func SendFile(c echo.Context) error {
}
filePath := "./files/" + info.FileType + "/" + info.FileName + "." + info.FileType
data:=readFile(filePath)
c.JSON(http.StatusOK, info)
return response.SendResponse(c, http.StatusOK, "msg",info.FileType, data)
//return c.HTML(http.StatusOK, fmt.Sprintf("<p>文件上传成功: %s</p>", file.Filename))
//return nil
}
func readFile(filePath string) string{
src, err := os.Open(filePath)
if err != nil {
fmt.Println(err)
return err
}
fi, err := src.Stat()
if err != nil {
return err
fmt.Println(err)
}
defer func() {
......@@ -190,16 +201,18 @@ func SendFile(c echo.Context) error {
}()
data := make([]byte, fi.Size())
count, err := src.Read(data)
//return response.SendResponse(c, http.StatusOK, "", "pong!")
count = count
_, err = src.Read(data)
if err != nil {
fmt.Println(err)
}
// 信息隐去
c.JSON(http.StatusOK, info)
return response.SendResponse(c, http.StatusOK, "msg", string(data))
//return c.HTML(http.StatusOK, fmt.Sprintf("<p>文件上传成功: %s</p>", file.Filename))w
//return nil
//c.JSON(http.StatusOK, info)
return string(data)
}
func RecvText(c echo.Context) error {
text := new(Text)
if err := c.Bind(text); err != nil {
......
......@@ -26,7 +26,7 @@ type logInfo struct {
/*
向文件或 stdout 输出详细的日志,记录用户的 User-Agent、IP 地址、访问时间、访问路径等信息
User-Agent,访问路径,暂且无法实现
User-Agent,访问路径,暂且无法实现
*/
func mylogger(c echo.Context) {
//c.GET("User-Agent")
......
......@@ -9,6 +9,7 @@ import (
type Response struct {
Code int `json:"code"`
Msg string `json:"msg"`
Type string `json:"type"`
Data interface{} `json:"data"`
}
......
pastebin @ ded77073
Subproject commit ded770739475219412b72eb9b8eb377405fb5803
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