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

本地调试2.2

parent b23a6407
...@@ -11,6 +11,7 @@ import ( ...@@ -11,6 +11,7 @@ import (
"github.com/labstack/echo/v4" "github.com/labstack/echo/v4"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"github.com/spf13/viper"
) )
// 如果没有对应文件夹就建立 // 如果没有对应文件夹就建立
...@@ -184,7 +185,11 @@ func DBupdate(c echo.Context, filePath string, info *File) (string, string) { ...@@ -184,7 +185,11 @@ func DBupdate(c echo.Context, filePath string, info *File) (string, string) {
} }
sid, _ = newAuthenticate(sid, url, info.Url, info.Time) sid, _ = newAuthenticate(sid, url, info.Url, info.Time)
model.CreatelinkFirstTime(sid, url, info.Time) model.CreatelinkFirstTime(sid, url, info.Time)
model.Savetext(filePath, 30, info.Passwd, info.Time, url) if info.MaxAccess == 0 {
//num,_=strconv.ParseUint(GetSetting("maxDefaultAccess"), 10, 64)
info.MaxAccess = 30 // 设置最大默认可访问次数
}
model.Savetext(filePath, info.MaxAccess, info.Passwd, info.Time, url)
return sid, url return sid, url
} }
...@@ -235,3 +240,17 @@ func timeAssign(oriTime time.Time) time.Time { ...@@ -235,3 +240,17 @@ func timeAssign(oriTime time.Time) time.Time {
return oriTime return oriTime
} }
} }
func GetSetting(key string) (value string) {
// config
viper.SetConfigName("config")
viper.SetConfigType("yaml")
viper.AddConfigPath("./")
err := viper.ReadInConfig()
if err != nil {
logrus.Panic(err)
}
// connect to database
logInfo := viper.GetStringMapString("setting")
return logInfo[key]
}
...@@ -19,7 +19,8 @@ type File struct { ...@@ -19,7 +19,8 @@ type File struct {
User string `json:"user" form:"user" query:"user"` User string `json:"user" form:"user" query:"user"`
Passwd string `json:"passwd" form:"passwd" query:"passwd"` Passwd string `json:"passwd" form:"passwd" query:"passwd"`
Url string `json:"url" form:"url" query:"url"` Url string `json:"url" form:"url" query:"url"`
Time time.Time `json:"time" form:"time" query:"time"` // 用户指定的时间期限 Time time.Time `json:"time" form:"time" query:"time"` // 用户指定的时间期限
MaxAccess uint `json:"maxaccess form:"maxaccess" query:"maxaccess"` // 文件最大可访问次数
//Expiry time.Time `json:"expiry"` // 有效期 //Expiry time.Time `json:"expiry"` // 有效期
//Content string `json:"content"` //Content string `json:"content"`
} }
...@@ -46,7 +47,7 @@ func RecvFile(c echo.Context) error { ...@@ -46,7 +47,7 @@ func RecvFile(c echo.Context) error {
defer dst.Close() defer dst.Close()
if !overflow(dst, 8*1024*1024) { if !overflow(dst, 8*1024*1024) {
return c.HTML(http.StatusOK, "<p>文件上传失败: 文件大小超过8MB</p>") return c.HTML(http.StatusOK, "<p>error:文件上传失败: 文件大小超过8MB</p>")
} }
// 这里将用户上传的文件复制到服务端的目标文件 // 这里将用户上传的文件复制到服务端的目标文件
...@@ -55,6 +56,8 @@ func RecvFile(c echo.Context) error { ...@@ -55,6 +56,8 @@ func RecvFile(c echo.Context) error {
return err return err
} }
// api 分配
// 更新数据库 // 更新数据库
_, url := DBupdate(c, filePath, info) // 不分配uid _, url := DBupdate(c, filePath, info) // 不分配uid
//sid := "fakesid" // //sid := "fakesid" //
...@@ -87,7 +90,7 @@ func SendFile(c echo.Context) error { ...@@ -87,7 +90,7 @@ func SendFile(c echo.Context) error {
stat := Autheticate(cookie, info.Url, info.Passwd, info.Time) // 包含创建链接Createlink stat := Autheticate(cookie, info.Url, info.Passwd, info.Time) // 包含创建链接Createlink
switch stat { switch stat {
case 0: case 0:
return c.HTML(http.StatusBadRequest, "密码错误") return c.HTML(http.StatusForbidden, "error:密码错误") // 403
case 1: // 鉴权通过 case 1: // 鉴权通过
info.FileType = TypeComplement(info.FileType) // 格式化后缀,仿止出错 info.FileType = TypeComplement(info.FileType) // 格式化后缀,仿止出错
//stat:=model.Createlink(cookie.Value,info.Passwd,info.Url,info.Time) //stat:=model.Createlink(cookie.Value,info.Passwd,info.Url,info.Time)
...@@ -96,9 +99,10 @@ func SendFile(c echo.Context) error { ...@@ -96,9 +99,10 @@ func SendFile(c echo.Context) error {
c.JSON(http.StatusOK, info) c.JSON(http.StatusOK, info)
return response.SendResponse3(c, http.StatusOK, GetFileContentType(info.FileType), data) // 返回数据 return response.SendResponse3(c, http.StatusOK, GetFileContentType(info.FileType), data) // 返回数据
case 2: case 2:
return c.HTML(http.StatusBadRequest, "内容过期") return c.HTML(http.StatusGone, "error:内容过期")
case 3: case 3:
return c.HTML(http.StatusBadRequest, "cookie过期") return c.HTML(http.StatusUnauthorized, "请进行身份验证")
} }
return nil return nil
} }
......
package app package app
import ( import (
"backend/app/controller"
"backend/utils" "backend/utils"
//"net/http" //"net/http"
"github.com/labstack/echo/v4" "github.com/labstack/echo/v4"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"github.com/spf13/viper"
) )
var e *echo.Echo var e *echo.Echo
...@@ -20,7 +21,7 @@ func InitWebFramework() { ...@@ -20,7 +21,7 @@ func InitWebFramework() {
} }
func StartServer() { func StartServer() {
e.Logger.Fatal(e.Start(getSetting("testurl"))) // 启动服务,注意默认端口80不能省略 e.Logger.Fatal(e.Start(controller.GetSetting("testurl"))) // 启动服务,注意默认端口80不能省略
//e.Logger.Fatal(e.Start("127.0.0.1:80")) // 启动服务,注意默认端口80不能省略 //e.Logger.Fatal(e.Start("127.0.0.1:80")) // 启动服务,注意默认端口80不能省略
//e.Logger.Fatal(e.Start("http://xlab.zju.edu.cn/test/pastebin/group-1")) // 启动服务,注意默认端口80不能省略,需要域名解析,config //e.Logger.Fatal(e.Start("http://xlab.zju.edu.cn/test/pastebin/group-1")) // 启动服务,注意默认端口80不能省略,需要域名解析,config
} }
...@@ -37,17 +38,3 @@ func InitLogger() { ...@@ -37,17 +38,3 @@ func InitLogger() {
}) })
logrus.SetReportCaller(true) logrus.SetReportCaller(true)
} }
func getSetting(key string) (value string) {
// config
viper.SetConfigName("config")
viper.SetConfigType("yaml")
viper.AddConfigPath("./")
err := viper.ReadInConfig()
if err != nil {
logrus.Panic(err)
}
// connect to database
logInfo := viper.GetStringMapString("setting")
return logInfo[key]
}
...@@ -17,4 +17,9 @@ func addRoutes() { ...@@ -17,4 +17,9 @@ func addRoutes() {
//api.POST("/text/recv",controller.RecvText) //api.POST("/text/recv",controller.RecvText)
//api.POST("/text/send",controller.RecvText) //api.POST("/text/send",controller.RecvText)
//files.Post("/download/*",controller.Download)
}
func ApiAssign() {
} }
...@@ -12,7 +12,6 @@ import ( ...@@ -12,7 +12,6 @@ import (
) )
var DB *gorm.DB var DB *gorm.DB
var id1 uint = 12
func Init() { func Init() {
ConnectDatabase() ConnectDatabase()
...@@ -47,7 +46,6 @@ func ConnectDatabase() { ...@@ -47,7 +46,6 @@ func ConnectDatabase() {
"@(localhost)/" + logInfo["database"] + "?charset=utf8mb4&parseTime=True&loc=Local" "@(localhost)/" + logInfo["database"] + "?charset=utf8mb4&parseTime=True&loc=Local"
DB, err = gorm.Open(mysql.Open(sqlInfo), &gorm.Config{}) DB, err = gorm.Open(mysql.Open(sqlInfo), &gorm.Config{})
if err != nil { if err != nil {
logrus.Println("数据库连接失败")
logrus.Panic(err) logrus.Panic(err)
} }
} }
...@@ -66,6 +64,8 @@ func Findlink(_url string) string { ...@@ -66,6 +64,8 @@ func Findlink(_url string) string {
// save text information // save text information
// 保存文本和文件信息 // 保存文本和文件信息
func Savetext(_Route string, _Time uint, _Passwd string, _Time1 time.Time, _url string) { func Savetext(_Route string, _Time uint, _Passwd string, _Time1 time.Time, _url string) {
var id1 int64
DB.Model(&Content{}).Count(&id1)
id1++ id1++
p := Content{ p := Content{
ID: id1, ID: id1,
...@@ -85,26 +85,27 @@ func Savetext(_Route string, _Time uint, _Passwd string, _Time1 time.Time, _url ...@@ -85,26 +85,27 @@ func Savetext(_Route string, _Time uint, _Passwd string, _Time1 time.Time, _url
func Checkt(p Content) bool { func Checkt(p Content) bool {
t := time.Now() t := time.Now()
p.Time2++ p.Time2++
//fmt.Println(p.Time2)
if p.Time2 > p.Time || t.After(p.Time1) { if p.Time2 > p.Time || t.After(p.Time1) {
DB.Delete(&p) DB.Delete(&p)
return true return true
} }
DB.Model(&Content{}).Where("Url1 = ?", p.Url1).Update("Time2", p.Time2)
return false return false
} }
/* // 这个函数那边有可能需要
这个函数那边有可能需要 // 检查sid是否超时
//检查sid是否超时
func Checkt1(sid string) bool { func Checkt1(sid string) bool {
var s Sid var s Sid
DB.First(&s, "S = ?",sid) DB.First(&s, "S = ?", sid)
t := time.Now() t := time.Now()
if t.After(s._Time) { if t.After(s.Time) {
return false return false
} else { } else {
return true return true
} }
}*/ }
// 查找sessionid和url是否关联 // 查找sessionid和url是否关联
func Find(sid string, _url string) bool { func Find(sid string, _url string) bool {
...@@ -152,16 +153,17 @@ func Createuser(_User Users) { ...@@ -152,16 +153,17 @@ func Createuser(_User Users) {
func Createlink(sid string, _passwd string, _url string, _Time1 time.Time) uint { func Createlink(sid string, _passwd string, _url string, _Time1 time.Time) uint {
var p Content var p Content
DB.First(&p, "Url1 = ?", _url) DB.First(&p, "Url1 = ?", _url)
if Checkt(p) {
return 2
}
if p.Passwd == _passwd { if p.Passwd == _passwd {
if Checkt(p) {
//fmt.Println(sid,_passwd,_url,_Time1)
return 2
}
var p1 Sid var p1 Sid
err := DB.First(&p1, "S = ?", sid).Error err := DB.First(&p1, "S = ?", sid).Error
if err != nil { if err != nil {
sid1 := Sid{ sid1 := Sid{
S: sid, S: sid,
_Time: _Time1, Time: _Time1,
Url1: []Url{ Url1: []Url{
{Url1: _url}, {Url1: _url},
}, },
...@@ -169,7 +171,7 @@ func Createlink(sid string, _passwd string, _url string, _Time1 time.Time) uint ...@@ -169,7 +171,7 @@ func Createlink(sid string, _passwd string, _url string, _Time1 time.Time) uint
DB.Create(&sid1) DB.Create(&sid1)
} else { } else {
t := time.Now() t := time.Now()
if t.After(p1._Time) { if t.After(p1.Time) {
return 3 return 3
} }
DB.Model(&p1).Association("Url1").Append(&Url{Url1: _url}) DB.Model(&p1).Association("Url1").Append(&Url{Url1: _url})
...@@ -182,11 +184,19 @@ func Createlink(sid string, _passwd string, _url string, _Time1 time.Time) uint ...@@ -182,11 +184,19 @@ func Createlink(sid string, _passwd string, _url string, _Time1 time.Time) uint
// 第一次上传 // 第一次上传
func CreatelinkFirstTime(sid string, _url string, _Time1 time.Time) { func CreatelinkFirstTime(sid string, _url string, _Time1 time.Time) {
sid1 := Sid{ sid1 := Sid{
S: sid, S: sid,
_Time: _Time1, Time: _Time1,
Url1: []Url{ Url1: []Url{
{Url1: _url}, {Url1: _url},
}, },
} }
DB.Create(&sid1) DB.Create(&sid1)
} }
/*
func Test() {
var p Content
var Url2 string = "1234"
DB.First(&p, "url1 = ?", Url2)
fmt.Println(Checkt(p))
}*/
...@@ -6,7 +6,7 @@ import ( ...@@ -6,7 +6,7 @@ import (
) )
type Content struct { type Content struct {
ID uint `gorm:"primarykey"` ID int64 `gorm:"primarykey"`
Route string Route string
Time uint Time uint
Passwd string Passwd string
...@@ -19,9 +19,9 @@ type Content struct { ...@@ -19,9 +19,9 @@ type Content struct {
type Sid struct { type Sid struct {
gorm.Model gorm.Model
Time time.Time
S string S string
_Time time.Time Url1 []Url `gorm:"many2many:sid_urls;"`
Url1 []Url `gorm:"many2many:sid_urls;"`
} }
type Url struct type Url struct
......
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