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 {
* 新建一个sid和url的关联,并返回一个bool表示成功与否
* 若无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 == "" {
rsid = IdGen(8)
} else {
rsid = sid
}
b = model.Createlink(rsid, passwd, url)
return rsid, b
stat = model.Createlink(rsid, passwd, url, time)
return rsid, stat
}
/*
* 判断用户有无权限访问
* 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
if model.Find(sid, url) {
return true
return 1 // 鉴权通过
} else {
if passwd != "" {
var b bool
sid, b = newAuthenticate(sid, url, passwd)
var stat uint
sid, stat = newAuthenticate(sid, url, passwd, time)
cookie.Value = sid
return b
} else {
return false
}
return stat
}
}
......@@ -133,6 +129,7 @@ func srcRead(c echo.Context, info *File) (src multipart.File, err error) {
return nil, err
}
info.FileName = file.Filename
return src, nil
}
......@@ -185,8 +182,8 @@ func DBupdate(c echo.Context, filePath string, info *File) (string, string) {
} else {
sid = cookie.Value
}
sid, _ = newAuthenticate(sid, url, info.Url)
model.Createlink(sid, info.Passwd, url)
sid, _ = newAuthenticate(sid, url, info.Url, info.Time)
model.CreatelinkFirstTime(sid, url, info.Time)
model.Savetext(filePath, 30, info.Passwd, info.Time, url)
return sid, url
}
......@@ -226,3 +223,15 @@ func TypeComplement(typ string) string {
}
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
import (
"backend/app/response"
"backend/model"
"fmt"
"io"
"net/http"
......@@ -12,13 +13,13 @@ import (
)
type File struct {
FileName string `json:"fileName"`
FileType string `json:"fileType"`
Expiration int `json:"expiration"`
User string `json:"user"`
Passwd string `json:"passwd"`
Url string `json:"url"`
Time time.Time `json:"time"` // 用户指定的时间期限
FileName string `json:"fileName" form:"fileName" query:"fileName"`
FileType string `json:"fileType" form:"fileType" query:"fileName"`
Expiration int `json:"expiration" form:"expiration" query:"expiration"`
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"` // 用户指定的时间期限
//Expiry time.Time `json:"expiry"` // 有效期
//Content string `json:"content"`
}
......@@ -37,6 +38,7 @@ func RecvFile(c echo.Context) error {
if err != nil {
return response.SendResponse4(c, err) // log在srcRead里进行
}
info.Time = timeAssign(info.Time) // 默认时间
// 创建目标文件,就是我们打算把用户上传的文件保存到什么地方
// info.Filename 参数指的是我们以用户上传的文件名,作为目标文件名,也就是服务端保存的文件名跟用户上传的文件名一样
dst, filePath := dstCreate(info)
......@@ -58,7 +60,6 @@ func RecvFile(c echo.Context) error {
//sid := "fakesid" //
//url := "fakeurl" //
//cookie := new(http.Cookie)
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 {
logrus.Println(err)
return err
}
info.Time = timeAssign(info.Time) // 默认时间为当前半小时后
cookie, _ := c.Cookie("User")
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) {
return response.SendResponse(c, http.StatusBadRequest, "msg", "", "密码错误")
}
stat := Autheticate(cookie, info.Url, info.Passwd, info.Time) // 包含创建链接Createlink
switch stat {
case 0:
return c.HTML(http.StatusBadRequest, "密码错误")
case 1: // 鉴权通过
info.FileType = TypeComplement(info.FileType) // 格式化后缀,仿止出错
filePath := "./files/" + info.FileType + "/" + info.FileName + 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
}
// 申请一个包含uid的cookie
......
......@@ -12,7 +12,7 @@ import (
)
var DB *gorm.DB
var id1 uint = 0
var id1 uint = 12
func Init() {
ConnectDatabase()
......@@ -26,7 +26,7 @@ func Init() {
if err != nil {
logrus.Fatal(err)
}
err = DB.AutoMigrate(&Sid{},&Url{})
err = DB.AutoMigrate(&Sid{}, &Url{})
if err != nil {
logrus.Fatal(err)
}
......@@ -47,12 +47,13 @@ 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)
}
}
// Backcheck content through link
//通过链接反查内容
// 通过链接反查内容
func Findlink(_url string) string {
var p Content
err := DB.First(&p, "Url1 = ?", _url)
......@@ -63,11 +64,11 @@ func Findlink(_url string) string {
}
// 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++
p := Content{
ID : id1,
ID: id1,
Route: _Route,
Time: _Time,
Passwd: _Passwd,
......@@ -80,16 +81,17 @@ func Savetext(_Route string, _Time uint, _Passwd string,_Time1 time.Time,_url st
}
// Check whether the number of visits exceeds the threshold and the time limit
//检查是否超过总访问次数和截止时间
// 检查是否超过总访问次数和截止时间
func Checkt(p Content) bool {
t := time.Now()
p.Time2++
if p.Time2 > p.Time||t.After(p.Time1) {
if p.Time2 > p.Time || t.After(p.Time1) {
DB.Delete(&p)
return true
}
return false
}
/*
这个函数那边有可能需要
//检查sid是否超时
......@@ -104,10 +106,10 @@ func Checkt1(sid string) bool {
}
}*/
//查找sessionid和url是否关联
func Find(sid string,_url string) bool {
// 查找sessionid和url是否关联
func Find(sid string, _url string) bool {
var s Sid
err1 := DB.First(&s, "S = ?",sid).Error
err1 := DB.First(&s, "S = ?", sid).Error
//fmt.Println(url.Url1,url.ID,"\n")
if err1 != nil {
return false
......@@ -134,7 +136,7 @@ func randStr(n int) string {
return string(b)
}*/
//新建用户
// 新建用户
func Createuser(_User Users) {
//_User.SessionId=randStr(8)
DB.Create(&_User)
......@@ -147,7 +149,7 @@ func Createuser(_User Users) {
1 表示密码正确
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
DB.First(&p, "Url1 = ?", _url)
if Checkt(p) {
......@@ -156,12 +158,12 @@ func Createlink(sid string , _passwd string , _url string,_Time1 time.Time) uint
if p.Passwd == _passwd {
var p1 Sid
err := DB.First(&p1, "S = ?", sid).Error
if err !=nil {
if err != nil {
sid1 := Sid{
S:sid,
S: sid,
_Time: _Time1,
Url1: []Url{
{ Url1: _url},
{Url1: _url},
},
}
DB.Create(&sid1)
......@@ -170,20 +172,20 @@ func Createlink(sid string , _passwd string , _url string,_Time1 time.Time) uint
if t.After(p1._Time) {
return 3
}
DB.Model(&p1).Association("Url1").Append(&Url{Url1:_url})
DB.Model(&p1).Association("Url1").Append(&Url{Url1: _url})
}
return 1
}
return 0
}
//第一次上传
func _Createlink(sid string,_url string,_Time1 time.Time) {
// 第一次上传
func CreatelinkFirstTime(sid string, _url string, _Time1 time.Time) {
sid1 := Sid{
S: sid,
_Time: _Time1,
Url1: []Url{
{ Url1: _url},
{Url1: _url},
},
}
DB.Create(&sid1)
......
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