2019-03-07 06:06:29 +00:00
|
|
|
package controller
|
2019-01-29 13:57:53 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"github.com/gin-gonic/gin"
|
2019-09-01 16:06:51 +00:00
|
|
|
uuid "github.com/satori/go.uuid"
|
2019-01-29 13:57:53 +00:00
|
|
|
"io"
|
2019-09-01 16:06:51 +00:00
|
|
|
"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()
|
|
|
|
out, err := os.Create("G://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()})
|
2019-01-29 13:57:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-12 06:01:25 +00:00
|
|
|
func (this *FileController) OnDownLoad(c *gin.Context) {
|
2019-01-29 13:57:53 +00:00
|
|
|
file, header, err := c.Request.FormFile("upload")
|
|
|
|
filename := header.Filename
|
|
|
|
fmt.Println(header.Filename)
|
|
|
|
out, err := os.Create("G://GoPath//image//" + filename + ".png")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
defer out.Close()
|
|
|
|
_, err = io.Copy(out, file)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|