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

完善了interface.go

parent 7f2a2a31
......@@ -4,14 +4,18 @@ import (
"net"
"net/http"
"pastebin_backend/app/response"
//"pastebin_backend/model"
//"error"
"time"
"github.com/labstack/echo/v4"
//"time"
//"encoding/json"
"fmt"
"io"
"math/rand"
"os"
//"github.com/google/uuid"
//"github.com/go-playground/validator"
......@@ -117,9 +121,21 @@ func RecvFile(c echo.Context) error {
}
filePath := "./files/" + info.FileType + "/" + file.Filename
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()
if fi.Size()>8*1024*10224{
fi, _ := dst.Stat()
if fi.Size() > 8*1024*10224 {
c.HTML(http.StatusOK, fmt.Sprintf("<p>文件上传失败: 文件大小超过8MB</p>"))
}
......@@ -134,23 +150,24 @@ func RecvFile(c echo.Context) error {
return err
}
// 更新数据库
// 更新数据库
/*
dinfo:=model.Users{
Username: info.User,
Passwd: info.Passwd,
Name:info.FileName,// 无后缀
Size: uint(fi.Size()),
Route:filePath,
//Time1: info.Expiry,
}*/
dinfo:=model.Users{
Username: info.User,
Passwd: info.Passwd,
Name:info.FileName,// 无后缀
Size: uint(fi.Size()),
Route:filePath,
//Time1: info.Expiry,
}*/
return c.HTML(http.StatusOK, fmt.Sprintf("<p>文件上传成功: %s</p>", file.Filename))
}
// 根据用户请求的文件名(不带后缀)和文件类型,返回对应文件的链接
func SendFile(c echo.Context) error {
// 判断需不需要密码
info := new(FileRe)
if err := c.Bind(info); err != nil {
return err
......@@ -164,15 +181,15 @@ func SendFile(c echo.Context) error {
fmt.Println(err)
return err
}
fi,err:=src.Stat()
if err != nil{
fi, err := src.Stat()
if err != nil {
return err
}
defer func() {
src.Close()
}()
data := make([]byte, fi.Size())
count, err := src.Read(data)
//return response.SendResponse(c, http.StatusOK, "", "pong!")
......@@ -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
}
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