You need to sign in or sign up before continuing.
Commits (3)
...@@ -2,9 +2,9 @@ config.yaml ...@@ -2,9 +2,9 @@ config.yaml
backend/files backend/files
.history/ .history/
.idea/** .idea/**
conf.yaml
docs/** docs/**
**.exe **.exe
files/** files/**
**.log **.log
**.sql **.sql
\ No newline at end of file mysql
\ No newline at end of file
...@@ -11,6 +11,7 @@ import ( ...@@ -11,6 +11,7 @@ import (
"github.com/spf13/viper" "github.com/spf13/viper"
) )
// =======authenticate==========
var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ") var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
/* /*
...@@ -26,17 +27,33 @@ func IdGen(n int) string { ...@@ -26,17 +27,33 @@ func IdGen(n int) string {
return string(b) return string(b)
} }
/*
func IdGen(n int ) string{
id:=newId(n)
var check bool
// 数据库里查找有无sid与id相同
while (!check){
}
}
*/
/* /*
* 新建一个sid和url的关联,并返回一个bool表示成功与否 * 新建一个sid和url的关联,并返回一个bool表示成功与否
* 若无sessionId,生成一个八位sid并返回 * 若无sessionId,生成一个八位sid并返回
*/ */
func newAuthenticate(sid string, url string, passwd string, time time.Time) (rsid string, stat uint) { func newAuthenticate(sid string, url string, passwd string, time time.Time, isFirst bool) (rsid string, stat uint) {
if sid == "" { if sid == "" {
rsid = IdGen(8) rsid = IdGen(8)
} else { } else {
rsid = sid rsid = sid
} }
stat = model.Createlink(rsid, passwd, url, time) if isFirst {
model.CreatelinkFirstTime(rsid, url, time)
stat = 1
} else {
stat = model.Createlink(rsid, passwd, url, time)
}
return rsid, stat return rsid, stat
} }
...@@ -50,32 +67,12 @@ func Autheticate(cookie *http.Cookie, url string, passwd string, time time.Time) ...@@ -50,32 +67,12 @@ func Autheticate(cookie *http.Cookie, url string, passwd string, time time.Time)
return 1 // 鉴权通过 return 1 // 鉴权通过
} else { } else {
var stat uint var stat uint
sid, stat = newAuthenticate(sid, url, passwd, time) sid, stat = newAuthenticate(sid, url, passwd, time, false)
cookie.Value = sid cookie.Value = sid
return stat return stat
} }
} }
/*
* 通过文件扩展名获取ContentType
*/
func GetFileContentType(fileType string) string {
var StrRet string = ""
switch fileType {
case ".txt":
StrRet = "text/plain"
case ".csv":
StrRet = "text/csv"
case ".tex":
StrRet = "application/x-tex"
case ".md":
StrRet = "text/x-markdown"
default:
StrRet = "text/plain"
}
return StrRet
}
// 设置cookie name sid, value link // 设置cookie name sid, value link
func SetCookie(c echo.Context, cookie *http.Cookie, sid string, maxAge int, time_ time.Time) error { func SetCookie(c echo.Context, cookie *http.Cookie, sid string, maxAge int, time_ time.Time) error {
cookie.Name = "User" // 标识为user cookie.Name = "User" // 标识为user
...@@ -114,6 +111,7 @@ func overflow(info *File, threshold int) bool { ...@@ -114,6 +111,7 @@ func overflow(info *File, threshold int) bool {
} }
// / ========== DB related==================
func DBupdate(c echo.Context, info *File) (string, string) { func DBupdate(c echo.Context, info *File) (string, string) {
url := "http://pastebin/" + IdGen(8) url := "http://pastebin/" + IdGen(8)
cookie, _ := c.Cookie("User") cookie, _ := c.Cookie("User")
...@@ -123,8 +121,7 @@ func DBupdate(c echo.Context, info *File) (string, string) { ...@@ -123,8 +121,7 @@ func DBupdate(c echo.Context, info *File) (string, string) {
} else { } else {
sid = cookie.Value sid = cookie.Value
} }
sid, _ = newAuthenticate(sid, url, info.Url, info.Time) sid, _ = newAuthenticate(sid, url, info.Url, info.Time, true)
model.CreatelinkFirstTime(sid, url, info.Time)
if info.MaxAccess == 0 { if info.MaxAccess == 0 {
//num,_=strconv.ParseUint(GetSetting("maxDefaultAccess"), 10, 64) //num,_=strconv.ParseUint(GetSetting("maxDefaultAccess"), 10, 64)
info.MaxAccess = 30 // 设置最大默认可访问次数 info.MaxAccess = 30 // 设置最大默认可访问次数
...@@ -133,17 +130,6 @@ func DBupdate(c echo.Context, info *File) (string, string) { ...@@ -133,17 +130,6 @@ func DBupdate(c echo.Context, info *File) (string, string) {
return sid, url return sid, url
} }
// 格式化后缀,仿止出错
func TypeComplement(typ string) string {
// 类型判断
if typ == "" {
typ = ".txt"
} else if typ[0] != '.' {
typ = "." + typ
}
return typ
}
/* /*
* 没有设定过期时间,oriTime 为 0 * 没有设定过期时间,oriTime 为 0
* 此函数将默认过期时间设为当前时间后半小时 * 此函数将默认过期时间设为当前时间后半小时
...@@ -156,6 +142,8 @@ func timeAssign(oriTime time.Time) time.Time { ...@@ -156,6 +142,8 @@ func timeAssign(oriTime time.Time) time.Time {
} }
} }
// ===========sumdry=======================
// 从config读取数据
func GetSetting(key string) (value string) { func GetSetting(key string) (value string) {
// config // config
viper.SetConfigName("config") viper.SetConfigName("config")
...@@ -169,3 +157,34 @@ func GetSetting(key string) (value string) { ...@@ -169,3 +157,34 @@ func GetSetting(key string) (value string) {
logInfo := viper.GetStringMapString("setting") logInfo := viper.GetStringMapString("setting")
return logInfo[key] return logInfo[key]
} }
/*
* 通过文件扩展名获取ContentType
*/
func GetFileContentType(fileType string) string {
var StrRet string = ""
switch fileType {
case ".txt":
StrRet = "text/plain"
case ".csv":
StrRet = "text/csv"
case ".tex":
StrRet = "application/x-tex"
case ".md":
StrRet = "text/x-markdown"
default:
StrRet = "text/plain"
}
return StrRet
}
// 格式化后缀,仿止出错
func TypeComplement(typ string) string {
// 类型判断
if typ == "" {
typ = ".txt"
} else if typ[0] != '.' {
typ = "." + typ
}
return typ
}
...@@ -74,6 +74,7 @@ func SendFile(c echo.Context) error { ...@@ -74,6 +74,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
// response // response
switch stat { switch stat {
case 0: case 0:
return response.SendResponse(c, http.StatusForbidden, cookieMsg+"error:密码错误", "") //403 return response.SendResponse(c, http.StatusForbidden, cookieMsg+"error:密码错误", "") //403
case 1: // 鉴权通过 case 1: // 鉴权通过
......
...@@ -25,7 +25,7 @@ func Init() { ...@@ -25,7 +25,7 @@ func Init() {
if err != nil { if err != nil {
logrus.Fatal(err) logrus.Fatal(err)
} }
err = DB.AutoMigrate(&Sid{}, &Url{}) err = DB.AutoMigrate(&Rel{})
if err != nil { if err != nil {
logrus.Fatal(err) logrus.Fatal(err)
} }
...@@ -97,36 +97,23 @@ func Checkt(p Content) bool { ...@@ -97,36 +97,23 @@ func Checkt(p Content) bool {
} }
// 这个函数那边有可能需要 // 这个函数那边有可能需要
// 检查sid是否超 // 检查sid_url是否超时
func Checkt1(sid string) bool { // 同时也可以查询sid,_url是否关联
var s Sid func Find(sid string, _url string) bool {
DB.First(&s, "S = ?", sid) var s Rel
err := DB.Where(&Rel{Sid: sid, Url: _url}).First(&s).Error
if err != nil {
return false
}
t := time.Now() t := time.Now()
if t.After(s.Time) { if t.After(s.Time) {
DB.Delete(&s)
return false return false
} else { } else {
return true return true
} }
} }
// 查找sessionid和url是否关联
func Find(sid string, _url string) bool {
var s Sid
err1 := DB.First(&s, "S = ?", sid).Error
//fmt.Println(url.Url1,url.ID,"\n")
if err1 != nil {
return false
}
var Urls []Url
DB.Model(&s).Association("Url1").Find(&Urls)
for _, value := range Urls {
if value.Url1 == _url {
return true
}
}
return false
}
// 通过url查询文件类型 // 通过url查询文件类型
func Find1(_url string, key string) string { func Find1(_url string, key string) string {
var p Content var p Content
...@@ -142,18 +129,6 @@ func Find1(_url string, key string) string { ...@@ -142,18 +129,6 @@ func Find1(_url string, key string) string {
return "" return ""
} }
//var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789")
/*//*随机生成字符串
func randStr(n int) string {
rand.Seed(time.Now().Unix())
b := make([]rune, n)
for i := range b {
b[i] = letters[rand.Intn(len(letters))]
}
return string(b)
}*/
// 新建用户 // 新建用户
func Createuser(_User Users) { func Createuser(_User Users) {
//_User.SessionId=randStr(8) //_User.SessionId=randStr(8)
...@@ -162,7 +137,7 @@ func Createuser(_User Users) { ...@@ -162,7 +137,7 @@ func Createuser(_User Users) {
//新建链接 //新建链接
/* 返回值: /* 返回值:
3 表示sid已经过期,需要重新分配 3 表示sid_url已经过期,需要重新分配
2 表示内容过期 2 表示内容过期
1 表示密码正确 1 表示密码正确
0 表示密码不正确 0 表示密码不正确
...@@ -175,23 +150,18 @@ func Createlink(sid string, _passwd string, _url string, _Time1 time.Time) uint ...@@ -175,23 +150,18 @@ func Createlink(sid string, _passwd string, _url string, _Time1 time.Time) uint
//fmt.Println(sid,_passwd,_url,_Time1) //fmt.Println(sid,_passwd,_url,_Time1)
return 2 return 2
} }
var p1 Sid var p1 Rel
err := DB.First(&p1, "S = ?", sid).Error err := DB.Where(&Rel{Sid: sid, Url: _url}).First(&p1).Error
if err != nil { if err != nil {
sid1 := Sid{ rel1 := Rel{
S: sid, Sid: sid,
Url: _url,
Time: _Time1, Time: _Time1,
Url1: []Url{
{Url1: _url},
},
} }
DB.Create(&sid1) DB.Create(&rel1)
} else { } else {
t := time.Now() p1.Time = _Time1
if t.After(p1.Time) { DB.Save(&p1)
return 3
}
DB.Model(&p1).Association("Url1").Append(&Url{Url1: _url})
} }
return 1 return 1
} }
...@@ -200,20 +170,15 @@ func Createlink(sid string, _passwd string, _url string, _Time1 time.Time) uint ...@@ -200,20 +170,15 @@ 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 := Rel{
S: sid, Sid: sid,
Url: _url,
Time: _Time1, Time: _Time1,
Url1: []Url{
{Url1: _url},
},
} }
DB.Create(&sid1) DB.Create(&sid1)
} }
/* /*
func Test() { func Test() {
var p Content fmt.Println(Find("ab","12345"))
var Url2 string = "1234"
DB.First(&p, "url1 = ?", Url2)
fmt.Println(Checkt(p))
}*/ }*/
...@@ -19,17 +19,11 @@ type Content struct { ...@@ -19,17 +19,11 @@ type Content struct {
Filename string Filename string
} }
type Sid struct { type Rel struct {
gorm.Model gorm.Model
Time time.Time Sid string
S string Url string
Url1 []Url `gorm:"many2many:sid_urls;"` Time time.Time
}
type Url struct
{
gorm.Model
Url1 string
} }
type Users struct { type Users struct {
......
...@@ -13,11 +13,6 @@ ...@@ -13,11 +13,6 @@
model: model:
init: 数据库相关函数,包含着有关和数据库交互的各种操作,比如连接数据库,通过数据库反查内容,保存文本和文件信息等操作的函数 init: 数据库相关函数,包含着有关和数据库交互的各种操作,比如连接数据库,通过数据库反查内容,保存文本和文件信息等操作的函数
model: 存储着各表的结构,一共有五个结构体 model: 存储着各表的结构,一共有五个结构体
mysql:
pastebinDatabase: 建立数据库脚本
pastebinTable: 建立数据库的数据表脚本
files:(修改)
sys.log: 日志文件
config.yaml: 修改设置 config.yaml: 修改设置
go.mod go.mod
go.sum go.sum
......