no message

master
zcy 2021-02-16 14:05:21 +08:00
parent c90b44b2c5
commit 59989de706
4 changed files with 175 additions and 84 deletions

View File

@ -26,46 +26,46 @@ type FileController struct {
} }
func (this *FileController) OnUpload(c *gin.Context) { func (this *FileController) OnUpload(c *gin.Context) {
uid,e := uuid.NewV1() uid, e := uuid.NewV1()
if nil != e{ if nil != e {
log.Print(e.Error()) log.Print(e.Error())
return return
} }
imgtype := c.Query("type") imgtype := c.Query("type")
log.Print(imgtype) log.Print(imgtype)
file, _, err := c.Request.FormFile("image") file, _, err := c.Request.FormFile("image")
if nil != err || nil == file{ if nil != err || nil == file {
log.Print(err.Error()) log.Print(err.Error())
return return
} }
if imgtype == "gif"{ if imgtype == "gif" {
allgifs,er := gif.DecodeAll(file) allgifs, er := gif.DecodeAll(file)
if nil != er{ if nil != er {
log.Print("decode error",er.Error()) log.Print("decode error", er.Error())
return return
} }
datout, err := os.Create("image/" + uid.String() + ".gif" ) datout, err := os.Create("image/" + uid.String() + ".gif")
defer datout.Close() defer datout.Close()
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
return return
} }
gif.EncodeAll(datout,allgifs) gif.EncodeAll(datout, allgifs)
c.JSON(200, map[string] interface{}{"url":uid.String() + ".gif" } ) c.JSON(200, map[string]interface{}{"url": uid.String() + ".gif"})
}else{ } else {
img, name, err := image.Decode(file) img, name, err := image.Decode(file)
if nil != err{ if nil != err {
log.Print(err.Error()) log.Print(err.Error())
return return
} }
dx := img.Bounds().Dx() dx := img.Bounds().Dx()
// resize to width 1000 using Lanczos resampling // resize to width 1000 using Lanczos resampling
// and preserve aspect ratio // and preserve aspect ratio
if(dx > 800){ if dx > 800 {
dx = dx / 2; dx = dx / 2
} }
m := resize.Resize(uint(dx), 0, img, resize.Lanczos3) m := resize.Resize(uint(dx), 0, img, resize.Lanczos3)
@ -75,7 +75,7 @@ func (this *FileController) OnUpload(c *gin.Context) {
log.Fatal(err) log.Fatal(err)
} }
jpeg.Encode(datout, m, nil) jpeg.Encode(datout, m, nil)
c.JSON(200, map[string] interface{}{"url":uid.String() + "." + name } ) c.JSON(200, map[string]interface{}{"url": uid.String() + "." + name})
} }
} }
@ -83,17 +83,17 @@ func (this *FileController) OnUpload(c *gin.Context) {
func (this *FileController) FileList(c *gin.Context) { func (this *FileController) FileList(c *gin.Context) {
var nodes utils.FileDesc var nodes utils.FileDesc
path := c.Query("path") path := c.Query("path")
utils.GetPathFileName("files\\" + path,&nodes) utils.GetPathFileName("files\\"+path, &nodes)
log.Print(nodes) log.Print(nodes)
bs,e := json.Marshal(nodes) bs, e := json.Marshal(nodes)
if nil != e{ if nil != e {
log.Print(e.Error()) log.Print(e.Error())
} }
log.Print(string(bs)) log.Print(string(bs))
c.JSON(200,map[string]interface{}{ c.JSON(200, map[string]interface{}{
"msg":"ok", "msg": "ok",
"status":200, "status": 200,
"files":nodes, "files": nodes,
}) })
} }
@ -102,23 +102,23 @@ func (this *FileController) FileType(c *gin.Context) {
} }
func (this *FileController) DownloadFile(c *gin.Context) { func (this *FileController) DownloadFile(c *gin.Context) {
resp := RespBase{Msg:"FAIL",Status: 211} resp := RespBase{Msg: "FAIL", Status: 211}
filename := c.Query("filename") filename := c.Query("filename")
file,e := os.Open( "files//" +filename) file, e := os.Open("files//" + filename)
if nil != e{ if nil != e {
log.Print(e.Error()) log.Print(e.Error())
c.JSON(200,resp) c.JSON(200, resp)
return return
} }
bytes,e :=ioutil.ReadAll(file) bytes, e := ioutil.ReadAll(file)
if nil != e{ if nil != e {
log.Print(e.Error()) log.Print(e.Error())
c.JSON(200,resp) c.JSON(200, resp)
return return
} }
c.Header("X-Content-Type-Options", "nosniff") c.Header("X-Content-Type-Options", "nosniff")
c.Header("Content-Disposition", c.Header("Content-Disposition",
"/files/" + filename) "/files/"+filename)
c.Writer.Write(bytes) c.Writer.Write(bytes)
} }
@ -127,42 +127,42 @@ func (this *FileController) OnFileUploadFile(c *gin.Context) {
path := c.Query("path") path := c.Query("path")
filename := c.Query("filename") filename := c.Query("filename")
file, _, err := c.Request.FormFile("file") file, _, err := c.Request.FormFile("file")
if nil != err || nil == file{ if nil != err || nil == file {
log.Print(err.Error()) log.Print(err.Error())
return return
} }
bytes,err := ioutil.ReadAll(file) bytes, err := ioutil.ReadAll(file)
if nil != err{ if nil != err {
log.Print(err.Error()) log.Print(err.Error())
return return
} }
header := make([]byte,512) header := make([]byte, 512)
copy(header,bytes) copy(header, bytes)
types := http.DetectContentType(header) types := http.DetectContentType(header)
log.Print(types) log.Print(types)
// jpg // jpg
defer file.Close() defer file.Close()
out, err := os.Create("files/" + path + "/" + filename) out, err := os.Create("files/" + path + "/" + filename)
if err != nil { if err != nil {
log.Print(err) log.Print(err)
} }
defer out.Close() defer out.Close()
out.Write(bytes) out.Write(bytes)
log.Print(len(bytes)) log.Print(len(bytes))
c.JSON(200, map[string] interface{}{"msg": "ok" }) c.JSON(200, map[string]interface{}{"msg": "ok"})
} }
func (this *FileController) OnThumbnail(c *gin.Context) { func (this *FileController) OnThumbnail(c *gin.Context) {
resp := RespBase{Msg:"FAIL",Status: 211} resp := RespBase{Msg: "FAIL", Status: 211}
fileName := c.Param("file") fileName := c.Param("file")
if "" == fileName{ if "" == fileName {
c.JSON(200,resp) c.JSON(200, resp)
return return
} }
// input files // input files
files := []string{"/home/ubuntu/api/bin/image/" +fileName} files := []string{"/home/ubuntu/api/bin/image/" + fileName}
// load images and make 100x100 thumbnails of them // load images and make 100x100 thumbnails of them
var thumbnails []image.Image var thumbnails []image.Image
@ -184,63 +184,62 @@ func (this *FileController) OnThumbnail(c *gin.Context) {
} }
// save the combined image to file // save the combined image to file
err := imaging.Save(dst, "/home/ubuntu/api/bin/image/thumbnail_" + fileName) err := imaging.Save(dst, "/home/ubuntu/api/bin/image/thumbnail_"+fileName)
if err != nil { if err != nil {
log.Print(err.Error()) log.Print(err.Error())
return return
} }
// open file // open file
file,e := os.Open("/home/ubuntu/api/bin/image/thumbnail_" + fileName) file, e := os.Open("/home/ubuntu/api/bin/image/thumbnail_" + fileName)
if nil != e{ if nil != e {
log.Print(e.Error()) log.Print(e.Error())
c.JSON(200,resp) c.JSON(200, resp)
return return
} }
defer file.Close() defer file.Close()
bytes,e :=ioutil.ReadAll(file) bytes, e := ioutil.ReadAll(file)
if nil != e{ if nil != e {
log.Print(e.Error()) log.Print(e.Error())
c.JSON(200,resp) c.JSON(200, resp)
return return
} }
c.Header("X-Content-Type-Options", "nosniff") c.Header("X-Content-Type-Options", "nosniff")
c.Header("Content-Type","image/png") c.Header("Content-Type", "image/png")
c.Header("Content-Disposition", c.Header("Content-Disposition",
"/image/" +fileName) "/image/"+fileName)
c.Writer.Write(bytes) c.Writer.Write(bytes)
} }
func (this *FileController) OnDownLoad(c *gin.Context) {
func (this *FileController) OnDownLoad(c *gin.Context) { resp := RespBase{Msg: "FAIL", Status: 211}
resp := RespBase{Msg:"FAIL",Status: 211}
fileName := c.Param("file") fileName := c.Param("file")
if "" == fileName{ if "" == fileName {
c.JSON(200,resp) c.JSON(200, resp)
return return
} }
file,e := os.Open(utils.GetCurrentDirectory() + "/image/" +fileName) file, e := os.Open(utils.GetCurrentDirectory() + "/image/" + fileName)
if nil != e{ if nil != e {
log.Print(e.Error()) log.Print(e.Error())
c.JSON(200,resp) c.JSON(200, resp)
return return
} }
defer file.Close() defer file.Close()
bytes,e :=ioutil.ReadAll(file) bytes, e := ioutil.ReadAll(file)
if nil != e{ if nil != e {
log.Print(e.Error()) log.Print(e.Error())
c.JSON(200,resp) c.JSON(200, resp)
return return
} }
c.Header("X-Content-Type-Options", "nosniff") c.Header("X-Content-Type-Options", "nosniff")
c.Header("Content-Type","image/png") c.Header("Content-Type", "image/png")
c.Header("Content-Disposition", c.Header("Content-Disposition",
"/image/" +fileName) "/image/"+fileName)
c.Writer.Write(bytes) c.Writer.Write(bytes)
} }

