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

本地调试2.1

parent 9d3ba6f6
...@@ -41,33 +41,29 @@ func IdGen(n int) string { ...@@ -41,33 +41,29 @@ func IdGen(n int) string {
* 新建一个sid和url的关联,并返回一个bool表示成功与否 * 新建一个sid和url的关联,并返回一个bool表示成功与否
* 若无sessionId,生成一个八位sid并返回 * 若无sessionId,生成一个八位sid并返回
*/ */
func newAuthenticate(sid string, url string, passwd string) (rsid string, b bool) { func newAuthenticate(sid string, url string, passwd string, time time.Time) (rsid string, stat uint) {
if sid == "" { if sid == "" {
rsid = IdGen(8) rsid = IdGen(8)
} else { } else {
rsid = sid rsid = sid
} }
b = model.Createlink(rsid, passwd, url) stat = model.Createlink(rsid, passwd, url, time)
return rsid, b return rsid, stat
} }
/* /*
* 判断用户有无权限访问 * 判断用户有无权限访问
* sid 用cookie 储存传输,如果新生成sid,则保存至cookie * sid 用cookie 储存传输,如果新生成sid,则保存至cookie
*/ */
func Autheticate(cookie *http.Cookie, url string, passwd string) bool { func Autheticate(cookie *http.Cookie, url string, passwd string, time time.Time) uint {
sid := cookie.Value sid := cookie.Value
if model.Find(sid, url) { if model.Find(sid, url) {
return true return 1 // 鉴权通过
} else { } else {
if passwd != "" { var stat uint
var b bool sid, stat = newAuthenticate(sid, url, passwd, time)
sid, b = newAuthenticate(sid, url, passwd) cookie.Value = sid
cookie.Value = sid return stat
return b
} else {
return false
}
} }
} }
...@@ -133,6 +129,7 @@ func srcRead(c echo.Context, info *File) (src multipart.File, err error) { ...@@ -133,6 +129,7 @@ func srcRead(c echo.Context, info *File) (src multipart.File, err error) {
return nil, err return nil, err
} }
info.FileName = file.Filename info.FileName = file.Filename
return src, nil return src, nil
} }
...@@ -185,8 +182,8 @@ func DBupdate(c echo.Context, filePath string, info *File) (string, string) { ...@@ -185,8 +182,8 @@ func DBupdate(c echo.Context, filePath string, info *File) (string, string) {
} else { } else {
sid = cookie.Value sid = cookie.Value
} }
sid, _ = newAuthenticate(sid, url, info.Url) sid, _ = newAuthenticate(sid, url, info.Url, info.Time)
model.Createlink(sid, info.Passwd, url) model.CreatelinkFirstTime(sid, url, info.Time)
model.Savetext(filePath, 30, info.Passwd, info.Time, url) model.Savetext(filePath, 30, info.Passwd, info.Time, url)
return sid, url return sid, url
} }
...@@ -226,3 +223,15 @@ func TypeComplement(typ string) string { ...@@ -226,3 +223,15 @@ func TypeComplement(typ string) string {
} }
return typ return typ
} }
/*
* 没有设定过期时间,oriTime 为 0
* 此函数将默认过期时间设为当前时间后半小时
*/
func timeAssign(oriTime time.Time) time.Time {
if oriTime.IsZero() {
return time.Now().Add(30 * time.Minute)
} else {
return oriTime
}
}
...@@ -2,6 +2,7 @@ package controller ...@@ -2,6 +2,7 @@ package controller
import ( import (
"backend/app/response" "backend/app/response"
"backend/model"
"fmt" "fmt"
"io" "io"
"net/http" "net/http"
...@@ -12,13 +13,13 @@ import ( ...@@ -12,13 +13,13 @@ import (
) )
type File struct { type File struct {
FileName string `json:"fileName"` FileName string `json:"fileName" form:"fileName" query:"fileName"`
FileType string `json:"fileType"` FileType string `json:"fileType" form:"fileType" query:"fileName"`
Expiration int `json:"expiration"` Expiration int `json:"expiration" form:"expiration" query:"expiration"`
User string `json:"user"` User string `json:"user" form:"user" query:"user"`
Passwd string `json:"passwd"` Passwd string `json:"passwd" form:"passwd" query:"passwd"`
Url string `json:"url"` Url string `json:"url" form:"url" query:"url"`
Time time.Time `json:"time"` // 用户指定的时间期限 Time time.Time `json:"time" form:"time" query:"time"` // 用户指定的时间期限
//Expiry time.Time `json:"expiry"` // 有效期 //Expiry time.Time `json:"expiry"` // 有效期
//Content string `json:"content"` //Content string `json:"content"`
} }
...@@ -37,6 +38,7 @@ func RecvFile(c echo.Context) error { ...@@ -37,6 +38,7 @@ func RecvFile(c echo.Context) error {
if err != nil { if err != nil {
return response.SendResponse4(c, err) // log在srcRead里进行 return response.SendResponse4(c, err) // log在srcRead里进行
} }
info.Time = timeAssign(info.Time) // 默认时间
// 创建目标文件,就是我们打算把用户上传的文件保存到什么地方 // 创建目标文件,就是我们打算把用户上传的文件保存到什么地方
// info.Filename 参数指的是我们以用户上传的文件名,作为目标文件名,也就是服务端保存的文件名跟用户上传的文件名一样 // info.Filename 参数指的是我们以用户上传的文件名,作为目标文件名,也就是服务端保存的文件名跟用户上传的文件名一样
dst, filePath := dstCreate(info) dst, filePath := dstCreate(info)
...@@ -58,7 +60,6 @@ func RecvFile(c echo.Context) error { ...@@ -58,7 +60,6 @@ func RecvFile(c echo.Context) error {
//sid := "fakesid" // //sid := "fakesid" //
//url := "fakeurl" // //url := "fakeurl" //
//cookie := new(http.Cookie) //cookie := new(http.Cookie)
return response.SendResponse2(c, http.StatusOK, GetFileContentType(info.FileType), fmt.Sprintf("文件上传成功: %s", info.FileName), url) return response.SendResponse2(c, http.StatusOK, GetFileContentType(info.FileType), fmt.Sprintf("文件上传成功: %s", info.FileName), url)
} }
...@@ -73,22 +74,33 @@ func SendFile(c echo.Context) error { ...@@ -73,22 +74,33 @@ func SendFile(c echo.Context) error {
logrus.Println(err) logrus.Println(err)
return err return err
} }
info.Time = timeAssign(info.Time) // 默认时间为当前半小时后
cookie, _ := c.Cookie("User") cookie, _ := c.Cookie("User")
if cookie == nil { if cookie == nil {
c.HTML(http.StatusBadRequest, "没有cookie") c.HTML(http.StatusBadRequest, "没有cookie,已分配.\n")
cookie = new(http.Cookie)
SetCookie(c, cookie, IdGen(8), 1800, time.Time{})
} }
// 鉴权 // 鉴权
if !Autheticate(cookie, info.Url, info.Passwd) { stat := Autheticate(cookie, info.Url, info.Passwd, info.Time) // 包含创建链接Createlink
return response.SendResponse(c, http.StatusBadRequest, "msg", "", "密码错误") switch stat {
case 0:
return c.HTML(http.StatusBadRequest, "密码错误")
case 1: // 鉴权通过
info.FileType = TypeComplement(info.FileType) // 格式化后缀,仿止出错
//stat:=model.Createlink(cookie.Value,info.Passwd,info.Url,info.Time)
filePath := model.Findlink(info.Url)
data := readFile(filePath)
c.JSON(http.StatusOK, info)
return response.SendResponse3(c, http.StatusOK, GetFileContentType(info.FileType), data) // 返回数据
case 2:
return c.HTML(http.StatusBadRequest, "内容过期")
case 3:
return c.HTML(http.StatusBadRequest, "cookie过期")
} }
return nil
info.FileType = TypeComplement(info.FileType) // 格式化后缀,仿止出错
filePath := "./files/" + info.FileType + "/" + info.FileName + info.FileType
data := readFile(filePath)
c.JSON(http.StatusOK, info)
return response.SendResponse3(c, http.StatusOK, GetFileContentType(info.FileType), data) // 返回数据
} }
// 申请一个包含uid的cookie // 申请一个包含uid的cookie
......
...@@ -3,7 +3,7 @@ package model ...@@ -3,7 +3,7 @@ package model
import ( import (
//"fmt" //"fmt"
"time" "time"
// "math/rand" // "math/rand"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"github.com/spf13/viper" "github.com/spf13/viper"
...@@ -12,7 +12,7 @@ import ( ...@@ -12,7 +12,7 @@ import (
) )
var DB *gorm.DB var DB *gorm.DB
var id1 uint = 0 var id1 uint = 12
func Init() { func Init() {
ConnectDatabase() ConnectDatabase()
...@@ -26,7 +26,7 @@ func Init() { ...@@ -26,7 +26,7 @@ func Init() {
if err != nil { if err != nil {
logrus.Fatal(err) logrus.Fatal(err)
} }
err = DB.AutoMigrate(&Sid{},&Url{}) err = DB.AutoMigrate(&Sid{}, &Url{})
if err != nil { if err != nil {
logrus.Fatal(err) logrus.Fatal(err)
} }
...@@ -47,12 +47,13 @@ func ConnectDatabase() { ...@@ -47,12 +47,13 @@ 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)
} }
} }
// Backcheck content through link // Backcheck content through link
//通过链接反查内容 // 通过链接反查内容
func Findlink(_url string) string { func Findlink(_url string) string {
var p Content var p Content
err := DB.First(&p, "Url1 = ?", _url) err := DB.First(&p, "Url1 = ?", _url)
...@@ -63,33 +64,34 @@ func Findlink(_url string) string { ...@@ -63,33 +64,34 @@ 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) {
id1++ id1++
p := Content{ p := Content{
ID : id1, ID: id1,
Route: _Route, Route: _Route,
Time: _Time, Time: _Time,
Passwd: _Passwd, Passwd: _Passwd,
Date: time.Now(), Date: time.Now(),
Time1: _Time1, Time1: _Time1,
Time2: 0, Time2: 0,
Url1: _url, Url1: _url,
} }
DB.Create(&p) DB.Create(&p)
} }
// Check whether the number of visits exceeds the threshold and the time limit // Check whether the number of visits exceeds the threshold and the time limit
//检查是否超过总访问次数和截止时间 // 检查是否超过总访问次数和截止时间
func Checkt(p Content) bool { func Checkt(p Content) bool {
t := time.Now() t := time.Now()
p.Time2++ 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
} }
return false return false
} }
/* /*
这个函数那边有可能需要 这个函数那边有可能需要
//检查sid是否超时 //检查sid是否超时
...@@ -97,17 +99,17 @@ func Checkt1(sid string) bool { ...@@ -97,17 +99,17 @@ 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 {
var s Sid var s Sid
err1 := DB.First(&s, "S = ?",sid).Error err1 := DB.First(&s, "S = ?", sid).Error
//fmt.Println(url.Url1,url.ID,"\n") //fmt.Println(url.Url1,url.ID,"\n")
if err1 != nil { if err1 != nil {
return false return false
...@@ -116,7 +118,7 @@ func Find(sid string,_url string) bool { ...@@ -116,7 +118,7 @@ func Find(sid string,_url string) bool {
DB.Model(&s).Association("Url1").Find(&Urls) DB.Model(&s).Association("Url1").Find(&Urls)
for _, value := range Urls { for _, value := range Urls {
if value.Url1 == _url { if value.Url1 == _url {
return true return true
} }
} }
return false return false
...@@ -134,10 +136,10 @@ func randStr(n int) string { ...@@ -134,10 +136,10 @@ func randStr(n int) string {
return string(b) return string(b)
}*/ }*/
//新建用户 // 新建用户
func Createuser(_User Users) { func Createuser(_User Users) {
//_User.SessionId=randStr(8) //_User.SessionId=randStr(8)
DB.Create(&_User) DB.Create(&_User)
} }
//新建链接 //新建链接
...@@ -147,44 +149,44 @@ func Createuser(_User Users) { ...@@ -147,44 +149,44 @@ func Createuser(_User Users) {
1 表示密码正确 1 表示密码正确
0 表示密码不正确 0 表示密码不正确
*/ */
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) { if Checkt(p) {
return 2 return 2
} }
if p.Passwd == _passwd { if p.Passwd == _passwd {
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},
}, },
} }
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})
} }
return 1 return 1
} }
return 0 return 0
} }
//第一次上传 // 第一次上传
func _Createlink(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)
} }
\ No newline at end of file
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