package controller import ( "background/utils" "github.com/disintegration/imaging" "github.com/gin-gonic/gin" uuid "github.com/satori/go.uuid" "image" "image/color" "io/ioutil" "log" "net/http" "os" "strings" ) type FileController struct { } func (this *FileController) OnUpload(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("image/" + 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} fileName := c.Param("file") if "" == fileName{ c.JSON(200,resp) return } // input files files := []string{"/home/ubuntu/api/bin/image/" +fileName} // load images and make 100x100 thumbnails of them var thumbnails []image.Image for _, file := range files { img, err := imaging.Open(file) if err != nil { panic(err) } thumb := imaging.Thumbnail(img, 100, 100, imaging.CatmullRom) thumbnails = append(thumbnails, thumb) } // create a new blank image dst := imaging.New(100*len(thumbnails), 100, color.NRGBA{0, 0, 0, 0}) // paste thumbnails into the new image side by side for i, thumb := range thumbnails { dst = imaging.Paste(dst, thumb, image.Pt(i*100, 0)) } // save the combined image to file err := imaging.Save(dst, "/home/ubuntu/api/bin/image/thumbnail_" + fileName) if err != nil { log.Print(err.Error()) return } // open file file,e := os.Open("/home/ubuntu/api/bin/image/thumbnail_" + fileName) if nil != e{ log.Print(e.Error()) c.JSON(200,resp) return } bytes,e :=ioutil.ReadAll(file) if nil != e{ log.Print(e.Error()) c.JSON(200,resp) return } c.Header("X-Content-Type-Options", "nosniff") c.Header("Content-Type","image/png") c.Header("Content-Disposition", "/image/" +fileName) c.Writer.Write(bytes) } func (this *FileController) OnDownLoad(c *gin.Context) { resp := RespBase{Msg:"FAIL",Status: 211} fileName := c.Param("file") if "" == fileName{ c.JSON(200,resp) return } file,e := os.Open(utils.GetCurrentDirectory() + "/image/" +fileName) if nil != e{ log.Print(e.Error()) c.JSON(200,resp) return } bytes,e :=ioutil.ReadAll(file) if nil != e{ log.Print(e.Error()) c.JSON(200,resp) return } c.Header("X-Content-Type-Options", "nosniff") c.Header("Content-Type","image/png") c.Header("Content-Disposition", "/image/" +fileName) c.Writer.Write(bytes) }