Commit 6309d619 authored by chenhan wang's avatar chenhan wang
Browse files

完善了interface.go

parent 7f2a2a31
...@@ -4,14 +4,18 @@ import ( ...@@ -4,14 +4,18 @@ import (
"net" "net"
"net/http" "net/http"
"pastebin_backend/app/response" "pastebin_backend/app/response"
//"pastebin_backend/model" //"pastebin_backend/model"
//"error" //"error"
"time"
"github.com/labstack/echo/v4" "github.com/labstack/echo/v4"
//"time"
//"encoding/json" //"encoding/json"
"fmt" "fmt"
"io" "io"
"math/rand"
"os" "os"
//"github.com/google/uuid" //"github.com/google/uuid"
//"github.com/go-playground/validator" //"github.com/go-playground/validator"
...@@ -117,9 +121,21 @@ func RecvFile(c echo.Context) error { ...@@ -117,9 +121,21 @@ func RecvFile(c echo.Context) error {
} }
filePath := "./files/" + info.FileType + "/" + file.Filename filePath := "./files/" + info.FileType + "/" + file.Filename
dst, err := os.Create(filePath) dst, err := os.Create(filePath)
if err != nil {
// 是否目录不完整引起的问题
if os.IsExist(err) == false {
if catalogBuild(info.FileType) != nil {
return err
}
dst, err = os.Create(filePath)
if err != nil {
return err
}
}
}
// 获取文件大小 // 获取文件大小
fi,_:=dst.Stat() fi, _ := dst.Stat()
if fi.Size()>8*1024*10224{ if fi.Size() > 8*1024*10224 {
c.HTML(http.StatusOK, fmt.Sprintf("<p>文件上传失败: 文件大小超过8MB</p>")) c.HTML(http.StatusOK, fmt.Sprintf("<p>文件上传失败: 文件大小超过8MB</p>"))
} }
...@@ -134,23 +150,24 @@ func RecvFile(c echo.Context) error { ...@@ -134,23 +150,24 @@ func RecvFile(c echo.Context) error {
return err return err
} }
// 更新数据库 // 更新数据库
/* /*
dinfo:=model.Users{ dinfo:=model.Users{
Username: info.User, Username: info.User,
Passwd: info.Passwd, Passwd: info.Passwd,
Name:info.FileName,// 无后缀 Name:info.FileName,// 无后缀
Size: uint(fi.Size()), Size: uint(fi.Size()),
Route:filePath, Route:filePath,
//Time1: info.Expiry, //Time1: info.Expiry,
}*/ }*/
return c.HTML(http.StatusOK, fmt.Sprintf("<p>文件上传成功: %s</p>", file.Filename)) return c.HTML(http.StatusOK, fmt.Sprintf("<p>文件上传成功: %s</p>", file.Filename))
} }
// 根据用户请求的文件名(不带后缀)和文件类型,返回对应文件的链接 // 根据用户请求的文件名(不带后缀)和文件类型,返回对应文件的链接
func SendFile(c echo.Context) error { func SendFile(c echo.Context) error {
// 判断需不需要密码
info := new(FileRe) info := new(FileRe)
if err := c.Bind(info); err != nil { if err := c.Bind(info); err != nil {
return err return err
...@@ -164,15 +181,15 @@ func SendFile(c echo.Context) error { ...@@ -164,15 +181,15 @@ func SendFile(c echo.Context) error {
fmt.Println(err) fmt.Println(err)
return err return err
} }
fi,err:=src.Stat() fi, err := src.Stat()
if err != nil{ if err != nil {
return err return err
} }
defer func() { defer func() {
src.Close() src.Close()
}() }()
data := make([]byte, fi.Size()) data := make([]byte, fi.Size())
count, err := src.Read(data) count, err := src.Read(data)
//return response.SendResponse(c, http.StatusOK, "", "pong!") //return response.SendResponse(c, http.StatusOK, "", "pong!")
...@@ -205,6 +222,22 @@ func SendText(conn net.Conn, text Text) error { ...@@ -205,6 +222,22 @@ func SendText(conn net.Conn, text Text) error {
} }
// 如果没有对应文件夹就建立 // 如果没有对应文件夹就建立
func fileTypeJudge(fileType string) error { func catalogBuild(fileType string) error {
err := os.Mkdir("pastebin/backend/files/"+fileType, 0666)
if err != nil {
return err
}
return nil return nil
} }
var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
func IdGen(n int) string {
b := make([]rune, n)
rand.Seed(time.Now().UnixNano())
//for i:=0;i<n;i++{
for i := range b {
b[i] = letters[rand.Intn(len(letters))]
}
return string(b)
}
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