89
controller/plan.go Normal file
View File

@ -0,0 +1,89 @@
package controller
import (
"background/db"
"background/model"
"log"
"strconv"
"github.com/gin-gonic/gin"
)
type PlanController struct {
}
func (this *PlanController) GetPlan(c *gin.Context) {
}
func (this *PlanController) GetPlanDates(c *gin.Context) {
type Req struct {
Date string `json:"date"`
}
var req Req
resp := RespBase{
Msg: "ERROR",
Status: 23,
}
defer c.JSON(200, &resp)
e := c.BindJSON(&req)
if nil != e {
log.Print(e.Error())
return
}
plan := []model.Plan{}
e = db.GetOrm().Model(&plan).Where("date = ?", req.Date).Find(&plan).Error
if nil != e {
log.Print(e.Error())
}
resp.Status = 0
resp.Msg = "OK"
resp.Data = plan
return
}
func (this *PlanController) AddPlan(c *gin.Context) {
var req model.Plan
resp := RespBase{Msg: "r", Status: 0, Data: nil}
defer c.JSON(200, &resp)
e := c.BindJSON(&req)
if nil != e {
log.Print(e.Error())
return
}
e = db.GetOrm().Create(&req).Error
if nil != e {
log.Print(e.Error())
return
}
resp.Msg = "OK"
resp.Status = 0
}
func (this *PlanController) DelPlan(c *gin.Context) {
resp := RespBase{Msg: "r", Status: 0, Data: nil}
defer c.JSON(200, &resp)
var e error
iid := 0
id := c.Query("id")
if "" != id {
iid, e = strconv.Atoi(id)
if nil != e {
log.Print(e.Error())
return
}
} else {
return
}
plan := model.Plan{}
plan.ID = uint32(iid)
e = db.GetOrm().Delete(&plan).Error
if nil != e {
log.Print(e.Error())
return
}
resp.Msg = "OK"
resp.Status = 0
}

