2019-03-07 06:06:29 +00:00
|
|
|
package controller
|
2019-01-29 13:57:53 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/gin-gonic/gin"
|
2019-09-01 16:06:51 +00:00
|
|
|
uuid "github.com/satori/go.uuid"
|
|
|
|
"io/ioutil"
|
2019-01-29 13:57:53 +00:00
|
|
|
"log"
|
2019-09-01 16:06:51 +00:00
|
|
|
"net/http"
|
2019-01-29 13:57:53 +00:00
|
|
|
"os"
|
2019-09-01 16:06:51 +00:00
|
|
|
"strings"
|
2019-01-29 13:57:53 +00:00
|
|
|
)
|
|
|
|
|
2019-03-07 06:06:29 +00:00
|
|
|
type FileController struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *FileController) OnUpload(c *gin.Context) {
|
2019-09-01 16:06:51 +00:00
|
|
|
uid,e := uuid.NewV1()
|
|
|
|
if nil != e{
|
|
|
|
log.Print(e.Error())
|
|
|
|
return
|
2019-01-29 13:57:53 +00:00
|
|
|
}
|
2019-09-01 16:06:51 +00:00
|
|
|
|
|
|
|
file, _, err := c.Request.FormFile("image")
|
|
|
|
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()
|
2019-09-03 08:38:35 +00:00
|
|
|
out, err := os.Create("image/" + uid.String() + ".jpg")
|
2019-09-01 16:06:51 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
defer out.Close()
|
|
|
|
out.Write(bytes)
|
|
|
|
log.Print(len(bytes))
|
2019-12-29 09:42:27 +00:00
|
|
|
c.JSON(200, map[string] interface{}{"url":uid.String() + ".jpg" })
|
2019-01-29 13:57:53 +00:00
|
|
|
}
|
2019-12-28 07:37:49 +00:00
|
|
|
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))
|
2019-12-29 09:42:27 +00:00
|
|
|
c.JSON(200, map[string] interface{}{"url":uid.String() + ".png" })
|
2019-12-28 07:37:49 +00:00
|
|
|
}
|
|
|
|
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))
|
2019-12-29 09:42:27 +00:00
|
|
|
c.JSON(200, map[string] interface{}{"url":uid.String() + ".gif" } )
|
2019-12-28 07:37:49 +00:00
|
|
|
}
|
2019-01-29 13:57:53 +00:00
|
|
|
}
|
|
|
|
|
2019-03-12 06:01:25 +00:00
|
|
|
func (this *FileController) OnDownLoad(c *gin.Context) {
|
2019-09-02 16:22:39 +00:00
|
|
|
resp := RespBase{Msg:"FAIL",Status: 211}
|
2019-09-03 08:38:35 +00:00
|
|
|
|
2019-09-02 16:22:39 +00:00
|
|
|
fileName := c.Param("file")
|
|
|
|
if "" == fileName{
|
2019-09-03 08:38:35 +00:00
|
|
|
c.JSON(200,resp)
|
|
|
|
|
2019-09-02 16:22:39 +00:00
|
|
|
return
|
|
|
|
}
|
2019-12-29 09:42:27 +00:00
|
|
|
file,e := os.Open("/home/ubuntu/api/bin/image/" +fileName)
|
2019-09-02 16:22:39 +00:00
|
|
|
if nil != e{
|
|
|
|
log.Print(e.Error())
|
2019-09-03 08:38:35 +00:00
|
|
|
c.JSON(200,resp)
|
|
|
|
|
2019-09-02 16:22:39 +00:00
|
|
|
return
|
2019-01-29 13:57:53 +00:00
|
|
|
}
|
2019-09-02 16:22:39 +00:00
|
|
|
bytes,e :=ioutil.ReadAll(file)
|
|
|
|
if nil != e{
|
|
|
|
log.Print(e.Error())
|
2019-09-03 08:38:35 +00:00
|
|
|
c.JSON(200,resp)
|
|
|
|
|
2019-09-02 16:22:39 +00:00
|
|
|
return
|
2019-01-29 13:57:53 +00:00
|
|
|
}
|
2019-09-03 08:38:35 +00:00
|
|
|
c.Header("X-Content-Type-Options", "nosniff")
|
2019-09-02 16:22:39 +00:00
|
|
|
c.Header("Content-Type","image/png")
|
|
|
|
c.Header("Content-Disposition",
|
2019-09-03 08:38:35 +00:00
|
|
|
"/image/" +fileName)
|
2019-09-02 16:22:39 +00:00
|
|
|
c.Writer.Write(bytes)
|
2019-01-29 13:57:53 +00:00
|
|
|
}
|
2020-03-27 16:00:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
func (this *FileController) OnThumbnail(c *gin.Context) {
|
|
|
|
resp := RespBase{Msg:"FAIL",Status: 211}
|
|
|
|
|
|
|
|
fileName := c.Param("file")
|
|
|
|
if "" == fileName{
|
|
|
|
c.JSON(200,resp)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
file,e := os.Open("/home/ubuntu/api/bin/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)
|
|
|
|
}
|