后台接口添加文件上传管理模块

master
a7458969 2020-04-20 23:37:01 +08:00
parent 800ce06907
commit fbf25b0abf
5 changed files with 119 additions and 1 deletions

View File

@ -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}

View File

@ -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()))

49
model/file.go Normal file
View File

@ -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
}

View File

@ -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)

7
mq/mq.go Normal file
View File

@ -0,0 +1,7 @@
package mq
type mq struct {
}