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

本地调试2.2

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