From fbf25b0abfad68aa873f10f7182333400e0b112f Mon Sep 17 00:00:00 2001 From: a7458969 <290198252@qq.com> Date: Mon, 20 Apr 2020 23:37:01 +0800 Subject: [PATCH] =?UTF-8?q?=E5=90=8E=E5=8F=B0=E6=8E=A5=E5=8F=A3=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=E6=96=87=E4=BB=B6=E4=B8=8A=E4=BC=A0=E7=AE=A1=E7=90=86?= =?UTF-8?q?=E6=A8=A1=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- controller/file.go | 60 ++++++++++++++++++++++++++++++++++++++++++++++ main.go | 2 ++ model/file.go | 49 +++++++++++++++++++++++++++++++++++++ model/hardware.go | 2 +- mq/mq.go | 7 ++++++ 5 files changed, 119 insertions(+), 1 deletion(-) create mode 100644 model/file.go create mode 100644 mq/mq.go diff --git a/controller/file.go b/controller/file.go index dae8c26..b924442 100644 --- a/controller/file.go +++ b/controller/file.go @@ -73,6 +73,66 @@ func (this *FileController) OnUpload(c *gin.Context) { c.JSON(200, map[string] interface{}{"url":uid.String() + ".gif" } ) } } +func (this *FileController) FileList(c *gin.Context) { + +} + +func (this *FileController) OnUploadFile(c *gin.Context) { + uid,e := uuid.NewV1() + if nil != e{ + log.Print(e.Error()) + return + } + + file, _, err := c.Request.FormFile("file") + if nil != err || nil == file{ + log.Print(err.Error()) + return + } + bytes,err := ioutil.ReadAll(file) + if nil != err{ + log.Print(err.Error()) + return + } + header := make([]byte,512) + copy(header,bytes) + types := http.DetectContentType(header) + log.Print(types) + // jpg + if strings.Contains(types,"jpeg"){ + defer file.Close() + out, err := os.Create("file/" + uid.String() + ".jpg") + if err != nil { + log.Fatal(err) + } + defer out.Close() + out.Write(bytes) + log.Print(len(bytes)) + c.JSON(200, map[string] interface{}{"url":uid.String() + ".jpg" }) + } + if strings.Contains(types,"png"){ + defer file.Close() + out, err := os.Create("image/" + uid.String() + ".png") + if err != nil { + log.Fatal(err) + } + defer out.Close() + out.Write(bytes) + log.Print(len(bytes)) + c.JSON(200, map[string] interface{}{"url":uid.String() + ".png" }) + } + if strings.Contains(types,"gif"){ + defer file.Close() + out, err := os.Create("image/" + uid.String() + ".gif") + if err != nil { + log.Fatal(err) + } + defer out.Close() + out.Write(bytes) + log.Print(len(bytes)) + c.JSON(200, map[string] interface{}{"url":uid.String() + ".gif" } ) + } +} func (this *FileController) OnThumbnail(c *gin.Context) { resp := RespBase{Msg:"FAIL",Status: 211} diff --git a/main.go b/main.go index 69c8f85..258d6e8 100644 --- a/main.go +++ b/main.go @@ -131,6 +131,8 @@ func main() { api.GET("/hardware",controller.ReadHardWare) // 读取硬件 api.DELETE("/hardware",controller.DeleteHardWare) // 读取硬件 + api.PUT("file",fileController.OnUploadFile) + api.GET("filelist",fileController.FileList) } e := r.Run(":" + strconv.Itoa(config.GetPort())) diff --git a/model/file.go b/model/file.go new file mode 100644 index 0000000..c3b72cb --- /dev/null +++ b/model/file.go @@ -0,0 +1,49 @@ +package model + +import ( + "background/db" + "fmt" + "log" + "background/logs" +) + +type File struct { + ID int64 `sql:"id" json:"id"` + Filename string `sql:"filename" json:"filename"` + Size int64 `sql:"size" json:"size"` + Types string `sql:"types" json:"types"` +} + +func GetFiles() ([]File,error){ + ret := []File{} + sql := fmt.Sprintf("select * from files") + e := db.GetBlogMysql().Query2(sql, &ret) + log.Print(ret) + if nil != e { + logs.Error(e.Error()) + return nil,e + } + return ret,nil +} + +func DeleteFiles(id int) error{ + sql := fmt.Sprintf("delete from files where id = '%d'",id) + _,e := db.GetBlogMysql().Query(sql) + if nil != e { + logs.Error(e.Error()) + return e + } + return nil +} + +func AddFile(f File) error{ + sql := fmt.Sprintf("insert into files(filename,size,types) values(%s,%d,%d)",f.Filename, + f.Size,f.Types) + var ret map[string]interface{} + e := db.GetBlogMysql().Query2(sql, &ret) + if nil != e{ + logs.Error(e.Error()) + return e + } + return nil +} \ No newline at end of file diff --git a/model/hardware.go b/model/hardware.go index 4df4401..fc7b411 100644 --- a/model/hardware.go +++ b/model/hardware.go @@ -40,7 +40,7 @@ func (this *Hardware )CreateHardware( ) error{ return errors.New(utils.ERRNULLPOINTER) } log.Print(this.Name) - matchPhraseQuery := elastic.NewMatchQuery("name", "stm32开发板") + matchPhraseQuery := elastic.NewMatchQuery("name", this.Name) existedHardware,e := QueryHardwares(matchPhraseQuery,10,0) log.Print(e,existedHardware) diff --git a/mq/mq.go b/mq/mq.go new file mode 100644 index 0000000..e737415 --- /dev/null +++ b/mq/mq.go @@ -0,0 +1,7 @@ +package mq + +type mq struct { + +} + +