20
main.go
View File

@ -27,6 +27,7 @@ var (
userController = controller.UserController{} userController = controller.UserController{}
mailContoller = controller.MailController{} mailContoller = controller.MailController{}
fileController = controller.FileController{} fileController = controller.FileController{}
planController = controller.PlanController{}
) )
func CORSMiddleware(c *gin.Context) { func CORSMiddleware(c *gin.Context) {
@ -149,7 +150,7 @@ func main() {
api.POST("/hardware", controller.AddHardware) // 新增硬件 api.POST("/hardware", controller.AddHardware) // 新增硬件
api.GET("/hardware", controller.ReadHardWare) // 读取硬件 api.GET("/hardware", controller.ReadHardWare) // 读取硬件
api.DELETE("/hardware", controller.DeleteHardWare) // 读取硬件 api.DELETE("/hardware", controller.DeleteHardWare) // 读取硬件
api.POST("/doc_search",) api.POST("/doc_search")
api.PUT("/file", fileController.OnFileUploadFile) // 上传文件 api.PUT("/file", fileController.OnFileUploadFile) // 上传文件
api.GET("/file", fileController.DownloadFile) // 下载 文件 api.GET("/file", fileController.DownloadFile) // 下载 文件
api.GET("/filelist", fileController.FileList) // 文件列表 api.GET("/filelist", fileController.FileList) // 文件列表
@ -168,12 +169,17 @@ func main() {
api.GET("doc_versions", nil) // 获取文章的某个版本 api.GET("doc_versions", nil) // 获取文章的某个版本
api.PUT("/book", controller.CreateBook) // api.PUT("/book", controller.CreateBook) //
api.POST("/book", controller.UpdateBook) // api.POST("/book", controller.UpdateBook) //
api.POST("/getbook", controller.GetBook) // 单书籍数据 api.POST("/getbook", controller.GetBook) // 单书籍数据
api.POST("/getbooks", controller.GetPageBook) // 批量书籍 api.POST("/getbooks", controller.GetPageBook) // 批量书籍
api.POST("/delbook", controller.Delbook) // 删除书籍 api.POST("/delbook", controller.Delbook) // 删除书籍
api.GET("/bookcount", controller.BookCount) // 删除书籍 api.GET("/bookcount", controller.BookCount) // 删除书籍
api.POST("/get_plans", planController.GetPlanDates)
api.PUT("/plan", planController.AddPlan)
api.DELETE("/plan", planController.DelPlan)
} }
openapi := r.Group("openapi") openapi := r.Group("openapi")

View File

@ -9,21 +9,18 @@ import (
"log" "log"
) )
type Plan struct { type Plan struct {
ID uint32 `gorm:"column:id;primary_key" json:"id"` ID uint32 `gorm:"column:id;primary_key" json:"id"`
StartTime utils.OrmTime `gorm:"column:start_time" json:"start_time"` StartTime utils.OrmTime `gorm:"column:start_time" json:"start_time"`
EndTime utils.OrmTime `gorm:"column:end_time" json:"end_time"` EndTime utils.OrmTime `gorm:"column:end_time" json:"end_time"`
Date utils.OrmTime `gorm:"column:date" json:"date"` Date string `gorm:"column:date" json:"date"`
Content string `gorm:"column:content" json:"content"` Content string `gorm:"column:content" json:"content"`
} }
func (m *Plan) TableName() string { func (m *Plan) TableName() string {
return "plan" return "plan"
} }
type Users struct { type Users struct {
ID int64 `sql:"id" json:"id"` ID int64 `sql:"id" json:"id"`
UserName string `sql:"user_name" json:"user_name"` UserName string `sql:"user_name" json:"user_name"`
@ -63,17 +60,17 @@ func GetUsers(limit int32, offsetPage int32, name string) ([]Users, int32) {
return users, cnts[0].Count return users, cnts[0].Count
} }
func ModyfyPassword(UserName string ,Password string) error { func ModyfyPassword(UserName string, Password string) error {
h := md5.New() h := md5.New()
h.Write([]byte(Password)) h.Write([]byte(Password))
query := fmt.Sprintf("update users set user_pwd = '%s' where user_name = '%s' ", query := fmt.Sprintf("update users set user_pwd = '%s' where user_name = '%s' ",
utils.ByteSliceToString(h.Sum(nil)),UserName) utils.ByteSliceToString(h.Sum(nil)), UserName)
n,err := db.GetMysqlClient().Update(query) n, err := db.GetMysqlClient().Update(query)
if nil != err { if nil != err {
logs.Error(err.Error()) logs.Error(err.Error())
return err return err
} }
if n == 0{ if n == 0 {
return nil return nil
} }
return nil return nil