Commit d1058854 authored by fengjie Lu's avatar fengjie Lu
Browse files

final

parent 17b02594
Pipeline #44 canceled with stages
...@@ -3,6 +3,7 @@ package controller ...@@ -3,6 +3,7 @@ package controller
import ( import (
"Pastebin/app/response" "Pastebin/app/response"
"Pastebin/model" "Pastebin/model"
"errors"
"fmt" "fmt"
"math/rand" "math/rand"
"net/http" "net/http"
...@@ -11,6 +12,7 @@ import ( ...@@ -11,6 +12,7 @@ import (
"github.com/labstack/echo/v4" "github.com/labstack/echo/v4"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"gorm.io/gorm"
) )
var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ") var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
...@@ -24,56 +26,152 @@ func randStr(n int) string { ...@@ -24,56 +26,152 @@ func randStr(n int) string {
return string(b) return string(b)
} }
func Text(c echo.Context) error { func Text(c echo.Context) error {
rec := new(LIST) var rec LIST
rec.Id = 0 rec.Id = 0
rec.Name = randStr(4) var s string
rec.Password = c.QueryParam("Password") for {
rec.ClickTimes, _ = strconv.Atoi(c.QueryParam("ClickTimes")) s = randStr(4)
rec.RemainTime, _ = strconv.Atoi(c.QueryParam("RemainTime")) if err := model.DB.Table("lists").Where("name = ?", s).First(&LIST{}).Error; errors.Is(err, gorm.ErrRecordNotFound) {
rec.Txt = c.QueryParam("Txt") rec.Name = s
logrus.Error("Genarating String Success!")
break
}
logrus.Info("Genarating New String")
}
rec.Password = c.Request().PostFormValue("Password")
rec.Clicktimes, _ = strconv.Atoi(c.Request().PostFormValue("Clicktimes"))
rec.Remaintime, _ = strconv.Atoi(c.Request().PostFormValue("Remaintime"))
rec.Txt = c.Request().PostFormValue("Txt")
rec.Type = false rec.Type = false
if err := model.DB.Debug().Create(&rec).Error; err != nil { tm := time.Now()
rec.Starttime = tm.Unix()
if err := model.DB.Table("lists").Debug().Create(&rec).Error; err != nil {
logrus.Error(err) logrus.Error(err)
} }
println("Insert TEXT success!") println("Insert TEXT success!")
s := fmt.Sprintf("WebCode:%s", rec.Name) s = fmt.Sprintf("WebCode:%s", rec.Name)
return response.SendResponse(c, http.StatusOK, "Insert TEXT success!", s) return response.SendResponse(c, http.StatusOK, "Insert TEXT success!", s)
} }
func File(c echo.Context) error { func File(c echo.Context) error {
rec := new(LIST) var rec LIST
rec.Id = 0 rec.Id = 0
rec.Name = randStr(4) var s string
rec.Password = c.QueryParam("Password") for {
rec.ClickTimes, _ = strconv.Atoi(c.QueryParam("ClickTimes")) s = randStr(4)
rec.RemainTime, _ = strconv.Atoi(c.QueryParam("RemainTime")) if err := model.DB.Table("lists").Where("name = ?", s).First(&LIST{}).Error; errors.Is(err, gorm.ErrRecordNotFound) {
rec.File = []byte(c.QueryParam("File")) rec.Name = s
logrus.Error("Genarating String Success!")
break
}
logrus.Info("Genarating New String")
}
rec.Password = c.Request().PostFormValue("Password")
rec.Clicktimes, _ = strconv.Atoi(c.Request().PostFormValue("Clicktimes"))
rec.Remaintime, _ = strconv.Atoi(c.Request().PostFormValue("Remaintime"))
tm := time.Now()
rec.Starttime = tm.Unix()
if err := c.Request().ParseMultipartForm(100000); err != nil {
logrus.Error(err)
}
file, fileHeader, err := c.Request().FormFile("File")
if err != nil {
logrus.Error(err)
}
defer file.Close()
fmt.Printf("the uploaded file: name[%s], size[%d], header[%#v]\n",
fileHeader.Filename, fileHeader.Size, fileHeader.Header)
data := make([]byte, fileHeader.Size)
_, err = file.Read(data)
if err != nil {
logrus.Error(err)
}
rec.File = data
rec.Type = true rec.Type = true
if err := model.DB.Debug().Create(&rec).Error; err != nil { if err := model.DB.Table("lists").Debug().Create(&rec).Error; err != nil {
logrus.Error(err) logrus.Error(err)
} }
println("Insert FILE success!") println("Insert FILE success!")
s := fmt.Sprintf("WebCode:%s", rec.Name) s = fmt.Sprintf("WebCode:%s", rec.Name)
return response.SendResponse(c, http.StatusOK, "Insert FILE success!", s) return response.SendResponse(c, http.StatusOK, "Insert FILE success!", s)
} }
/*
func Session(c echo.Context) error { func Session(c echo.Context) error {
var ses SESSION
kie := c.Request().PostFormValue("Cookie")
if err := model.DB.Table("sessions").Debug().Where("cookie = ?", kie).First(&ses).Error; err != nil {
logrus.Error("Not find Session")
return response.SendResponse(c, http.StatusOK, "", "Status:false")
}
tm := time.Now()
if tm.Unix()-ses.Starttime >= int64(ses.Time) {
logrus.Info("SESSION Time out")
if err := model.DB.Table("sessions").Debug().Where("cookie = ?", kie).Delete(&ses).Error; err != nil {
logrus.Error(err)
}
return response.SendResponse(c, http.StatusOK, "", "Status:false")
} else {
return response.SendResponse(c, http.StatusOK, "", "Status:true")
}
} }
func Check(c echo.Context) error {
func Check(c echo.Context) error {
var rec LIST
name := c.Request().PostFormValue("Name")
recPass := c.Request().PostFormValue("Password")
if err := model.DB.Table("lists").Debug().Where("name = ?", name).First(&rec).Error; err != nil {
logrus.Error(err)
}
tm := time.Now()
if tm.Unix()-rec.Starttime >= int64(rec.Remaintime) || rec.Clicktimes == 0 {
if err := model.DB.Table("lists").Debug().Where("name = ?", name).First(&rec).Error; err != nil {
logrus.Error(err)
}
logrus.Info("LIST Time out or Click out")
return response.SendResponse(c, http.StatusBadRequest, "", "Status:false")
}
if rec.Password == recPass {
CreateSession(c.Request().PostFormValue("Cookie"))
if err := model.DB.Table("lists").Debug().Where("name = ?", name).Update("Clicktimes", rec.Clicktimes-1).Error; err != nil {
logrus.Error(err)
}
return response.SendResponse(c, http.StatusOK, "", "Status:true")
} else {
return response.SendResponse(c, http.StatusOK, "", "Status:false")
}
} }
func Content(c echo.Context) error { func Content(c echo.Context) error {
var rec LIST
name := c.Request().PostFormValue("Name")
if err := model.DB.Table("lists").Debug().Where("name = ?", name).First(&rec).Error; err != nil {
logrus.Error(err)
return response.SendResponse(c, http.StatusBadRequest, "", rec)
}
return response.SendResponse(c, http.StatusOK, "", rec)
}
func CreateSession(Cookie string) {
tm := time.Now()
if err := model.DB.Table("sessions").Create(&SESSION{0, Cookie, 30, tm.Unix()}); err != nil {
logrus.Error(err)
}
println("Session is Created")
} }
*/
type LIST struct { type LIST struct {
Id int `gorm:"primary_key;AUTO_INCREMENT"` Id int `gorm:"primary_key;AUTO_INCREMENT"`
Name string Name string `json:"Name" form:"Name" query:"Name"`
Password string Password string `json:"Password" form:"Password" query:"Password"`
Type bool Type bool `json:"Type" form:"Type" query:"Type"`
Txt string Txt string `json:"Txt" form:"Txt" query:"Txt"`
File []byte `gorm:"type:MediumBlob"` File []byte `gorm:"type:MediumBlob" json:"File" form:"File" query:"File"`
ClickTimes int Clicktimes int `json:"Clicktimes" form:"Clicktimes" query:"Clicktimes"`
RemainTime int Remaintime int `json:"Remaintime" form:"Remaintime" query:"Remaintime"`
Starttime int64
}
type SESSION struct {
Id int `gorm:"primary_key;AUTO_INCREMENT"`
Cookie string `json:"Cookie" form:"Cookie" query:"Cookie"`
Time int //剩余时间
Starttime int64
} }
...@@ -9,7 +9,7 @@ func AddRoutes() { ...@@ -9,7 +9,7 @@ func AddRoutes() {
e.Use(middleware.Auth) e.Use(middleware.Auth)
e.POST("/submit/text", controller.Text) e.POST("/submit/text", controller.Text)
e.POST("/submit/file", controller.File) e.POST("/submit/file", controller.File)
//e.GET("/query/session", controller.Session) e.POST("/query/session", controller.Session)
//e.GET("/query/check", controller.Check) e.POST("/query/check", controller.Check)
//e.GET("/query/content", controller.Content) e.POST("/query/content", controller.Content)
} }
...@@ -15,6 +15,10 @@ func DBinit() { ...@@ -15,6 +15,10 @@ func DBinit() {
if err != nil { if err != nil {
logrus.Fatal(err) logrus.Fatal(err)
} }
err = DB.AutoMigrate(&SESSION{})
if err != nil {
logrus.Fatal(err)
}
println("Connect Success!") println("Connect Success!")
} }
...@@ -34,6 +38,7 @@ func ConnectDatabase() { ...@@ -34,6 +38,7 @@ func ConnectDatabase() {
"?charset=utf8mb4&parseTime=True&loc=Local" "?charset=utf8mb4&parseTime=True&loc=Local"
var err error var err error
DB, err = gorm.Open(mysql.Open(dbArgs), &gorm.Config{}) DB, err = gorm.Open(mysql.Open(dbArgs), &gorm.Config{})
DB.AllowGlobalUpdate = true
if err != nil { if err != nil {
logrus.Fatal(err) logrus.Fatal(err)
} }
...@@ -48,6 +53,13 @@ type LIST struct { ...@@ -48,6 +53,13 @@ type LIST struct {
Type bool Type bool
Txt string Txt string
File []byte `gorm:"type:MediumBlob"` File []byte `gorm:"type:MediumBlob"`
ClickTimes int Clicktimes int
RemainTime int Remaintime int
Starttime int64
}
type SESSION struct {
Id int `gorm:"primary_key;AUTO_INCREMENT"`
Cookie string `json:"Cookie" form:"Cookie" query:"Cookie"`
Time int //剩余时间
Starttime int64
} }
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